Story

In earlier blogs, we have gone through different cloud platforms like Azure UbidotsThingSpeak Losant etc. We have been using the MQTT protocol for sending the sensor data to the cloud in almost all the cloud platform. For more information on MQTT, its advantages and benefits over HTTP protocol you can refer to this blog.

In this blog, we will zoom into yet another and most familiar cloud platform Amazon Web Services. Many of you might be familiar with AWS aka Amazon Web Services and the cloud functionality provided by AWS. It has been the core of web development for many years. With the increasing scale of IoT applications, AWS has come up with the solution of AWSIoT. AWSIoT is a reliable solution for hosting our IoT applications.

By following this blog:

Setting Up AWS Account

Setting Up AWS account is fairly easy. You just need to upload a couple of certificates, attach policies to it, Register the device and start receiving the sensor data messages in AWS.

To set up the AWS account follow this tutorial .

Wireless Vibration and Temperature Sensors

This is a Long Range Industrial IoT wireless vibration and temperature sensor , boasting up to a 2 Mile range using a wireless mesh networking architecture. Incorporating a 16-bit Vibration and Temperature sensor, this sensor transmits highly accurate vibration data at user-defined intervals. It has the following features :

ESP32 AWS Firmware

To connect to AWS and to start sending the data go through the following steps

git clone https://github.com/ExploreEmbedded/Hornbill-Examples.git

Now let's go through the code:

Getting the Sensor Data From Wireless Vibration and Temperature Sensor

We are getting a 54-byte frame from the Wireless Temperature and Vibration Sensors. This frame is manipulated to get the actual temperature and Vibration data.

ESP32 has three UARTs available for the Serial use

and 3 hardware Serial ports

First, initialize Hardware Serial header file. Here we will be using RX2 and TX2 aka. GPIO 16 and GPIO 17 pins of the ESP32 board to get the serial data.

#include <HardwareSerial.h>
# define RXD2 16
# define TXD2 17
Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2); // pins 16 rx2, 17 tx2, 19200 bps, 8 bits no parity 1 stop bit 

Following steps will lead you further to get the real sensor values

if (Serial2.available())  {    Serial.println("Read Serial");    
data[0] = Serial2.read();    
delay(k);   
if(data[0]==0x7E) {    
Serial.println("Got Packet");    
while (!Serial2.available());    
for ( i = 1; i< 55; i++)  {      
data[i] = Serial2.read();      
delay(1); }    
if(data[15]==0x7F)  /////// to check if the recive data is correct      
{  if(data[22]==0x08)  //////// make sure the sensor type is correct  
{  rms_x = ((uint16_t)(((data[24])<<16) + ((data[25])<<8) + (data[26]))/100);  
rms_y = ((uint16_t)(((data[27])<<16) + ((data[28])<<8) + (data[29]))/100);  
rms_z = ((uint16_t)(((data[30])<<16) + ((data[31])<<8) + (data[32]))/100);  
int16_t max_x = ((uint16_t)(((data[33])<<16) + ((data[34])<<8) + (data[35]))/100);  
int16_t max_y = ((uint16_t)(((data[36])<<16) + ((data[37])<<8) + (data[38]))/100);  
int16_t max_z = ((uint16_t)(((data[39])<<16) + ((data[40])<<8) + (data[41]))/100);int16_t min_x = ((uint16_t)(((data[42])<<16) + ((data[43])<<8) + (data[44]))/100);  
int16_t min_y = ((uint16_t)(((data[45])<<16) + ((data[46])<<8) + (data[47]))/100);  
int16_t min_z = ((uint16_t)(((data[48])<<16) + ((data[49])<<8) + (data[50]))/100);cTemp = ((((data[51]) * 256) + data[52]));  
float battery = ((data[18] * 256) + data[19]);  
voltage = 0.00322 * battery;  Serial.print("Sensor Number  ");  

Serial.println(data[16]);  
senseNumber = data[16];   
Serial.print("Sensor Type  ");  
Serial.println(data[22]); 
 Serial.print("Firmware Version  ");  
Serial.println(data[17]);  
Serial.print("Temperature in Celsius :");  
Serial.print(cTemp);  Serial.println(" C");    
Serial.print("RMS vibration in X-axis :");  
Serial.print(rms_x);  Serial.println(" mg");  
Serial.print("RMS vibration in Y-axis :");  
Serial.print(rms_y);  Serial.println(" mg");  
Serial.print("RMS vibration in Z-axis :");  
Serial.print(rms_z);  Serial.println(" mg");Serial.print("Min vibration in X-axis :");  
Serial.print(min_x);  Serial.println(" mg");  
Serial.print("Min vibration in Y-axis :");  
Serial.print(min_y);  Serial.println(" mg");  
Serial.print("Min vibration in Z-axis :");  
Serial.print(min_z);  Serial.println(" mg"); Serial.print("ADC value:");  Serial.println(battery);  
Serial.print("Battery Voltage:");  Serial.print(voltage);  
Serial.println("\n");  
if (voltage < 1) {    
Serial.println("Time to Replace The Battery");  }          
}      
}else{      
for ( i = 0; i< 54; i++)    
{      
Serial.print(data[i]);      
Serial.print(" , ");      
delay(1);    
}}    
}  
}

Connecting to AWS

//*********AWS Credentials*************//
char HOST_ADDRESS[]="*****************.us-east-1.amazonaws.com";char 
CLIENT_ID[]= "Enter policy name here";char TOPIC_NAME[]= "Enter topic name here"; 
const char *format = "{\"SensorId\":\"%d\", \"messageId\":%d, \"rmsX\":%d, \"rmsY\":%d, \"rmsZ\":%d, \"cTemp\":%d,\"voltage\":%.2f}";
AWS_IOT esp; //Instance of AWS_IOT class
void reconnectMQTT()
{if(hornbill.connect(HOST_ADDRESS,CLIENT_ID)== 0)    
{Serial.println("Connected to AWS");        
delay(1000);
if(0==hornbill.subscribe(TOPIC_NAME,mySubCallBackHandler))        
{            
Serial.println("Subscribe Successfull");        
}        
else{            
Serial.println("Subscribe Failed, Check the Thing Name and Certificates");            
while(1);        
}    
}    
else    
{ Serial.println("AWS connection failed, Check the HOST Address");        
while(1);    
}
delay(2000);  
}
if(tick >= 60)   // publish to topic every 5seconds    
{        
tick=0;        
char payload[PAYLOAD_MAX_LEN];           
snprintf(payload,PAYLOAD_MAX_LEN,format,senseNumber,msgCount++,rms_x,rms_y,rms_z,cTemp,voltage);        
Serial.println(payload);        
if(hornbill.publish(TOPIC_NAME,payload) == 0){                    
Serial.print("Publish Message:");            
Serial.println(payload);        
}        
else{           
Serial.println("Publish failed");        
}    
}      
vTaskDelay(1000 / portTICK_RATE_MS);     
tick++;

Visualizing Data in AWS

Overall Code

You can find the overall code on this Github Repository.

Credits