Weather Station with BMP280

Author: Bukkosi George-Daniel

Introduction

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.

Hardware

The components used for the project were the following:

  • ESP32 Dev Board
  • BMP280 weather sensor
  • Jumper wires
  • Breadboard
  • Soldering kit

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:

  1. ESP32 GND pin - BMP280 GND pin
  2. ESP32 3v3 pin - BMP280 VCC pin
  3. ESP32 GPIO22 pin (SCL) - BMP280 SCL pin
  4. ESP32 GPIO21 pin (SDA) - BMP280 SDA pin

One issue that was faced here was with the first sensor that I ordered and came in broken. After multiple tries to connect it on both the 0x76 and 0x77 address and also on other GPIO pins, I drew the conclusion that it was broken. The new sensor connected immediately without any issues.

A schematic of the connections can be observed below:

Software

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:

  • Read sensor data: The function starts by reading sensor data using the BMP sensor. It retrieves values for temperature in Celsius and Fahrenheit, pressure, and altitude based on the sea level pressure.
  • Check for incoming client connections: The code then checks if there's a new client connection by calling server.available(). If a client has connected, it proceeds to handle the client request.
  • Handle client request: Once a client is detected, the code enters a loop to handle the client's request. It reads the incoming data from the client and stores it in the currentLine string. The loop continues as long as the client remains connected and the elapsed time since the client connected is within the specified timeout limit.
  • Generate and send HTTP response: If the client's request is complete (indicated by an empty currentLine), the code generates an HTTP response. It sends the appropriate headers, including a response code of 200 OK and the content type as text/html. It then proceeds to send the HTML content that forms the web page.
  • Close the client connection: After sending the response, the client connection is closed using client.stop(). The code prints a message indicating that the client has disconnected.
  • Repeat the loop: The loop() function then repeats from the beginning, continuously checking for new client connections and handling their requests.

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.

References

iothings/proiecte/2022sric/weather-station.txt · Last modified: 2023/05/29 22:21 by george.bukkosi
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0