Table of Contents

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:

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:

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