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()) {
** wall of text for HTML page construction **
}
}
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.