Pub

mercredi 10 février 2021

Ma mini station météo avec Arduino - my lil' weather station with Arduino

 Voici la réalisation d'une petite station météo avec Arduino Uno R3, plus un afficheur LCD1602, un module thermomètre/hygromètre/baromètre BME280 et un module horloge temps réel DS3231.

 Here is the realization of a small weather station with Arduino Uno R3, plus a thermometer/hygrometer/barometer module BME280 and a real-time clock module DS3231.








Fonctions :

- Affichage de la température en degrés Celsius, de l'humidité et de la pression atmosphérique (ramenée au niveau de la mer) en hPa, sur l'afficheur LCD.

Et sur six LEDs (de gauche à droite) :

- LED n°1 bleue fixe : la température est inférieure à 19°C.
- LED n°1 bleue clignotante : la température a baissé durant les dix dernières minutes.
- LED n°2 rouge fixe : la température est supérieure à 20°C.
- LED n°2 rouge clignotante : la température a augmenté durant les dix dernières minutes.
- LED n°3 jaune fixe : l'humidité relative est inférieure à 40%.
- LED n°3 jaune clignotante : l'humidité relative a baissé durant les dix dernières minutes.
- LED n°4 verte fixe : l'humidité relative est supérieure à 60%.
- LED n°4 jaune clignotante : l'humidité relative a augmenté durant les dix dernières minutes.
- LED n°5 rouge fixe : la pression atmosphérique est inférieure à 1013 hPa (dépressionnaire).
- LED n°5 rouge clignotante : la pression atmosphérique a baissé d'au moins 1 hPa durant les trente dernières minutes.
- LED n°6 verte fixe : la pression atmosphérique est supérieure ou égale à 1013 hPa (anticyclone).
- LED n°6 verte clignotante : la pression atmosphérique a augmenté d'au moins 1 hPa durant les trente dernières minutes.


Functions:

- Displaying temperature in degrees Celsius, humidity and atmospheric pressure (brought back to sea level) in hPa, on the LCD display.

And out of six LEDs (from left to right):

- LED no.1 fixed blue: the temperature is less than 19°C.
- LED no.1 flashing blue: the temperature has decreased (by at least 1°C) during the last ten minutes.
- LED no.2 fixed red : the temperature is above 20°C.
- LED no.2 flashing red: the temperature has increased (
by at least 1°C) during the last ten minutes.
- LED no.3 fixed yellow: relative humidity is less than 40%.

- LED no.3 flashing yellow: relative humidity has decreased during the last ten minutes.
- LED no.4 fixed green : relative humidity is greater than 60%.
- LED no.4 flashing yellow: relative humidity increased during the last ten minutes.
- LED no.5 fixed red : atmospheric pressure is less than 1013 hPa (depression).
- LED no.5 flashing red: the air pressure has decreased (by at least 1 hPa) during the last thirty minutes.
- LED no.6 fixed green: atmospheric pressure is greater or equal to 1013 hPa (anticyclone).
- LED no.6 flashing green: atmospheric pressure increased (by at least 1 hPa) in the last thirty minutes.


Voici mon programme / Here is my software :


/* Program name: THRPA2-guyvano.ino

 * Author: Guy Vanoverbeke @GuyVano

 * Program last update (dd/mm/yyyy) : 10/02/2021

 * Arduino IDE V1.8.13

 * Board: Arduino UNO R3

 * Function: display Temperature, Relative Humidity, Barometric pressure on LCD and trends on LEDs.

 */

#include <Wire.h>

//

// LCD1602 display 2 lines of 16 characters

//

#include <LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // LCD display connected to Arduino pins 2 to 7

//

// I2C Temperature, humidity and barometric pressure sensor

//

#include <Adafruit_BME280.h>

Adafruit_BME280 bme; 

//

// I2C Real Time Clock

//

#include <RTClib.h>

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {

  "Sun",

  "Mon",

  "Tue",

  "Wed",

  "Thu",

  "Fri",

  "Sat"

  };

/*

 * Define the 6 leds pins

 */ 

const byte tblueled=8;      // Temperature Blue Led : fixed means the temperature is below Cold value / blinking : temperature is falling

const byte tredled=9;       // Temperature Red Led : fixed means the temperature is above Warm value / blinking : temperature is rising

const byte hyellowled=10;   // Humidity Yellow Led : fixed means the humidity is below Dry value / blinking : humidity is falling

const byte hgreenled=11;    // Humidity Green Led : fixed means the humidity is above Wet value / blinking : humidity is rising

const byte predled=12;      // Barometric pressure Red Led : fixed means the pressure is below Anticyclonic value / blinking : pressure is falling

const byte pgreenled=13;    // Barometric pressure Green Led : fixed means the pressure is equal or above Anticyclonic value / blinking : pressure is rising

/*

 * Define the value limits conditions of temperature and humidity to light the leds

 */

const int cold=19;  // define Cold value

const int warm=20;  // define Warm value

const int dry=40;   // define Dry value

const int wet=60;   // define Wet value

const int anticyc = 1013;  // define the Anticyclonic value

/*

 * Altitude in meter for calculating qnh

 */

const int alti=186;  // remplace with your altitude

/*

 * Arduino connected to pc or not, for monitoring and debug

 */

const boolean acnt = false; // true = connected to pc to check via serial monitor

/*

 * variables

 */

int temp = 0; // Temperature

int hum = 0;  // Relative humidity

int pres = 0; // Barometric pressure

int qnh = 0;  // Barometric pressure calculated at see level

int deltatemp = 0; // temperature evolution

int deltahum = 0; // humidity evolution

int deltaqnh = 0; // qnh evolution

int prevqnh = 0; // previous qnh

int prevtemp = 0; // previous temperature

int prevhum = 0; // previous humidity

int i = 0; // Loop control

boolean ftempup = false; // flag mem temp up 

boolean ftempdown = false; // flag mem temp down

boolean fhumup = false; // flag mem hum up

boolean fhumdown = false; // flag mem hum down

boolean fqnhup = false; // flag mem qnh up

boolean fqnhdown = false; // flag mem qnh down

byte bltemp = 0 ; // number of loops before clearing flags mem temp up or down

byte blhum = 0 ; // number of loops before clearing flags mem hum up or down

byte blqnh = 0 ; // number of loops before clearing flags mem qnh up or down

//

//

//

void setup()                                                                                     

{

  if (acnt) {

    Serial.begin(9600);

    Serial.println(" ");

    Serial.println("*******************************************************");

    Serial.println(" ");

    Serial.println("THRPA2-guyvano restarted!");

  } else {};

 /*

  * Define pins modes

  */

  pinMode(tblueled, OUTPUT);

  pinMode(tredled, OUTPUT);

  pinMode(hyellowled, OUTPUT);

  pinMode(hgreenled, OUTPUT);

  pinMode(predled, OUTPUT);

  pinMode(pgreenled, OUTPUT);

  /*

   * 

   */

  bme.begin(0x76);    // address of the BME280 I2C sensor

/*

 * Display a welcome message on the LCD

 */

  lcd.begin(16,2);

  lcd.clear();

  lcd.setCursor(3,0);

  lcd.print("Welcome to");

  lcd.setCursor(4,1);

  lcd.print("Arduino");

/* 

 *  light up all the leds to check them at startup

 */

  digitalWrite(tblueled,HIGH);

  digitalWrite(tredled,HIGH);

  digitalWrite(hyellowled,HIGH);

  digitalWrite(hgreenled,HIGH);

  digitalWrite(predled,HIGH);

  digitalWrite(pgreenled,HIGH);

  delay(2000);

  /*

   * Set date and time

   */

  rtc.begin();

  if (acnt) {

    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  } else {};

  /*

   * Clear and display the constant informations on the LCD

   */

  lcd.clear();

  lcd.setCursor(0,0) ;

  lcd.write(0xDF);              // Display the special character degree

  lcd.print("C :");

  lcd.setCursor(8,0) ;

  lcd.print("hPa:");

  lcd.setCursor(0,1) ;

  lcd.print("HR%:"); 


  pres = bme.readPressure() / 100.0F;

  temp = bme.readTemperature();

  hum = bme.readHumidity();

  qnh = pres + (alti * 0.125);

  prevtemp = temp;

  prevhum = hum;

  prevqnh = qnh;

 

}


void loop()

{


    DateTime now = rtc.now(); 

    temp = bme.readTemperature();

    hum = bme.readHumidity();

    pres = bme.readPressure() / 100.0F;

    qnh = pres + (alti * 0.125);

    lcd.setCursor(4,0);

    lcd.print("   ");

    lcd.setCursor(4,0);        

    lcd.print(temp);

    lcd.setCursor(12,0); 

    lcd.print("    ");  

    lcd.setCursor(12,0);        

    lcd.print(qnh);

    lcd.setCursor(4,1); 

    lcd.print("   ");  

    lcd.setCursor(4,1);

    lcd.print(hum);

   /*

    * Display current time

    */

    lcd.setCursor(11,1);

    if (now.hour() < 10) {

      lcd.setCursor(11,1);

      lcd.print("0");

      lcd.print(now.hour());

      }

    else { 

      lcd.print(now.hour(), DEC);

      };

    lcd.print(':');

    if (now.minute() < 10) {

      lcd.print("0");

      lcd.print(now.minute());

      }

    else {

      lcd.print(now.minute(), DEC);

      };

    lcd.print(':');

    if (now.second() < 10) {

      lcd.print("0");

      lcd.print(now.second());

      }

    else {

      lcd.print(now.second(), DEC);

      };

     //

    deltatemp = temp - prevtemp;

    if ((deltatemp > 0) & ~(ftempup)) {

      bltemp = 20;

      ftempup = true;

      ftempdown = false;

    } else {};

    //

    if ((deltatemp < 0) & ~(ftempdown)) {

      bltemp = 20;

      ftempup = false;

      ftempdown = true;

    } else {};

    //

    deltahum = hum - prevhum;

    if ((deltahum > 0) & ~(fhumup)) {

      blhum = 20;

      fhumup = true;

      fhumdown = false;

    } else {};

    //

    if ((deltahum < 0) & ~(fhumdown)) {

      blhum = 20;

      fhumup = false;

      fhumdown = true;

    } else {};

    //

    deltaqnh = qnh - prevqnh;

    if ((deltaqnh > 0) & ~(fqnhup)) {

      blqnh = 60;

      fqnhup = true;

      fqnhdown = false;

    } else {};

    //

    if ((deltaqnh < 0) & ~(fqnhdown)) {

      blqnh = 60;

      fqnhup = false;

      fqnhdown = true;

    } else {};

    //


      /*

       * command the blinking of the leds if the flags set

       */

      //

      // Temperature blink

      // 

      if (ftempup) {

        digitalWrite(tblueled,LOW);

        digitalWrite(tredled,LOW);

        delay(200);

        for(i = 0; i < 5; i++ ){

          digitalWrite(tredled,HIGH);

          delay(100);

          digitalWrite(tredled,LOW);

          delay(200);

          };

          bltemp = bltemp - 1;

        }

      else {};

     if (ftempdown){

        digitalWrite(tblueled,LOW);

        digitalWrite(tredled,LOW);

        delay(200);

        for(i = 0; i < 5; i++ ){

          digitalWrite(tblueled,HIGH);

          delay(100);

          digitalWrite(tblueled,LOW);

          delay(200);

          };

        bltemp = bltemp - 1;

        }

     else {};

     if (bltemp < 1) {

      ftempup = false;

      ftempdown = false;

      }

     else {};

      //

      // Humidity blink

      // 

      if (fhumup) {

        digitalWrite(hyellowled,LOW);

        digitalWrite(hgreenled,LOW);

        delay(200);

        for(i = 0; i < 5; i++ ){

          digitalWrite(hgreenled,HIGH);

          delay(100);

          digitalWrite(hgreenled,LOW);

          delay(200);

          };

          blhum = blhum - 1;

        }

      else {};

     if (fhumdown){

        digitalWrite(hyellowled,LOW);

        digitalWrite(hgreenled,LOW);

        delay(200);

        for(i = 0; i < 5; i++ ){

          digitalWrite(hyellowled,HIGH);

          delay(100);

          digitalWrite(hyellowled,LOW);

          delay(200);

          };

        blhum = blhum - 1;

        }

     else {};

     if (blhum < 1) {

      fhumup = false;

      fhumdown = false;

      }

     else {};

      //

      // QNH blink 

      // 

      if (fqnhup) {

        digitalWrite(predled,LOW);

        digitalWrite(pgreenled,LOW);

        delay(200);

        for(i = 0; i < 5; i++ ){

          digitalWrite(pgreenled,HIGH);

          delay(100);

          digitalWrite(pgreenled,LOW);

          delay(200);

          };

          blqnh = blqnh - 1;

        }

      else {};

     if (fqnhdown){

        digitalWrite(pgreenled,LOW);

        digitalWrite(predled,LOW);

        delay(200);

        for(i = 0; i < 5; i++ ){

          digitalWrite(predled,HIGH);

          delay(100);

          digitalWrite(predled,LOW);

          delay(200);

          };

        blqnh = blqnh - 1;

        }

     else {};

     if (blqnh < 1) {

      fqnhup = false;

      fqnhdown = false;

      }

     else {};


     /*

      * light on or off the leds following the levels conditions

      */

      if (temp < cold) {

        digitalWrite(tblueled,HIGH);

        }

      else {

        digitalWrite(tblueled,LOW);

        };

      if (temp > warm) {

        digitalWrite(tredled,HIGH);

        }

      else {

        digitalWrite(tredled,LOW);

        };

      if (hum < dry) {

        digitalWrite(hyellowled,HIGH);

        }

      else {

        digitalWrite(hyellowled,LOW);

        };

      if (hum > wet) {

        digitalWrite(hgreenled,HIGH);

        }

      else {

        digitalWrite(hgreenled,LOW);

        };

      if (qnh < anticyc) {

        digitalWrite(predled,HIGH);

        digitalWrite(pgreenled,LOW);

        }

      else {

        digitalWrite(pgreenled,HIGH);  

        digitalWrite(predled,LOW);

        };


    //

    // serial debug and control values

    //

    if (acnt) {

      Serial.println(" ");

      Serial.println(" ");

      if (now.hour() < 10) {

        Serial.print("0");

        Serial.print(now.hour());

        }

      else {

        Serial.print(now.hour());

      };

      Serial.print(":");

      if (now.minute() < 10) {

        Serial.print("0");

        Serial.print(now.minute());

        }

      else {

        Serial.print(now.minute());

      };

      Serial.print(":");

      if (now.second() < 10) {

        Serial.print("0");

        Serial.print(now.second());

        }

      else {

        Serial.print(now.second());

      };

      //

      Serial.println(" ");

      Serial.print("temp:");

      Serial.print(temp);

      Serial.print(", prevtemp:");

      Serial.print(prevtemp);

      Serial.print(", bltemp:");

      Serial.print(bltemp);

      Serial.print(", ftempup:");

      Serial.print(ftempup);

      Serial.print(", ftempdown:");

      Serial.print(ftempdown);

      //

      Serial.println(" ");

      Serial.print("hum:");

      Serial.print(hum);

      Serial.print(", prevhum:");

      Serial.print(prevhum);

      Serial.print(", blhum:");

      Serial.print(blhum);

      Serial.print(", fhumup:");

      Serial.print(fhumup);

      Serial.print(", fhumdown:");

      Serial.print(fhumdown);

      //

      Serial.println(" ");      

      Serial.print("qnh:");

      Serial.print(qnh);

      Serial.print(", prevqnh:");

      Serial.print(prevqnh);

      Serial.print(", blqnh:");

      Serial.print(blqnh);

      Serial.print(", deltaqnh:");

      Serial.print(deltaqnh);

      Serial.print(", fqnhup:");

      Serial.print(fqnhup);

      Serial.print(", fqnhdown:");

      Serial.print(fqnhdown);

      } else {};

      //

      delay(29000);

      //

      // store the actual values as the previous values for the next loop

      //

      prevtemp = temp;

      prevhum = hum;

      prevqnh = qnh;

  

}


// End of the program - Thanks for watching !



*** Guy F8ABX - 10/02/2021 ***

Aucun commentaire:

Enregistrer un commentaire