Adding a BMP280 to The Weather Station.

The BMP280 is a great addition to the weather station. This relatively cheap module gives us the capability to measure atmospheric pressure. The BMP280 also has a temperature and humidity sensor built in. The AM2302 used in our original project can be used as an inside temperature sensor.

Advertisements

The most common used library for the BMP280 is the Adafruit library. If you are using the D1 Mini Pro, the Adafruit library will not work. I found an example on a website that does not use a library. The example sketch worked well. The commands for communicating with the BMP280 were used in the weather station.

To connect the BMP280, simply connect it to your I2C bus. 3.3v is used to power the module.

Advertisements

Weather Station Using BMP280 Sketch

//Basic Weather Station Using D1 Mini pro 10/13/19
//Real Time Clock added 10/26/19
//https://KM4NMP.com/
//For the I2C LCD library used and installation instructions use link below
//https://randomnerdtutorials.com/esp32-esp8266-i2c-lcd-arduino-ide/
//Real Time Clock Library used is RTClib by Adafruit. to install search in
//the Library Manager and click install
//BMP280 portion of Sketch was sourced from http://www.esp8266learning.com/esp8266-and-bme280-temperature-sensor-example.php

#include<Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>

#define Addr 0x76

RTC_DS3231 rtc;

int lcdColumns = 16;
int lcdRows = 2;
bool state=0;
unsigned long previousMillis = 0;
const long interval = 2000;


LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);



void setup() {
  

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif

Wire.begin();
Serial.begin(9600);
  
  
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);  
  lcd.print("   KM4NMP.com");
  delay(3000);

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }


    //SET DATE AND TIME TO COMPILE TIME
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    //rtc.adjust(DateTime(2019, 10, 26, 3, 0, 0));
  
    DateTime now = rtc.now();
    lcd.setCursor(11, 0);
    if (now.hour() >= 10 ) {      //check hours if 10 or above and print hours
      lcd.print(now.hour(), DEC);
      lcd.print(':');
    }
    
    if (now.hour() <= 9) {        //check hours if 9 or below
      lcd.print("0");             //if needed 0 is printed before hours
      lcd.print(now.hour(), DEC);
      lcd.print(':');
    }

    if (now.minute() >=10) {          //checks if minutes are 10 or above 
      lcd.print(now.minute(), DEC);
    }

     if (now.minute() <=9) {         //checks if minutes are 9 or below
      lcd.print("0");                //if needed prints 0 before minutes
      lcd.print(now.minute(), DEC);
    }

}
void loop() {
    unsigned long currentMillis = millis();
    DateTime now = rtc.now();

    unsigned int b1[24];
    unsigned int data[8];
    unsigned int dig_H1 = 0;
  
    lcd.setCursor(11, 0);
    if (now.hour() >= 10 ) {
      lcd.print(now.hour(), DEC);
      lcd.print(':');
    }
    
    if (now.hour() <= 9) {
      lcd.print("0");
      lcd.print(now.hour(), DEC);
      lcd.print(':');
    }

Advertisements
 if (now.minute() >=10) {
      lcd.print(now.minute(), DEC);
    }

     if (now.minute() <=9) {
      lcd.print("0");
      lcd.print(now.minute(), DEC);
    }
  
  
  if (currentMillis - previousMillis >= interval){ //checks time elapsed since last read 
    for(int i = 0; i < 24; i++)
    {
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select data register
    Wire.write((136+i));
    // Stop I2C Transmission
    Wire.endTransmission();
 
    // Request 1 byte of data
    Wire.requestFrom(Addr, 1);
 
    // Read 24 bytes of data
    if(Wire.available() == 1)
    {
    b1[i] = Wire.read();
    }
    }
 
    // Convert the data
    // temp coefficients
    unsigned int dig_T1 = (b1[0] & 0xff) + ((b1[1] & 0xff) * 256);
    int dig_T2 = b1[2] + (b1[3] * 256);
    int dig_T3 = b1[4] + (b1[5] * 256);
 
    // pressure coefficients
    unsigned int dig_P1 = (b1[6] & 0xff) + ((b1[7] & 0xff ) * 256);
    int dig_P2 = b1[8] + (b1[9] * 256);
    int dig_P3 = b1[10] + (b1[11] * 256);
    int dig_P4 = b1[12] + (b1[13] * 256);
    int dig_P5 = b1[14] + (b1[15] * 256);
    int dig_P6 = b1[16] + (b1[17] * 256);
    int dig_P7 = b1[18] + (b1[19] * 256);
    int dig_P8 = b1[20] + (b1[21] * 256);
    int dig_P9 = b1[22] + (b1[23] * 256);
 
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select data register
    Wire.write(161);
    // Stop I2C Transmission
    Wire.endTransmission();
 
    // Request 1 byte of data
    Wire.requestFrom(Addr, 1);
 
    // Read 1 byte of data
    if(Wire.available() == 1)
    {
    dig_H1 = Wire.read();
    }
 
    for(int i = 0; i < 7; i++)
    {
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select data register
    Wire.write((225+i));
    // Stop I2C Transmission
    Wire.endTransmission();
 
    // Request 1 byte of data
    Wire.requestFrom(Addr, 1);
 
    // Read 7 bytes of data
    if(Wire.available() == 1)
    {
    b1[i] = Wire.read();
    }
    }
 
    // Convert the data
    // humidity coefficients
    int dig_H2 = b1[0] + (b1[1] * 256);
    unsigned int dig_H3 = b1[2] & 0xFF ;
    int dig_H4 = (b1[3] * 16) + (b1[4] & 0xF);
    int dig_H5 = (b1[4] / 16) + (b1[5] * 16);
    int dig_H6 = b1[6];
 
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select control humidity register
    Wire.write(0xF2);
    // Humidity over sampling rate = 1
    Wire.write(0x01);
    // Stop I2C Transmission
    Wire.endTransmission();
Advertisements
 // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select control measurement register
    Wire.write(0xF4);
    // Normal mode, temp and pressure over sampling rate = 1
    Wire.write(0x27);
    // Stop I2C Transmission
    Wire.endTransmission();
 
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select config register
    Wire.write(0xF5);
    // Stand_by time = 1000ms
    Wire.write(0xA0);
    // Stop I2C Transmission
    Wire.endTransmission();
 
    for(int i = 0; i < 8; i++)
    {
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select data register
    Wire.write((247+i));
    // Stop I2C Transmission
    Wire.endTransmission();
 
    // Request 1 byte of data
    Wire.requestFrom(Addr, 1);
 
    // Read 8 bytes of data
    if(Wire.available() == 1)
    {
    data[i] = Wire.read();
    }
    }
 
    // Convert pressure and temperature data to 19-bits
    long adc_p = (((long)(data[0] & 0xFF) * 65536) + ((long)(data[1] & 0xFF) * 256) + (long)(data[2] & 0xF0)) / 16;
    long adc_t = (((long)(data[3] & 0xFF) * 65536) + ((long)(data[4] & 0xFF) * 256) + (long)(data[5] & 0xF0)) / 16;
    // Convert the humidity data
    long adc_h = ((long)(data[6] & 0xFF) * 256 + (long)(data[7] & 0xFF));
 
    // Temperature offset calculations
    double var1 = (((double)adc_t) / 16384.0 - ((double)dig_T1) / 1024.0) * ((double)dig_T2);
    double var2 = ((((double)adc_t) / 131072.0 - ((double)dig_T1) / 8192.0) *
    (((double)adc_t)/131072.0 - ((double)dig_T1)/8192.0)) * ((double)dig_T3);
    double t_fine = (long)(var1 + var2);
    double cTemp = (var1 + var2) / 5120.0;
    double fTemp = cTemp * 1.8 + 32;
 
    // Pressure offset calculations
    var1 = ((double)t_fine / 2.0) - 64000.0;
    var2 = var1 * var1 * ((double)dig_P6) / 32768.0;
    var2 = var2 + var1 * ((double)dig_P5) * 2.0;
    var2 = (var2 / 4.0) + (((double)dig_P4) * 65536.0);
    var1 = (((double) dig_P3) * var1 * var1 / 524288.0 + ((double) dig_P2) * var1) / 524288.0;
    var1 = (1.0 + var1 / 32768.0) * ((double)dig_P1);
    double p = 1048576.0 - (double)adc_p;
    p = (p - (var2 / 4096.0)) * 6250.0 / var1;
    var1 = ((double) dig_P9) * p * p / 2147483648.0;
    var2 = p * ((double) dig_P8) / 32768.0;
    double pressure = (p + (var1 + var2 + ((double)dig_P7)) / 16.0) / 100;
 
    // Humidity offset calculations
    double var_H = (((double)t_fine) - 76800.0);
    var_H = (adc_h - (dig_H4 * 64.0 + dig_H5 / 16384.0 * var_H)) * (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * var_H * (1.0 + dig_H3 / 67108864.0 * var_H)));
    double humidity = var_H * (1.0 - dig_H1 * var_H / 524288.0);
    if(humidity > 100.0)
    {
    humidity = 100.0;
    }
    else if(humidity < 0.0)
    {
    humidity = 0.0;
    }
    double inchMerc = pressure * 0.02953;
    
    // Output data to serial monitor
    Serial.print("Temperature in Celsius : ");
    Serial.print(cTemp);
    Serial.println(" C");
    Serial.print("Temperature in Fahrenheit : ");
    Serial.print(fTemp);
    Serial.println(" F");
    Serial.print("Pressure : ");
    Serial.print(pressure);
    Serial.println(" hPa");
    Serial.print(inchMerc);
    Serial.println("inhg");
    Serial.print("Relative Humidity : ");
    Serial.print(humidity);
    Serial.println(" RH");
    lcd.setCursor(0, 0);
    lcd.print(inchMerc);
    lcd.print("inHg  ");
    lcd.setCursor(0, 1);
    lcd.print(fTemp);                              //prints temp
    lcd.print((char)223);                          //charecter for derees
    lcd.print("F   ");
    lcd.print(humidity);                           //prints humidity
    lcd.print((char)37);                           //speacial charecter for%
    previousMillis = currentMillis;                // resets time of last read
  }
    
  
}
Advertisements
Advertisements

Links

http://www.esp8266learning.com/esp8266-and-bme280-temperature-sensor-example.php

https://km4nmp.com/2019/10/26/adding-a-real-time-clock-to-the-basic-weather-station/

https://km4nmp.com/2019/10/13/basic-weather-station-using-a-d1-mini-pro/

https://km4nmp.com/2019/05/11/lm7805-regulated-5-volts/

Advertisements

Latest Posts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close