Pub

samedi 27 mars 2021

Baromètre connecté : schéma et code

La toute première version du 22 mars dernier ne gérait pas correctement les problèmes de communication, mais après une modification du code le lendemain, le baromètre fonctionne depuis en permanence et il transmet correctement ses mesures à Weather Underground. La mesure de la pression atmosphérique ramenée au niveau de la mer (QNH) est transmise toutes les 5 minutes au site. En cas de problème de connexion, un nouvel essai est fait 2 minutes plus tard. Au delà de 5 échecs de connexion, le micro contrôleur redémarrera.

Les mesures sont consultables ici https://bit.ly/barogrelonges 

Faites défiler la page car le graphique est situé presque tout en bas. Par défaut, le site affiche en la pression en pouces de mercure mais l'on peut l'avoir en hPa (bouton réglages en haut à droite, puis °C pour avoir la pression en hecto-Pascal...).

Pour publier ses mesures sur Weather Underground, il faut d'abord s'être créé un compte et au moins un "device".
L'identifiant du device et son mot de passe associé sont indispensables pour la publication des mesures.
En ce qui concerne la connexion à Internet, il faudra rentrer le SSID et le mot de passe de votre connexion Wifi dans le programme.
Il faut de même renseigner correctement l'altitude du capteur (en mètres).
Donc en tout, il y a 5 constantes à personnaliser dans le programme.

Depuis le 23 mars 2021, cette version me donne satisfaction.


Le schéma de câblage des composants :





Le code de la v.2.0.0 du 23 mars 2021 :

/* Program name: esp32-baro-wu-2-0-0.ino
   Author: Guy Vanoverbeke @GuyVano
   Program last update (dd/mm/yyyy) : 23/03/2021 - V.2 R.0 C.0
   Arduino IDE V1.8.13
   Board: ESP-WROOM-32
   Function: Publish the barometric pressure from a BME280 sensor on the Weather Underground web site.
   Disclaimer:
   This program (in other words: this code, this software or this application) is a personal creation
   made as part of a hobby and it is given without guarantee of any kind and no support is provided.
   It is free of rights and can be reused freely as you wish.
*/

#include <Wire.h>
#include <WiFi.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme; // I2C
#include <HTTPClient.h>
//
// Wifi SSID and password
//
const char* ssid       = "my Wifi SSID";
const char* password   = "my Wifi password";

//
// Weather Underground device ID and password
//
const char* wudevid    = "my WU device ID";
const char* wudevpw    = "my WU device password";

//
const float myaltitude = 186; // my sensor altitude in meters
//
const boolean autcnx = true; // autorize the connection in http flag
const boolean spr = false;   // true for serial prints or false for no prints
const unsigned long ndly = 300000; // 300000 for 5 minutes. Normal delay between each data upload
const unsigned long edly = 120000; // 120000 for 2 minutes. Retry delay after error
//
float temp = 0;
float hr = 0;
float qfe = 0;
float myqnh = 0;
float myqnhinches = 0;
String wumsg; // http connection url + data to transmit
boolean error = false; // error flag
boolean rderr = false; // sensor read error flag
boolean cnxok = false; // http connection ok flag
int serr = 0; // sensor error counter
int herr = 0; // internet error counter
int hcode = 0; // returned http code
int i = 0; // loop control
String htxt; // returned http text
unsigned snsrsts; // sensor status
//
void setup() {
  //
  pinMode(19, OUTPUT); // red LED for error
  pinMode(23, OUTPUT); // green LED ok
  //
  do {
    digitalWrite(19, LOW);
    digitalWrite(23, HIGH);
    delay(500);
    digitalWrite(19, HIGH);
    digitalWrite(23, LOW);
    delay(500);
    i++;
  } while (i < 6);
  // start the serial connection
  if (spr) {
    Serial.begin(115200);
    // wait for serial monitor to open
    while (! Serial);
  }
  //
  // Connect to WiFi
  //
  if (spr) {
    Serial.println("Connecting to Wifi...");
  }
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    if (spr) {
      Serial.print(">");
    }
  }
  if (spr) {
    Serial.println();
    Serial.println(" Wifi connected.");
    //
    Serial.println();
    Serial.println("BME280 linking...");
  }
  // default settings
  snsrsts = bme.begin(0x76);
  if (!snsrsts) {
    rderr = true;
    error = true;
    if (spr) {
      Serial.println("BME280 sensor not found.");
    }
  }
}
void loop() {
  //
  HTTPClient http;
  rdSensor();
  if (spr) {
    prtValues();
  }
  bldwuMsg();
  if ((!rderr) & (autcnx)) {
    http.begin(wumsg);
    hcode = http.GET();
    htxt = http.getString();
  }
  if (spr) {
    Serial.println();
    Serial.print("Returned code: ");
    Serial.print(hcode);
    Serial.print(" / ");
    Serial.println(htxt);
  }
  if (hcode == 200) {
    // a return code 200 means success
    herr = 0;
    error = false;
    cnxok = true;
  } else {
    herr = herr + 1;
    cnxok = false;
    error = true;
    if (spr) {
      Serial.println();
      Serial.print("HTTP error #");
      Serial.print(herr);
      Serial.println(".");
    }
  }
  //
  //
  //
  if (error) {
    digitalWrite(19, HIGH);
  } else {
    digitalWrite(19, LOW);
  }
  if (cnxok) {
    digitalWrite(23, HIGH);
  } else {
    digitalWrite(23, LOW);
  }
  if (herr > 5) {
    // too much errors, restart the MCU
    ESP.restart();
  }
  //
  //
  //
  if (error) {
    delay((edly - 2000));
  } else {
    delay((ndly - 2000));
  }
  digitalWrite(19, LOW);
  digitalWrite(23, LOW);
  delay(2000);
  hcode = 0;
  htxt = ' ';
}
void prtValues() {
  //
  // Print Temperature, Humidity and barometric pressure
  //
  Serial.print("Temperature = ");
  Serial.print(temp);
  Serial.println(" °C");
  Serial.print("Humidity = ");
  Serial.print(hr);
  Serial.println(" % ");
  Serial.print("Pressure = ");
  Serial.print(myqnh);
  Serial.println(" hPa");
}
void rdSensor() {
  //
  temp = bme.readTemperature();
  hr = bme.readHumidity();
  qfe = bme.readPressure() / 100.0F;
  // myqnh = qfe + (myaltitude * 0.1205F); // previous used formulae
  myqnh = qfe / pow((1 - (myaltitude * (0.0065 / 288.15))), 5.255); // OACI standard atmosphear formulae
  myqnhinches = myqnh * 0.02953F;
  if ((myqnh < 860) | (myqnh > 1090)) {
    //
    // myqnh is out of range, probably error sensor or wiring
    //
    serr = serr + 1;
    rderr = true;
    error = true;
    if (spr) {
      Serial.println();
      Serial.print("Error reading sensor #");
      Serial.print(serr);
      Serial.println(".");
    }
  } else {
    serr = 0;
    rderr = false;
    error = false;
  }
  //
}
void bldwuMsg() {
  //
  // build the internet message for Weather Underground publishing
  //
  wumsg = "https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?ID=";
  wumsg = wumsg + wudevid + "&PASSWORD=" + wudevpw + "&dateutc=now&baromin=";
  wumsg = wumsg + myqnhinches + "&action=updateraw";
  //
  if (spr) {
    Serial.println();
    Serial.println(wumsg);
  }
}
// End of program 
esp32-baro-wu-2-0-0.ino - Thanks for watching !


Cordiales 73 !


**** Guy F8ABX - 27/03/2021 ***



Aucun commentaire:

Enregistrer un commentaire