The temperature and humidity sensors come in various models, such as DHT11 and HTU21D. However, due to parameters such as accuracy and sensitivity, they are not suitable for industrial-grade temperature and humidity monitoring. Their accuracy and sensitivity are both poor. Therefore, in this article, we will use the SHT31 temperature/humidity sensor. They are the best and highest precision devices you can get. Digital sensors using the I2C interface can easily read humidity and temperature. The SHT31 sensor has excellent ±2% relative humidity and ±0.3°C temperature accuracy in most cases.
In this article, we will connect the SHT31 temperature/humidity sensor to the NodeMCU ESP8266 development board and then send the data to the ThingSpeak server. ThingSpeak is an open-source IoT application that allows users to store and retrieve data from IoT devices using HTTP and MQTT protocols via the provided API.
Required Components
● NodeMCU ESP8266 development board
● SHT31 sensor
● Micro-USB data cable
● Connecting wires.
SHT31 Humidity Temperature Sensor
The SHT31 is the next generation temperature and humidity sensor from Sensirion. Compared to previous products, the SHT31 has higher intelligence, reliability, and improved accuracy metrics. Its features include enhanced signal processing, temperature, and humidity, which can be read using I2C communication. This I2C mini module uses a standardized sensor package for easy reading of temperature and humidity. Insert the interface module to access the cloud from anywhere in the world.
All I2C mini modules are designed to operate at 5V DC. With a convenient 4-pin header, devices can be connected to the I2C bus in a daisy-chain manner without soldering. Just connect the devices needed for the next automation application.
Schematic Diagram of SHT31 Connected to NodeMCU ESP8266
Below is a schematic diagram of connecting the SHT31 humidity temperature sensor to the NodeMCU ESP8266.
Connect the VCC pin of the SHT31 to the 3.3V of the ESP8266 and connect GND to GND. As shown in the figure above, connect the SCL and SDA pins of the SHT31 to the SCL (D1) and SDA (D2) pins of the ESP8266, respectively.
Setting Up ThingSpeak
1. Go to the website https://thingspeak.com/, and if you do not have an account, create a new account and log in.
2. Click the create button to create a new channel. Enter basic details for the channel, such as Field 1 and Field 2. Then scroll down and save the channel.
3. Then go to the API key and copy this key into a separate text file. You will need it later while programming.
Source Code
Below is the source code for connecting the SHT31 to the NodeMCU ESP8266. You can copy this code and upload it to the ESP8266 development board. However, before that, you need the SHT31 Sensor library. So, download the library from the link below.
● Download SHT31 library
#include <ESP8266WiFi.h>#include <Arduino.h>#include <Wire.h>#include "Adafruit_SHT31.h"
String apiKey = "PW3AKTNO270BFQGT"; // Enter your Write API key from ThingSpeak
const char *ssid = "BYNARK"; // replace with your wifi ssid and wpa2 key
const char *pass = "bynark@123";
const char* server = "api.thingspeak.com";
WiFiClient client;
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup() {Serial.begin(115200);
while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Connecting to ");Serial.println(ssid);WiFi.begin(ssid, pass);while (WiFi.status() != WL_CONNECTED){ delay(500); Serial.print(".");} Serial.println(""); Serial.println("WiFi connected");
Serial.println("SHT31 test");if (! sht31.begin(0x44)) // Set to 0x45 for alternate i2c addr{ Serial.println("Couldn't find SHT31");while (1) delay(1);}}
void loop() { float t = sht31.readTemperature(); float h = sht31.readHumidity();
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com { String sendData = apiKey+"&field1="+String(t)+"&field2="+String(h)+"
"; //Serial.println(sendData);
client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(sendData.length()); client.print("\n\n"); client.print(sendData); }
if (! isnan(t)) // check if 'is not a number'{Serial.print("Temp *C = "); Serial.println(t);
} else {Serial.println("Failed to read temperature");}
if (! isnan(h)) // check if 'is not a number'{ Serial.print("Hum. % = "); Serial.println(h);
} else {Serial.println("Failed to read humidity");}Serial.println();delay(1000);}
Monitoring Data on ThingSpeak
After uploading, open the serial monitor and set the baud rate to 115200. Press the reset button on the ESP8266. The wifi connection status should print along with the humidity and temperature values in the serial monitor.
Now, you can go to the ThingSpeak webpage to check the data online. The data will be updated to the ThingSpeak server after a 15-second interval.
For more content, please click“Read Original” 》》