Introduction: Getting Started With AWS IoT With Wireless Temperature Sensor Using MQTT
In earlier Instructables, we have gone through different cloud platforms like Azure , Ubidots , ThingSpeak , 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 instructable.
In this instructable, 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 instructable:
- You will be able to set up AWS account for your IoT application
- You will be able to connect the ESP32 to AWS IoT core
- Send and receive messages using MQTT and HTTP protocol
- Visualize the sent data in AWS
Step 1: 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 .
Step 2: Hardware and Software Specifications
Software Specification
Hardware Specification
Step 3: 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 :
- Industrial Grade 3-axis Vibration Sensor with ±32g Range
- Calculates RMS, MAX, and MIN g Vibration
- Noise Removal using Low-pass Filter
- Frequency Range (Bandwidth) up to 12,800 Hz
- Sample Rate up to 25,600Hz
- Encrypted Communication with 2 Mile Wireless Range
- Operating Temperature Range -40 to +85 °C
- Wall-Mounted or Magnet Mounted IP65 Rated EnclosureExample Software for Visual Studio and LabVIEW
- Vibration Sensor with External Probe Option
- Up to 500,000 Transmissions from 4 AA BatteriesMany Gateway and Modem Options Available
Step 4: ESP32 AWS Firmware
To connect to AWS and to start sending the data go through the following steps
- Download the AWS library from the following Github repository
- clone the repo and place the AWS_IOT file to the library folder of the Arduino directory
git clone <a href="https://github.com/ExploreEmbedded/Hornbill-Examples.git" rel="nofollow"> https://github.com/ExploreEmbedded/Hornbill-Examp...</a>
Now let's go through the code:
- In this application, we have made use of a captive portal to save the WiFi credentials and to hover through the IP settings. For the detailed introduction on the captive portal, you can go through the following instructable .
- The captive portal gives us the option to choose between Static and DHCP settings. Just enter the credentials like Static IP, Subnet Mask, gateway and the Wireless Sensor Gateway will get configured on that IP.
- A webpage is being hosted where a list showing available WiFi networks and there RSSI. Select the WiFi network and password and enter submit. The credentials will be saved in the EEPROM and the IP setting will be saved in the SPIFFS. More on this can be found in this instructable .
Step 5: 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
- RX0 GPIO 3, TX0 GPIO 1
- RX1 GPIO9, TX1 GPIO 10
- RX2 GPIO 16, TX2 GPIO 17
and 3 hardware Serial ports
- Serial
- Serial1
- Serial2
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 17Serial2.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
- Create variables for storing temperature, humidity, battery, and other sensor values
- Set the Rx, tx pin, baud rate and parity bits for the hardware serial
- First, check that there is something to read using Serial1.available()
- We will get the frame of 54 bytes.
- Check for 0x7E which is start byte.
- Vibration data consists of RMS value for the 3 axis, min values for 3 axis, max values for 3 axis.
- temperature and battery values will be containing 2 bytes of data
- get the sensor name, type, sensor version will be containing 1 byte of data and can be got from there respective address
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); } } } }
Step 6: Connecting to AWS
- Include AWS_IOT.h, WiFi.h header files to set up a connection with AWSIoT hub
- Enter your Host Address, Client Id which will be the policy name, and topic name which will be the thing name
//*********AWS Credentials*************//
char HOST_ADDRESS[]="a2smbp7clzm5uw-ats.iot.us-east-1.amazonaws.com"; char CLIENT_ID[]= "ncdGatewayPolicy"; char TOPIC_NAME[]= "ncdGatewayThing";
- Create a char variable to store your JSON, in this case, we have created a format to store JSON
const char *format = "{\"SensorId\":\"%d\", \"messageId\":%d, \"rmsX\":%d, \"rmsY\":%d, \"rmsZ\":%d, \"cTemp\":%d,\"voltage\":%.2f}";
- Create an instance of the AWS_IOT class
AWS_IOT esp; //Instance of AWS_IOT class
- Now connect to AWSIoT hub using the following method.
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); }
- publish the sensor data after every 1 minute.
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++;
Step 7: Visualizing Data in AWS
- Login to your AWS account.
- on the Left corner of the toolbar, you will find Services tab
- Click on this tab and under the Internet of Things heading select IoT Core.
- Select the QoS and no. of messages to subscribers. Enter the topic name.
Step 8: Overall Code
You can find the overall code on this Github Repository.
Credits
- Arduino Json
- Wireless Temperature and Humidity Sensors
- ESP32
- PubSubClient