IoT based ESP32 Wi-Fi Weather Station using DHT11 and BMP180 Sensor

IoT based ESP32 Wi-Fi Weather Station

In this tutorial, we will make an ESP32 based Weather station in which we will build an ESP32 Webserver to display temperature, humidity, pressure, luminosity, and altitude values on a web browser. Here DHT11 sensor is used to get the temperature and humidity data, BMP180 is used to get pressure and altitude data, and an LDR is used to get Luminosity data. This weather data can be monitored from anywhere in the world using the IP address of ESP32. It can also be uploaded to ThingSpeak to make it more interactive as we have done in Raspberry Pi weather station

 

In IoT based applications, Webservers are used to control or monitor any sensor values using web browsers, we previously created many webservers using Arduino, ESP8266, NodeMCU, ESP32, Raspberry Pi, etc.

 

DHT11 Temperature and Humidity Sensor

The DHT11 sensor is very popular for measuring Temperature and humidity, and we previously used DHT11 to build weather stations with other microcontrollers like Arduino, Raspberry Pi, etc. It is industry calibrated and gives a digital output which makes it very easy to interface with all types of MCUs. It has only 3 pins (Data, Vcc, and GND) as shown in the picture below.

DHT11 Sensor

Check all the DHT11 based Projects here.

 

BMP180 Pressure Sensor

BMP180 is a barometric pressure sensor that is mainly used to measure altitude. It also has one inbuilt temperature sensor. It works with the I2C interface. BMP180 sensor measures the absolute pressure of the air around it. The pressure value depends on both the weather and altitude.

BMP180 Pressure Sensor

 

Components Required

  1. ESP32 Dev Module
  2. DHT11 Temperature and Humidity Sensor
  3. BMP180 Pressure Sensor
  4. LDR (Light Dependent Resistor)
  5. Resistors
  6. Breadboard
  7. Male-Female wires

 

ESP32 Wi-Fi Weather Station Circuit Diagram

ESP32 Wi-Fi Weather Station Circuit Diagram

 

ESP32, DHT11 and BMP180 connections: 

  1. Connect D22 pin(SCL) of ESP32 -> SCL pin of BMP180
  2. Connect D21 pin(SDA) of ESP32 -> SDA pin of BMP180
  3. Connect D13 pin of ESP32 -> Data pin of DHT11
  4. Connect D12 pin of ESP32 -> output of voltage divider
  5. Connect 3.3v pin of ESP32 -> Vcc of sensors
  6. Connect GND of ESP32 -> GND of sensors

IoT Weather Station using ESP32

 

Code Explanation

Here ESP32 is programmed with Arduino IDE and complete code with a demonstration Video is given at the end of the project.

We also need to have four libraries for this IoT based Weather station- WiFi, DHT (for DHT sensor), Adafruit_BMP085 (for BMP180), and Wire (for I2C).

Download these libraries from these links: DHT11 library, BMP180 and install them in Arduino IDE by going in Include library in the Sketch menu.

First, include all the required libraries.

#include <WiFi.h>
#include "DHT.h"
#include <Adafruit_BMP085.h>
#include <Wire.h>

 

Then, initialize variables and make instances for DHT11, BMP180, Wi-Fi name, Wi-Fi password, and some other variables used in the code.

#define DHTPIN 13     
#define DHTTYPE DHT11  
Adafruit_BMP085 bmp;
DHT dht(DHTPIN, DHTTYPE);
const char* ssid     = "********"; // Your ssid
const char* password = "*****"; // Your Password

 

Next, declare an object of the Wi-Fi library to access its functions. The argument for the instance formed will be the port number where the server is listening. Since 80 is the default port for HTTP, we will use this value.

WiFiServer server(80);

 

In Void setup() function, initialize the baud rate , DHT sensor and BMP180 using .begin() function then connect the module with the Wi-Fi using WiFi.begin(ssid, password); function.

void setup() {
Serial.begin(115200);
 delay(100);
  dht.begin();
   if (!bmp.begin()) {
 Serial.println("Could not find a valid BMP085 sensor, check wiring!");
..
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
..

 

Now, start the servers using the server.begin(); function and print the IP address of ESP32. This IP address will be used to access weather data on the browser.

server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}…
..

 

In void loop() function, get luminosity by reading the analog pin 12 where LDR is connected. Also, get the temperature in Celsius and Fahrenheit and store this data in different variables using dht.read() function. Then, get the altitude and pressure using the bmp.readPressure function. The pressure will be in millibar. Then, print this data on the serial monitor.

ESP32 has 12-bit ADC by default so, LDR readings will be from 0-4095, where 4095 is considered as max brightness and 0 as min brightness.

int ldrValue = analogRead(12); 
Serial.println(ldrValue, DEC); // prints the value read
 float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
float p = bmp.readPressure()/100;
   Serial.print("Pressure = ");
    Serial.print(p);
    Serial.println(" mb");
     float a = bmp.readAltitude();
..

 

Next, design the web page using HTML and send each line using the client.println() function. 

WiFiClient client = server.available();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");  // the connection will be closed after completion of the response
client.println("Refresh: 10");  // update the page after 10 sec

 

To decorate the page, use CSS to style the text and background. You can change the properties in the below lines to change the color, font, and other styles.

client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<style>html { font-family: Fantasy; display: block; margin: 0px auto; text-align: center;color: #333333; background-color: #ccffb3;}");

 

Then create the division using <div> tag for each parameter i.e. Pressure, Temperature, Humidity, Altitude, and Luminosity. Also, define different properties for these parameters.

..
..
client.println("<div class=\"side_adjust text1\">Humidity:</div>");
client.println("<div class=\"side_adjust data1\">");
client.print(h);
client.println("<div class=\"side_adjust text1\">%</div>");
client.println("</div>");

 

Similarly, make the division class for other parameters and close the HTML and body tag.

client.println("</body>");
client.println("</html>");

Complete code with a Demo video can be found at the end of this tutorial.

 

Testing the ESP32 Weather Station

Now, connect the ESP32 with the laptop and choose the board and port correctly and then hit the Upload button. Make sure your laptop or Smartphone share the same Wi-Fi network as the ESP32.

After uploading the code, open the serial monitor. Make the baud rate of serial monitor as 115200. You will see the IP address in the monitor, just copy this IP and paste it in the browser.

IoT Weather Station IP Address

So open this IP address in the web browser by copy-pasting the IP address in the address bar, and you will see a webpage showing all the weather data like Temperature, Humidity, Pressure, Altitude, and Luminosity. This page will be refreshed automatically after every 10 seconds.

ESP32 Weather Station

So, this is how you can easily build a complete weather station using ESP32, DHT11 and BMP180 sensor. This data can be monitored from anywhere in the world by enabling the port forwarding in your router settings.

Code

#include <WiFi.h>
#include "DHT.h"
#include <Wire.h>
#include <Adafruit_BMP085.h>
#define DHTPIN 13     
#define DHTTYPE DHT11  
Adafruit_BMP085 bmp;
DHT dht(DHTPIN, DHTTYPE);
const char* ssid     = "*******"; // Your ssid
const char* password = "********"; // Your Password
char status;
WiFiServer server(80);
void setup() {
  Serial.begin(115200);
 delay(100);
  dht.begin();
   if (!bmp.begin()) {
 Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  while (1) {}
  }
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi is connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}
void loop() { 
int ldrValue = analogRead(12); // read analog input pin 0
Serial.println(ldrValue, DEC); // prints the value read
 float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
 float p = bmp.readPressure()/100;
   Serial.print("Pressure = ");
   Serial.print(p);
   Serial.println(" mb");
  // Calculate altitude assuming 'standard' barometric
  // pressure of 1013.25 millibar = 101325 Pascal
  float a = bmp.readAltitude();
  Serial.print("Altitude = ");
  Serial.print(a);
  Serial.println(" meters");
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  WiFiClient client = server.available();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");  // the connection will be closed after completion of the response
client.println("Refresh: 10");  // update the page after 10 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<style>html { font-family: Fantasy; display: block; margin: 0px auto; text-align: center;color: #333333; background-color: #ccffb3;}");
client.println("body{margin-top: 50px;}");
client.println("h1 {margin: 50px auto 30px; font-size: 50px; text-align: center;}");
client.println(".side_adjust{display: inline-block;vertical-align: middle;position: relative;}");
client.println(".text1{font-weight: 180; padding-left: 15px; font-size: 50px; width: 170px; text-align: left; color: #3498db;}");
client.println(".data1{font-weight: 180; padding-left: 80px; font-size: 50px;color: #3498db;}");
client.println(".text2{font-weight: 180; font-size: 50px; width: 170px; text-align: left; color: #3498db;}");
client.println(".data2{font-weight: 180; padding-left: 150px; font-size: 50px;color: #3498db;}");
client.println(".data{padding: 10px;}");
client.println("</style>");
client.println("</head>");
client.println("<body>");
client.println("<div id=\"webpage\">");   
client.println("<h1>ESP32 Weather Station</h1>");
client.println("<div class=\"data\">");
client.println("<div class=\"side_adjust text1\">Humidity:</div>");
client.println("<div class=\"side_adjust data1\">");
client.print(h);
client.println("<div class=\"side_adjust text1\">%</div>");
client.println("</div>");
client.println("<div class=\"data\">");
client.println("<div class=\"side_adjust text2\">Temperature:</div>");
client.println("<div class=\"side_adjust data2\">");
client.print(t);
client.println("<div class=\"side_adjust text2\">*C</div>");
client.print(f);
client.println("<div class=\"side_adjust text2\">F</div>");
client.println("</div>");
client.println("<div class=\"data\">");
client.println("<div class=\"side_adjust text1\">Pressure:</div>");
client.println("<div class=\"side_adjust data1\">");
client.print(p);
client.println("<div class=\"side_adjust text1\">mb</div>");
client.println("</div>");
client.println("<div class=\"data\">");
client.println("<div class=\"side_adjust text1\">Altitude:</div>");
client.println("<div class=\"side_adjust data1\">");
client.print(a);
client.println("<div class=\"side_adjust text1\">m</div>");
client.println("</div>");
client.println("<div class=\"data\">");
client.println("<div class=\"side_adjust text2\">Luminiousity:</div>");
client.println("<div class=\"side_adjust data2\">");
client.print(ldrValue);
client.println("</div>");
client.println("</div>");
client.println("</body>");
client.println("</html>

Video

16 Comments

I really wanted to jot down a simple remark to be able to appreciate you for those unique information you are placing at this site. My time consuming internet search has at the end been recognized with sensible know-how to go over with my pals. I would repeat that many of us site visitors actually are extremely endowed to live in a perfect network with very many lovely professionals with very beneficial things. I feel extremely lucky to have discovered your entire webpage and look forward to really more excellent moments reading here. Thank you again for a lot of things.

I wanted to send you one little note to help thank you yet again with your amazing views you've provided on this page. This is quite remarkably generous of you to give openly all a number of people might have supplied as an e-book to make some money for their own end, primarily given that you might have done it if you ever considered necessary. These pointers also served to provide a good way to fully grasp most people have the same dream similar to my personal own to know the truth a little more on the subject of this problem. Certainly there are millions of more pleasant moments ahead for people who find out your website.

I wish to express appreciation to this writer just for rescuing me from this situation. After checking throughout the world wide web and obtaining strategies which were not helpful, I figured my entire life was well over. Being alive minus the strategies to the problems you've solved all through your good report is a critical case, and those which could have negatively damaged my career if I hadn't noticed your web page. Your main natural talent and kindness in dealing with almost everything was priceless. I'm not sure what I would have done if I had not encountered such a solution like this. I can at this time relish my future. Thanks so much for this professional and effective help. I won't hesitate to refer the website to anyone who should receive guidance on this issue.

I actually wanted to type a simple comment to be able to express gratitude to you for those great tips you are giving at this website. My considerable internet investigation has now been compensated with sensible facts and techniques to go over with my family members. I would point out that most of us readers actually are definitely blessed to live in a really good community with so many lovely individuals with very helpful points. I feel very much lucky to have come across your webpages and look forward to plenty of more excellent moments reading here. Thanks once more for everything.

I in addition to my buddies have been examining the nice advice from your site and then then got a terrible feeling I had not thanked the blog owner for those tips. Those boys happened to be totally happy to learn all of them and have now clearly been using these things. I appreciate you for really being well thoughtful and also for obtaining such beneficial issues millions of individuals are really needing to know about. Our own sincere regret for not expressing appreciation to you sooner.

I as well as my guys appeared to be taking note of the best strategies located on your web blog and all of a sudden got a horrible suspicion I had not thanked the web blog owner for those strategies. My ladies happened to be for that reason thrilled to see all of them and now have seriously been taking advantage of them. Many thanks for actually being simply accommodating and for settling on varieties of remarkable useful guides most people are really wanting to know about. Our own honest apologies for not expressing gratitude to sooner.

I have to express appreciation to the writer for bailing me out of this particular circumstance. Right after browsing through the world-wide-web and obtaining advice that were not productive, I assumed my entire life was well over. Living without the presence of solutions to the problems you have solved as a result of your posting is a critical case, and those that might have in a wrong way damaged my entire career if I had not discovered the website. Your own personal expertise and kindness in touching the whole lot was useful. I am not sure what I would've done if I had not come upon such a subject like this. I can at this time look forward to my future. Thanks for your time very much for this reliable and amazing guide. I will not think twice to refer your site to any person who should have care about this subject matter.

Needed to send you the little word to say thank you yet again considering the breathtaking techniques you've provided on this website. It was open-handed of you to make freely all that a number of us would've offered as an e-book to end up making some money on their own, most notably now that you could possibly have done it in the event you decided. The tips additionally acted to become a good way to be aware that the rest have a similar dream just as my very own to understand a good deal more with respect to this issue. I believe there are a lot more fun occasions up front for individuals who read your blog.

I enjoy you because of all your hard work on this web site. My aunt take interest in managing investigation and it is easy to understand why. A number of us know all relating to the lively way you render rewarding guides through the web site and even foster response from people about this concern and my child is certainly being taught a whole lot. Take advantage of the remaining portion of the year. You are conducting a very good job.

I wanted to compose you this little bit of observation so as to thank you the moment again on the beautiful pointers you have contributed above. This has been open-handed of people like you to supply extensively just what most of us would've advertised for an e book to make some cash for their own end, precisely seeing that you could possibly have done it if you desired. The points as well worked to provide a good way to know that other individuals have the same dreams the same as my personal own to know the truth a whole lot more concerning this matter. I know there are several more fun periods in the future for those who browse through your blog post.

I truly wanted to post a word to be able to appreciate you for some of the unique tips and tricks you are giving at this site. My prolonged internet research has at the end been rewarded with reliable suggestions to share with my great friends. I would mention that most of us readers are truly lucky to be in a great website with very many awesome people with very beneficial ideas. I feel pretty lucky to have seen the website page and look forward to so many more awesome times reading here. Thanks a lot once again for everything.

I intended to create you one very little word to finally thank you again with the exceptional information you have provided on this page. It has been so wonderfully open-handed of people like you to make easily all some people would have sold as an e-book to generate some profit on their own, certainly considering the fact that you might well have tried it if you ever desired. The solutions also worked as the easy way to understand that the rest have the same keenness just like my personal own to see more and more on the subject of this condition. I'm sure there are several more enjoyable sessions in the future for individuals that scan your blog.

A lot of thanks for all your efforts on this web page. Kim really loves going through research and it's really simple to grasp why. Almost all hear all about the dynamic manner you give functional tricks by means of your web blog and therefore recommend contribution from website visitors on this issue while our own child is learning a whole lot. Have fun with the rest of the year. You are doing a remarkable job.

My wife and i ended up being satisfied Peter managed to finish off his web research while using the ideas he received from your web page. It's not at all simplistic to just always be freely giving secrets which the rest might have been making money from. Therefore we see we have the writer to thank for that. All the explanations you have made, the simple web site navigation, the relationships your site assist to create - it is many superb, and it is facilitating our son in addition to our family consider that this concept is satisfying, which is especially vital. Thanks for all!

A lot of thanks for each of your labor on this site. Debby enjoys conducting internet research and it's really easy to see why. A number of us hear all concerning the lively mode you provide vital tricks on the website and boost participation from people about this point so our own child is certainly becoming educated a lot of things. Take advantage of the remaining portion of the new year. You are carrying out a wonderful job.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.