Hi there to you all,
Just completed my Arduino Nano weather station with (limited) weather analysis. It shows the usual 'suspects': temperature, humidity and air pressure on a 20x4 LCD. The bottom line of the LCD tells you whether air pressure is rising or falling, whether the pressure values are normal, or if we are dealing with low or high pressure conditions. The OLED's are used to display time, pressure values over the past 4 hours and what the tendency is: falling, rising or a rapid fall of pressure, which is an indication for deterioration of weather conditions and strong winds.
I guess the biggest challenge was to get it all hooked up on the multiplexer and get the data presented on the two OLED's. Using Adafruits standard library, simply didn't work. The script would compile without errors, but the Nano froze every time I tried to execute the program. Learning by doing.....I installed the U8x8 library which uses far less SRAM, and suddenly it worked. Glad I have the Arduino Cookbook and some help from our AI friends :-). In addition to this less consuming library, I also used 'freeram' to check for the amount of SRAM left to run the program.
Anyway, it's working and fun to look at. I'll leave it in this breadboard version. The next version is already on my mind: a large TFT screen to display text, figures and graphics, an additional outdoor sensor and a bit more 'prediction intelligence'! Got myself an Arduino Avery, so that should solve the SRAM problem!
That's all for now.
Met grote groet,
Mike
/*
Project weather station version 3.0 with:
- TCA 9548A I2C multiplexer,
- BME280 Temperature, Humidity and Pressure sensor on bus 0,
- DS3231 Real Time Clock on bus 1,
- OLED SSD1306 0,96" display on bus 2 and 3,
- LCD 20x4 display normal wiring.
This weather stations saves data every hour for a period of three hours,
as long as the power is on. Changing weather conditions based on these
measurements are presented on OLED2. The TCA9548A is used to connect
the sensor, clock and two OLEDS on the I2C bus.
*/
// Setup LCD 20x4 display------------------------------
#include<LiquidCrystal.h>
int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//--------------------------------------------------------------
// Setup TCA 9548A I2C multiplexer (I2C address 0x70)-----------
#include <Wire.h>
void TCASELECT(uint8_t bus) {
Wire.beginTransmission(0x70);
Wire.write(1 << bus);
Wire.endTransmission();
}
//--------------------------------------------------------------
// Setup BME280 Sensor (I2C address 0x76)-----------------------
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme;
float temperature;
float humidity;
float pressure;
const byte degreeSymbol = 0xDF;
/*
The following variables will be used for weather trend analysis.
A pressure drop of 3 hPa in less than three hours is a strong
indication for storm and or heavy rain.
*/
float pressureNow;
float pressureMin1;
float pressureMin2;
float pressureMin3;
//Weather analysis messages to be presented on the LCD
String msg0 = "Temperature";
String msg1 = "Humidity";
String msg2 = "Pressure";
String msg3 = "Normal air pressure";
String msg4 = "Slightly high";
String msg5 = "High pressure area";
String msg6 = "Slightly low";
String msg7 = "Low pressure area";
String msg8 = "Warning gale or rain";
//--------------------------------------------------------------
//Setup DS3231 Real Time Clock----------------------------------
#include <DS3231.h>
DS3231 clock;
RTCDateTime dt;
int hour;
int minute;
int second;
int day;
int month;
int year;
//The following variables are used for weather trend analysis.
int hourNow;
int hourMin1;
int hourMin2;
int hourMin3;
//Measurement intervals
int secondOld=0;//Time update on OLED 1 once a second.
int minuteOld=0;//Sensor and LCD update once a minute.
int hourOld=0;//Pressure updates on OLED 2 once every hour.
//--------------------------------------------------------------
/*
Setup OLED with U8x8lib.h library.
Don't use Adafruit.h library on Arduino Uno and Nano with two OLEDs.
Allthough program compilation shows no problems, the system will
freeze due to SRAM overflow.
*/
#include <U8x8lib.h>
U8X8_SSD1306_128X64_NONAME_HW_I2C oled(U8X8_PIN_NONE);
//--------------------------------------------------------------
//Variables-----------------------------------------------------
int baudrate = 9600;
int delayMedium=1000;
//Startconditions
int start=1;
int errorBme=0;
int errorClock=0;
//--------------------------------------------------------------
//--------------------------------------------------------------
void setup()
{
Serial.begin(baudrate);
// Wait maximum of 3 seconds
unsigned long startMillis = millis();
while (!Serial && (millis() - startMillis < 3000));
Serial.println("Powering up weatherstation");
// Start LCD display------------------------------------------
lcd.clear();
lcd.begin(20, 4);
lcd.setCursor(0, 0);
lcd.print("Powering up:");
delay(delayMedium);
lcd.setCursor(0, 1);
lcd.print("Checking devices....");
delay(delayMedium);
lcd.clear();
// Start I2C bus----------------------------------------------
Wire.begin();
// Start BME280 on bus 0--------------------------------------
TCASELECT(0);
if (!bme.begin(0x76))
{
errorBme=1;
Serial.println("Error, BME280 not connected");
lcd.setCursor(0, 0);
lcd.print("BME 280 not connected");
delay(delayMedium);
}
else
{
Serial.println("BME280 connected.");
lcd.setCursor(0, 0);
lcd.print("BME280: connected");
delay(delayMedium);
}
// Start DS3231 RTC on bus 1 (I2C address 0x68)---------------
TCASELECT(1);
if (!clock.begin())
{
errorClock=1;
Serial.println("Error, DS3231 not connected");
lcd.setCursor(0, 1);
lcd.print("DS3231 not connected");
delay(delayMedium);
}
else
{
Serial.println("DS3231 connected");
lcd.setCursor(0, 1);
lcd.print("DS3231: connected");
delay(delayMedium);
}
// Start OLED1 on bus 2---------------------------------------
TCASELECT(2);
if (!oled.begin())
{
Serial.println("Error, OLED1 not connected");
lcd.setCursor(0, 2);
lcd.print("OLED1 not connected");
delay(delayMedium);
}
else
{
oled.clearDisplay();
Serial.println("OLED1 connected");
lcd.setCursor(0, 2);
lcd.print("OLED1: connected");
delay(delayMedium);
}
// Start OLED2 on bus 3---------------------------------------
TCASELECT(3);
if (!oled.begin())
{
Serial.println("Error, OLED2 not connected");
lcd.setCursor(0, 2);
lcd.print("OLED2 not connected");
delay(delayMedium);
}
else
{
oled.clearDisplay();
Serial.println("OLED2 connected");
lcd.setCursor(0, 3);
lcd.print("OLED2: connected");
delay(delayMedium);
}
if(errorBme || errorClock)
{
Serial.println("Setup not successfull!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Setup not succesfull");
delay(delayMedium);
}
else
{
Serial.println("Setup completed successfully!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Setup completed");
delay(delayMedium);
}
//Display the amount of free SRAM
// display_freeram();
}
//--------------------------------------------------------------
//--------------------------------------------------------------
void loop()
{
// Read DS3231 RTC data---------------------------------------
TCASELECT(1);
dt = clock.getDateTime();
hour=dt.hour;
minute=dt.minute;
second=dt.second;
day=dt.day;
month=dt.month;
year=dt.year;
//Write time every second on OLED1----------------------------
if(second!=secondOld)
{
secondOld=second;
displayOLED1();
}
//Write sensor data to LCD display once a minute--------------
if(minute!=minuteOld)
{
minuteOld=minute;
// Read BME280 sensor data----------------------------------
TCASELECT(0);
temperature = bme.readTemperature();
humidity = bme.readHumidity();
pressure = bme.readPressure() / 100.0;
// Write Sensor data to LCD---------------------------------
lcd.clear();
lcd.setCursor(0,0);
lcd.print(msg0);
lcd.setCursor(12,0);
lcd.print(temperature,1);
lcd.write(degreeSymbol);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print(msg1);
lcd.setCursor(12,1);
lcd.print(humidity,1);
lcd.print(" % ");
lcd.setCursor(0,2);
lcd.print(msg2);
lcd.setCursor(10,2);
lcd.print(pressure,1);
lcd.print(" hPa");
lcd.setCursor(0, 3);
if (pressure >= 1020.0)
{
lcd.print(msg5);
}
else if (pressure >= 1016.0 && pressure < 1020.0)
{
lcd.print(msg4);
}
else if (pressure > 1010.0 && pressure < 1016.0)
{
lcd.print(msg3);
}
else if (pressure > 1006.0 && pressure <= 1010.0)
{
lcd.print(msg6);
}
else if (pressure <= 1006.0)
{
lcd.print(msg7);
}
}
//Assign power up start values for pressure to hours 0, -1h, -2h and -3h
if (start==1)
{
start=0;
pressureMin3=pressure;
pressureMin2=pressure;
pressureMin1=pressure;
pressureNow=pressure;
}
//Save pressure values of four consequetive hours-------------
if (hour!=hourOld)
{
hourOld=hour;
displayOLED2();
}
}
//---------------------------------------------------------
// Functions to write to OLED's---------------------------------
void displayOLED1()
{
TCASELECT(2);
oled.setFont(u8x8_font_px437wyse700a_2x2_r);
char timeBuffer[16];
char dateBuffer[16];
snprintf(timeBuffer, sizeof(timeBuffer), "%02d:%02d:%02d", hour, minute, second);
oled.drawString(0, 1, timeBuffer);
if ((pressureMin3-pressureNow)>=3.0)//First check on possible gale conditions
{
oled.drawString(0,4, " ");//Clear this line on the display
oled.drawString(0,4, "Gale ");
oled.drawString(0,6, " ");//Clear this line on the display
oled.drawString(0,6, " warning");
}
else if(abs(pressureNow-pressureMin1)<=0.3)//Stable atmospheric pressure conditions
{
oled.drawString(0,4, "Stable ");//Clear this line on the display
oled.drawString(0,6, " weather");//Clear this line on the display
}
else if((pressureMin1-pressureNow)>0.3)
{
oled.drawString(0,4, "Pressure");
oled.drawString(0,6, " falling");
}
else if((pressureNow-pressureMin1)>0.3)
{
oled.drawString(0,4, "Pressure");
oled.drawString(0,6, " rising");
}
}
void displayOLED2()
{
TCASELECT(3);
oled.setFont(u8x8_font_chroma48medium8_r);
oled.clearDisplay();
pressureMin3=pressureMin2;
pressureMin2=pressureMin1;
pressureMin1=pressureNow;
pressureNow=pressure;
hourNow = hour;
hourMin1 = (hour-1+24)%24;
hourMin2 = (hour-2+24)%24;
hourMin3 = (hour-3+24)%24;
char timeNow[6];
sprintf(timeNow, "%02d:%02d", hourNow, 0);
char timeMin1[6];
sprintf(timeMin1, "%02d:%02d", hourMin1, 0);
char timeMin2[6];
sprintf(timeMin2, "%02d:%02d", hourMin2, 0);
char timeMin3[6];
sprintf(timeMin3, "%02d:%02d", hourMin3, 0);
oled.draw1x2String(0, 0, timeNow);
oled.draw1x2String(6, 0, String(pressureNow,1).c_str());
oled.draw1x2String(13, 0, "hPa");
oled.draw1x2String(0, 2, timeMin1);
oled.draw1x2String(6, 2, String(pressureMin1,1).c_str());
oled.draw1x2String(13, 2, "hPa");
oled.draw1x2String(0, 4, timeMin2);
oled.draw1x2String(6, 4, String(pressureMin2,1).c_str());
oled.draw1x2String(13, 4, "hPa");
oled.draw1x2String(0, 6, timeMin3);
oled.draw1x2String(6, 6, String(pressureMin3,1).c_str());
oled.draw1x2String(13, 6, "hPa");
oled.display();
}
//--------------------------------------------------------------
//Let's see how much SRAM is left-------------------------------
/*
void display_freeram()
{
Serial.print(F("SRAM left: "));
Serial.print(freeRam());
Serial.println(" Bytes.");
}
int freeRam()
{
extern int __heap_start,*__brkval;
int v;
return (int)&v - (__brkval == 0
? (int)&__heap_start : (int) __brkval);
}
*/
Hi Michel, that's very impressive. Have you come across the Zambretti Forecaster? I played with one a few years ago but then I saw something shiny and never went back to it. A few people have written code for it, (not me) if you start digging around a bit. From memory, I'd say the only info you haven't already got is wind direction, which I'm not sure is crucial anyway, but adds to the accuracy. I'd planned to input the wind direction manually but a sensor would make a lot more sense. I believe the thing is very accurate.
Good luck, whichever way you go.
Have you come across the Zambretti Forecaster? I played with one a few years ago but then I saw something shiny and never went back to it. A few people have written code for it, (not me) if you start digging around a bit.
Some years ago, I did this project on the forum: https://forum.dronebotworkshop.com/show-tell/inqweather-forecasting-weather-station/
I could have sworn, I left the source code on it, but I don't see it now. If you're interested, it has the Zambretti Forecaster fleshed out and its use in the Sketch. The code is setup for use in a web server app, but the Zambretti is segregated in a separate file so you won't have to dissect it and should be able to easily adapt it to your project.
It may have troubles on your Arduino Nano because of its use of a rotating array that is required to save the last 3 hours of pressure readings. In this version it stores once for every minute. If you find it can't fit, it could be easily modified to do say every 10 to 15 minutes and thus save a lot of memory. If you have any questions or need some help, let me know.
Regards,
Inq
Thanks Mike.
Yes wind would be great. I will have to find a way to get on top of my roof ;-).
But I will certainly look in to it!
Kind regards
Mike
Thanks Inq!
An other interesting suggestion.
I’ll let you know when I start working on it!
Met grote groet
Mike

