This is an old revision of the document!
Author: Bukkosi George-Daniel
This project is meant to show the implementation of a simple Weather Station that outputs the data to a local webserver created using the Arduino <WiFi.h> library.
The components used for the project were the following:
To power up the ESP32 and to load the code to it, a microUSB cable was required.
The ESP32 development board and the BMP280 sensor were connected via the I2C protocol. In order to establish this, the following wirings were made:
A schematic of the connections can be observed below:
The following libraries were used for the software:
#include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BMP280.h> #include <WiFi.h>
In order to interface with the BMP280 sensor, the following addresses were used in the setup() function:
#define BMP280_ADDRESS (0x76) #define BMP280_CHIPID (0x58)
The Adafruit library allows us to read data from the BMP280 sensor. Its API is quite simple, here is the code for reading the temperature, pressure and altitude:
float temperature_celsius = bmp.readTemperature(); float temperature_fahrenheit = bmp.readTemperature() + 32; float pressure = bmp.readPressure(); float altitude = bmp.readAltitude(SEALEVELPRESSURE_HPA);
where
#define SEALEVELPRESSURE_HPA (1013.25)
In the setup() function, we have the WiFi connection logic and the BMP280 connection logic:
void setup() {
Serial.begin(115200);
delay(2000);
if (!bmp.begin(BMP280_ADDRESS, BMP280_CHIPID)) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
Serial.println("BMP280 sensor initialized");
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 connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
The loop() function has the following functionalities implemented:
Code below:
void loop() {
float temperature_celsius = bmp.readTemperature();
float temperature_fahrenheit = bmp.readTemperature() + 32;
float pressure = bmp.readPressure() / 100.0F;
float altitude = bmp.readAltitude(SEALEVELPRESSURE_HPA);
WiFiClient client = server.available();
if (client) {
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client.");
String currentLine = "";
while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
if (client.available()) {
char c = client.read();
Serial.write(c);
header += c;
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}");
client.println("table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }");
client.println("th { padding: 12px; background-color: #0043af; color: white; }");
client.println("tr { border: 1px solid #ddd; padding: 12px; }");
client.println("tr:hover { background-color: #bcbcbc; }");
client.println("td { border: none; padding: 12px; }");
client.println(".sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }");
client.println("</style></head><body><h1>Weather station with BME280</h1>");
client.println("<table><tr><th>MEASUREMENT</th><th>VALUE</th></tr>");
client.println("<tr><td>Temp. Celsius</td><td><span class=\"sensor\">");
client.println(temperature_celsius);
client.println(" *C</span></td></tr>");
client.println("<tr><td>Temp. Fahrenheit</td><td><span class=\"sensor\">");
client.println(temperature_fahrenheit);
client.println(" *F</span></td></tr>");
client.println("<tr><td>Pressure</td><td><span class=\"sensor\">");
client.println(pressure);
client.println(" hPa</span></td></tr>");
client.println("<tr><td>Approx. Altitude</td><td><span class=\"sensor\">");
client.println(altitude);
client.println(" m</span></td></tr>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
By simply accessing the local IP address printed in the Serial Monitor of the Arduino IDE you can see all the sensor gathered data in one place.