ESP32 Weather Station With BM280
ESP32 Weather Station With BM280
ESP32 Weather Station With BM280
With BME280
Don’t let the smartphone weather apps or commercial weather stations(that feeds you with data
from stations based miles away) ruin your outdoor plans. With this IoT project you can be your
own weatherman!
This project uses ESP32 as the control device that easily connects to existing WiFi network &
creates a Web Server. When any connected device accesses this web server, ESP32 reads in
temperature, humidity, barometric pressure & altitude from BME280 & sends it to the web
browser of that device with a nice interface. Excited? Let’s get started!
BME280 is the next-generation digital temperature, humidity and pressure sensor manufactured
by Bosch. It’s a successor to sensors like BMP180, BMP085 or BMP183.
The operating voltage of the BME280 module is from 3.3V to 5V – Perfect for interfacing with
3.3V microcontrollers like ESP32.
The module features a simple two-wire I2C interface for communication. The default I2C
address of the BME280 module is 0x76 and can be changed to 0x77 easily with this procedure.
Next, Connect the SCL pin to the I2C clock D22 pin on your ESP32 and connect the SDA pin to the
I2C data D21 pin on your ESP32.
To install the library navigate to the Arduino IDE > Sketch > Include Library > Manage
Libraries… Wait for Library Manager to download libraries index and update list of installed
libraries.
Learn more
Filter your search by typing ‘bme280’. There should be a couple entries. Look for Adafruit
BME280 Library by Adafruit. Click on that entry, and then select Install.
The BME280 sensor library uses the Adafruit Sensor support backend. So, search the library
manager for Adafruit Uni ed Sensor and install that too (you may have to scroll a bit)
Displaying Temperature, Humidity, Pressure & Altitude On
ESP32 Web Server
Now, we are going to con gure our ESP32 into Station (STA) mode, and create a web server to
serve up web pages to any connected client under existing network.
Learn more
If you want to learn about creating a web server with ESP32 in AP/STA mode, check this tutorial
out.
Before you head for uploading the sketch, you need to make one change to make it work for you.
You need to modify the following two variables with your network credentials, so that ESP32 can
establish a connection with existing network.
Once you are done, go ahead and try the sketch out.
#include <WiFi.h>
#include <WebServer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme;
WebServer server(80);
void setup() {
Serial.begin(115200);
delay(100);
bme.begin(0x76);
Serial.println("Connecting to ");
Serial println(ssid);
Next, load up a browser and point it to the IP address shown on the serial monitor. The ESP32
should serve up a web page showing temperature, humidity, pressure and altitude from BME280.
Detailed Code Explanation
The sketch starts by including following libraries.
WiFi.h library provides ESP32 speci c WiFi methods we are calling to connect to network.
WebServer.h library has some methods available that will help us setting up a server and
handle incoming HTTP requests without needing to worry about low level implementation
details.
Wire.h library communicates with any I2C device not just BME280.
#include <WiFi.h>
#include <WebServer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Next we create an object of the sensor and variables to store temperature, humidity, pressure
and altitude.
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme;
As we are con guring ESP32 in Station (STA) mode, it will join existing WiFi network. Hence, we
need to provide it with your network’s SSID & Password. Next we start web server at port 80.
WebServer server(80);
First of all, we initialize serial communication with PC and initialize BME object using begin()
function. It initializes I2C interface with given I2C Address(0x76) and checks if the chip ID is
correct. It then resets the chip using soft-reset & waits for the sensor for calibration after wake-
up.
Serial.begin(115200);
delay(100);
bme.begin(0x76);
Now, we need to join the WiFi network using WiFi.begin() function. The function takes SSID
(Network Name) and password as a parameter.
Serial.println("Connecting to ");
Serial.println(ssid);
While the ESP32 tries to connect to the network, we can check the connectivity status with
WiFi.status() function.
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Once the ESP32 is connected to the network, the sketch prints the IP address assigned to ESP32
by displaying WiFi.localIP() value on serial monitor.
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
In order to handle incoming HTTP requests, we need to specify which code to execute when a
URL is hit. To do so, we use on method. This method takes two parameters. First one is a URL
path and second one is the name of function which we want to execute when that URL is hit.
The code below indicates that when a server receives an HTTP request on the root (/) path, it will
trigger the handle_OnConnect function. Note that the URL speci ed is a relative path.
server.on("/", handle_OnConnect);
We haven’t speci ed what the server should do if the client requests any URL other than speci ed
with server.on . It should respond with an HTTP status 404 (Not Found) and a message for
the user. We put this in a function as well, and use server.onNotFound to tell it that it should
execute it when it receives a request for a URL that wasn’t speci ed with server.on
server.onNotFound(handle_NotFound);
Now, to start our server, we call the begin method on the server object.
server.begin();
Serial.println("HTTP server started");
Next, we need to create a function we attached to root (/) URL with server.on Remember?
At the start of this function, we get the temperature, humidity, pressure & altitude readings from
the sensor. In order to respond to the HTTP request, we use the send method. Although the
method can be called with a different set of arguments, its simplest form consists of the HTTP
response code, the content type and the content.
In our case, we are sending the code 200 (one of the HTTP status codes), which corresponds to
the OK response. Then, we are specifying the content type as “text/html“, and nally we are
calling SendHTML() custom function which creates a dynamic HTML page containing
temperature, humidity, pressure & altitude readings.
void handle_OnConnect() {
temperature = bme.readTemperature();
humidity = bme.readHumidity();
pressure = bme.readPressure() / 100.0F;
altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
server.send(200, "text/html",
SendHTML(temperature,humidity,pressure,altitude));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
Next, the <meta> viewport element makes the web page responsive in any web browser, while
title tag sets the title of the page.
Following code sets color, font and margin around the body, H1 and p tags.
If you try to compare this function with the previous one, you’ll come to know that they are similar
except these changes.
We have used Google commissioned Open Sans web font for our web page. Note that you
cannot see Google font, without active internet connection on the device. Google fonts are
loaded on the y.
The icons used to display temperature, humidity, pressure & altitude readings are actually
a Scalable Vector Graphics (SVG) de ned in <svg> tag. Creating SVG doesn’t require any
special programming skills. You can use Google SVG Editor for creating graphics for your
page. We have used these SVG icons.
With the addition of a single meta tag into your HTML document, you can instruct the browser to
automatically reload the page at a provided interval.
Place this code in the the <head> tag of your document, this meta tag will instruct the browser to
refresh every two seconds. Pretty nifty!
Here is the AJAX script that we’ll be using. Place this script just before you close </head> tag.
ptr +="<script>\n";
ptr +="setInterval(loadDoc,1000);\n";
ptr +="function loadDoc() {\n";
ptr +="var xhttp = new XMLHttpRequest();\n";
ptr +="xhttp.onreadystatechange = function() {\n";
ptr +="if (this.readyState == 4 && this.status == 200) {\n";
ptr +="document.body.innerHTML =this.responseText}\n";
ptr +="};\n";
ptr +="xhttp.open(\"GET\", \"/\", true);\n";
ptr +="xhttp.send();\n";
ptr +="}\n";
ptr +="</script>\n";
The script starts with <script> tag. As AJAX script is nothing but a javascript, we need to write it
in <script> tag. In order for this function to be repeatedly called, we will be using the javascript
setInterval() function. It takes two parameters – a function to be executed and time interval
(in milliseconds) on how often to execute the function.
ptr +="<script>\n";
ptr +="setInterval(loadDoc,1000);\n";
The heart of this script is a loadDoc() function. Inside this function, an XMLHttpRequest()
object is created. This object is used to request data from a web server.
The xhttp.onreadystatechange() function is called every time the readyState changes. The
readyState property holds the status of the XMLHttpRequest. It has one of the following values.
2: request received
3: processing request
The status property holds the status of the XMLHttpRequest object. It has one of the following
values.
200: “OK”
403: “Forbidden”
When readyState is 4 and status is 200, the response is ready. Now, the content of body (holding
temperature readings) is updated.
The HTTP request is then initiated via the open() and send() functions.
Share