In this project, we will build a cool temperature and humidity monitoring system using the NodeMCU development board. This project contains an ESP8266 NodeMCU, a DHT11 temperature and humidity sensor and an LCD display for showing real-time values of surroundings.
Also, we include Blynk software for monitoring real-time data through Android phones. Being IoT-based devices, we can discover and operate this from anywhere in the world through an internet connection.
Must Read IoT-Based Projects
Principle Behind Temperature And Humidity Monitoring System
The principle behind this project is simple. NodeMCU works here as a server. It connects with the Blynk cloud through an internet connection. While ambient temperature is sensed by the DHT11 temperature sensor, it sends the data to the server. Then the LCD display shows the real-time temperature and humidity after processing in the server.
Project
Circuit Diagram
Components Required
- ESP8266 NodeMCU Board
- DHT11 Sensor
- 16×2 LCD Display
- I2C Module
- Wires
- 5V Power Supply
Preparing Blynk for Temperature And Humidity Monitoring System
For creating a project on the Blynk platform, you first need to sign up on Blynk. In case if you already have an account on Blynk, sign in using your user id and password. For creating your account, go to www.blynk.com. I used the Blynk app for easy setup.
Click on sign-up if you don’t have an account and if you already have an account, click on sign in. After clicking on sign-up, fill up your details. After this verify your E-mail id and then continue.
Then click on “New Project”, enter the name of the project and choose the connection type and click on “Create”.
Once sign-in after successful account verification, create a new project by clicking the “New Project” button and confirm. Now click on the “+” icon in the right corner and add the Gauge and LCD widgets.
Let’s click the widgets one after another and set up the widget’s settings. First, click on the LCD widget and change its virtual pins to V0 and V1 (values from 0 to 100). Next, click on the Gauge widgets and name them. Set the temperature Gauge as A0 and the humidity Gauge as A1. Also, change the values from 0 to 100. That’s it.
Arduino Code for Temperature And Humidity Monitoring System
Go to File and select Preferences and paste the link “https://arduino.esp8266.com/stable/package_esp8266com_index.json” in Additional Board Manager URLs to add the ESP8266 board. Open Boards Manager from the Tools menu and type ESP8266 to install the ESP8266 platform.
The library files that are needed to compile this code; are Adafruit Sensor Library, Blynk Library, DHT11 Library, and I2C Library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include <LiquidCrystal_I2C.h> #define BLYNK_PRINT Serial #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> #include <DHT.h> LiquidCrystal_I2C lcd(0x27, 16, 2); DHT dht(D3, DHT11); BlynkTimer timer; char auth[] = ""; //Enter the Auth Code char ssid[] = ""; //Enter your WIFI Name char pass[] = ""; //Enter your WIFI Password void weather() { float h = dht.readHumidity(); float t = dht.readTemperature(); int r = analogRead(A0); bool l = digitalRead(D4); r = map(r, 0, 1023, 100, 0); if (isnan(h) || isnan(t)) { Serial.println("Failed To Read From DHT Sensor"); return; } Blynk.virtualWrite(V0, t); //V0 is for Temperature Blynk.virtualWrite(V1, h); //V1 is for Humidity Blynk.virtualWrite(V2, r); //V2 is for Rainfall if (l == 0) { WidgetLED led1(V3); led1.on(); lcd.setCursor(9, 1); lcd.print("L:"); lcd.print("High"); lcd.print(" "); } else if (l == 1) { WidgetLED led1(V3); led1.off(); lcd.setCursor(9, 1); lcd.print("L :"); lcd.print("Low"); lcd.print(" "); } lcd.setCursor(0, 0); lcd.print("Temp:"); lcd.print(t); lcd.setCursor(0, 1); lcd.print("Hum:"); lcd.print(h); lcd.setCursor(9, 0); lcd.print("R:"); lcd.print(r); lcd.print(" "); } void setup() { Serial.begin(9600); lcd.init(); lcd.backlight(); Blynk.begin(auth, ssid, pass); dht.begin(); // Setup a function to be called every second timer.setInterval(10L, weather); } void loop() { Blynk.run(); timer.run(); } |
Code Explanation
1 2 3 4 5 | #include <LiquidCrystal_I2C.h> #define BLYNK_PRINT Serial #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> #include <DHT.h> |
These include library files that are helping to compile the program.
1 2 3 | LiquidCrystal_I2C lcd(0x27, 16, 2); DHT dht(D3, DHT11); BlynkTimer timer; |
The I2C module and DHT11 temperature sensor are defined here.
1 2 3 | char auth[] = ""; //Enter the Auth Code char ssid[] = ""; //Enter your WIFI Name char pass[] = ""; //Enter your WIFI Password |
Now check your mailbox for Blynk auth token for creating the server. Also, put down your WiFi name and password here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | void weather() { float h = dht.readHumidity(); float t = dht.readTemperature(); int r = analogRead(A0); bool l = digitalRead(D4); r = map(r, 0, 1023, 100, 0); if (isnan(h) || isnan(t)) { Serial.println("Failed To Read From DHT Sensor"); return; } Blynk.virtualWrite(V0, t); //V0 is for Temperature Blynk.virtualWrite(V1, h); //V1 is for Humidity Blynk.virtualWrite(V2, r); //V2 is for Rainfall if (l == 0) { WidgetLED led1(V3); led1.on(); lcd.setCursor(9, 1); lcd.print("L :"); lcd.print("High"); lcd.print(" "); } else if (l == 1) { WidgetLED led1(V3); led1.off(); lcd.setCursor(9, 1); lcd.print("L :"); lcd.print("Low"); lcd.print(" "); } lcd.setCursor(0, 0); lcd.print("T :"); lcd.print(t); lcd.setCursor(0, 1); lcd.print("H :"); lcd.print(h); lcd.setCursor(9, 0); lcd.print("R :"); lcd.print(r); lcd.print(" "); } |
This is the main function of this project. This code will operate the whole process of the system.
1 2 3 4 5 6 7 8 9 | void setup() { Serial.begin(9600); lcd.init(); lcd.backlight(); Blynk.begin(auth, ssid, pass); dht.begin(); // Setup a function to be called every second timer.setInterval(10L, weather); } |
Now come to the setup function. Setting up an LCD display through the I2C module is vital to show the result. So this code is for setting up the LCD and serial monitor.
1 2 3 4 | void loop() { Blynk.run(); timer.run(); } |
The Blynk library will run in this loop function.
Great the way you explain the code makes things clearer
Thanks