SI7021 with ESP32
In this tutorial, we will see how to interface the Si7021 humidity and temperature sensor with the ESP32 board and send the data to the IIOT Platform. The Si7021 is a humidity and temperature sensor. The Si7021 I2C sensor has humidity and temperature sensor elements, an analogue-to-digital converter, signal processing, and calibration data. Here is a picture of the Si7021 humidity and temperature sensor and the ESP32 board.
Fig 1: SI7021 And ESP32
We will use the Witty Fox Storm Board with the IIOT Platform library to read the humidity and temperature readings and send them to the IIOT Platform.
Step 1:
Hardware Setup
The Si7021 Humidity and Temperature Sensor is directly compatible with the ESP32 Board through the I2C ports P9 and P10. These are the connections you need to make from the ESP32 board to Si7021:
ESP32 board | Si7021 |
---|---|
3.3 V | VCC |
GND | GND |
P9(SCL) | SCL |
P10(SDA) | SDA |
Fig 2: SI7021 With ESP32 Connections
Step 2:
Retrieve MQTT Credentials
Go to the project tab. From there, you can copy your MQTT username and token inside the setting section and your MQTT topic inside the assets section that is needed in the program.
Fig 3: Settings Section
Step 3:
Install Required Libraries
(downloading the libraries):
Connect your ESP32 board with the Si7021 humidity and temperature sensor. To send the sensor readings to the IIOT Platform, install the Adafruit Si7021 and PubSubClient libraries using the Library Manager in the Arduino IDE. Go to Sketch > Include Library > Manage Libraries and search for the library name in the library manager.
Fig 4: Arduino Library Manger
You can download IIOTPlatform library from HERE DOWNLOAD THE LIBRARY
Step 4:
Implement Arduino Sketch
First, you need to include the necessary libraries. Create an Adafruit si7021 object called sensor and an IIOTPlatform object called obj. Next, you have to set the network credentials in the initWiFi() section, the MQTT credentials in the setCredentials() section, and the MQTT topic in the setTopic() section for the ESP32 board. After that, we check whether the Si7021 module is working or not. In the loop function, we use the Adafruit sensor object to read the data from the si7021 sensor and send it to the IIOT Platform using the IIOTPlatform sensor object.
Program
#include "Adafruit_Si7021.h"
#include "IIOTPlatform.h"
IIOT obj;
Adafruit_Si7021 sensor = Adafruit_Si7021();
void setup() {
Serial.begin(115200);
obj.debugMode(true);
obj.initWifi("wifi_ssid", "WiFi_password");
obj.setCredentials("mqtt_username", "mqtt_password");
obj.initMqtt();
obj.setTopic("mqtt_topic");
while (!Serial) {
delay(10);
}
Serial.println("Si7021 test!");
if (!sensor.begin()) {
Serial.println("Did not find Si7021 sensor!");
while (true);
}
Serial.print("Found model ");
switch(sensor.getModel()) {
case SI_Engineering_Samples:
Serial.print("SI engineering samples"); break;
case SI_7013:
Serial.print("Si7013"); break;
case SI_7020:
Serial.print("Si7020"); break;
case SI_7021:
Serial.print("Si7021"); break;
case SI_UNKNOWN:
default:
Serial.print("Unknown");
}
Serial.print(" Rev(");
Serial.print(sensor.getRevision());
Serial.print(")");
Serial.print(" Serial #");
Serial.print(sensor.sernum_a, HEX);
Serial.println(sensor.sernum_b, HEX);
}
void loop () {
float temperature = sensor.readTemperature();
float humidity = sensor.readHumidity();
obj.addParameter("temperature", temperature);
obj.addParameter("humidity", humidity);
obj.publish();
Serial.print("Humidity: ");
Serial.print(sensor.readHumidity(), 2);
Serial.print(" Temperature: ");
Serial.println(sensor.readTemperature(), 2);
delay(1000);
}
Step 5:
Upload Code to ESP32 Board
After inserting your network credentials, MQTT credentials, and MQTT topic in the code, upload the code to your ESP32 board.
Step 6:
Monitor Data on IIOT Platform
Allow the programme to run for a while so that you can view some useful data on the website. A sample set of readings stored on the website is shown below in graphical format:
Fig 5: Graphical Data Representation