Add wifi auto-reconnect
authorEmmanuel Lacour <elacour@easter-eggs.com>
Sat, 23 Jan 2021 18:35:40 +0000 (19:35 +0100)
committerEmmanuel Lacour <elacour@easter-eggs.com>
Sat, 23 Jan 2021 18:35:40 +0000 (19:35 +0100)
cave/cave.ino
garage/garage.ino
palier/palier.ino
salon/salon.ino
station-meteo/station-meteo.ino

index 24d9d46..996154e 100644 (file)
@@ -62,34 +62,47 @@ void setup() {
 void loop() {
   delay(POLL_INT);
 
-  sensors_event_t event;
-  
-  dht.temperature().getEvent(&event);
-  if (isnan(event.temperature)) {
-      #if defined(DEBUG)
-      Serial.println("DHT 22 temperature error");
-      #endif
-  } else {
-      #if defined(DEBUG)
-      Serial.println("Temperature: "+String(event.temperature));
-      #endif
-      sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
-      double water_dist = distanceSensor.measureDistanceCm(event.temperature);
-      #if defined(DEBUG)
-      Serial.println("Distance: "+String(water_dist));
-      #endif
-      sendToInfluxDB("water_well,city="+city+",location="+location, "distance", String(water_dist));
+  // Check wifi connexion
+  if ( WiFi.status() != WL_CONNECTED ) {
+      int retry = 0;
+      WiFi.mode(WIFI_STA);
+      WiFi.begin(WIFI_SSID, WIFI_PASS);
+      while (retry < 10 || WiFi.status() != WL_CONNECTED) {
+          retry++;
+          delay(500);
+      }
   }
-  dht.humidity().getEvent(&event);
-  if (isnan(event.relative_humidity)) {
-      #if defined(DEBUG)
-      Serial.println("DHT 22 humidity error");
-      #endif
-  } else {
-      #if defined(DEBUG)
-      Serial.println("Humidity: "+String(event.relative_humidity));
-      #endif
-      sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
+
+  if ( WiFi.status() != WL_CONNECTED ) {
+      sensors_event_t event;
+      
+      dht.temperature().getEvent(&event);
+      if (isnan(event.temperature)) {
+          #if defined(DEBUG)
+          Serial.println("DHT 22 temperature error");
+          #endif
+      } else {
+          #if defined(DEBUG)
+          Serial.println("Temperature: "+String(event.temperature));
+          #endif
+          sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
+          double water_dist = distanceSensor.measureDistanceCm(event.temperature);
+          #if defined(DEBUG)
+          Serial.println("Distance: "+String(water_dist));
+          #endif
+          sendToInfluxDB("water_well,city="+city+",location="+location, "distance", String(water_dist));
+      }
+      dht.humidity().getEvent(&event);
+      if (isnan(event.relative_humidity)) {
+          #if defined(DEBUG)
+          Serial.println("DHT 22 humidity error");
+          #endif
+      } else {
+          #if defined(DEBUG)
+          Serial.println("Humidity: "+String(event.relative_humidity));
+          #endif
+          sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
+      }
   }
 }
 
index 0f0b7a7..5128f1f 100644 (file)
@@ -47,34 +47,47 @@ void setup() {
 void loop() {
   delay(POLL_INT);
 
-  sensors_event_t event;
-  
-  dht.temperature().getEvent(&event);
-  if (isnan(event.temperature)) {
-    // Error
-  } else {
-      sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
+  // Check wifi connexion
+  if ( WiFi.status() != WL_CONNECTED ) {
+      int retry = 0;
+      WiFi.mode(WIFI_STA);
+      WiFi.begin(WIFI_SSID, WIFI_PASS);
+      while (retry < 10 || WiFi.status() != WL_CONNECTED) {
+          retry++;
+          delay(500);
+      }
   }
-  dht.humidity().getEvent(&event);
-  if (isnan(event.relative_humidity)) {
-    // Error
-  } else {
-      sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
-  }
-  
-  teleinfo.process();
-  if(teleinfo.available()){
-    long I1 = teleinfo.getLongVal("IINST1");
-    long I2 = teleinfo.getLongVal("IINST2");
-    long I3 = teleinfo.getLongVal("IINST3");
-    long PAPP = teleinfo.getLongVal("PAPP");
-    long BASE = teleinfo.getLongVal("BASE");
-    sendToInfluxDB("teleinfo,city="+city+",phase=1", "IINST", String(I1));
-    sendToInfluxDB("teleinfo,city="+city+",phase=2", "IINST", String(I2));
-    sendToInfluxDB("teleinfo,city="+city+",phase=3", "IINST", String(I3));
-    sendToInfluxDB("teleinfo,city="+city, "PAPP", String(PAPP));
-    sendToInfluxDB("teleinfo,city="+city, "BASE", String(BASE));
-    teleinfo.resetAvailable();
+
+  if ( WiFi.status() == WL_CONNECTED ) {
+      sensors_event_t event;
+      
+      dht.temperature().getEvent(&event);
+      if (isnan(event.temperature)) {
+        // Error
+      } else {
+          sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
+      }
+      dht.humidity().getEvent(&event);
+      if (isnan(event.relative_humidity)) {
+        // Error
+      } else {
+          sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
+      }
+      
+      teleinfo.process();
+      if(teleinfo.available()){
+        long I1 = teleinfo.getLongVal("IINST1");
+        long I2 = teleinfo.getLongVal("IINST2");
+        long I3 = teleinfo.getLongVal("IINST3");
+        long PAPP = teleinfo.getLongVal("PAPP");
+        long BASE = teleinfo.getLongVal("BASE");
+        sendToInfluxDB("teleinfo,city="+city+",phase=1", "IINST", String(I1));
+        sendToInfluxDB("teleinfo,city="+city+",phase=2", "IINST", String(I2));
+        sendToInfluxDB("teleinfo,city="+city+",phase=3", "IINST", String(I3));
+        sendToInfluxDB("teleinfo,city="+city, "PAPP", String(PAPP));
+        sendToInfluxDB("teleinfo,city="+city, "BASE", String(BASE));
+        teleinfo.resetAvailable();
+      }
   }
 }
 
index 7cf3a33..285673d 100644 (file)
@@ -44,21 +44,33 @@ void setup() {
 void loop() {
   delay(POLL_INT);
 
-  sensors_event_t event;
-  
-  dht.temperature().getEvent(&event);
-  if (isnan(event.temperature)) {
-    // Error
-  } else {
-      sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
+  // Check wifi connexion
+  if ( WiFi.status() != WL_CONNECTED ) {
+      int retry = 0;
+      WiFi.mode(WIFI_STA);
+      WiFi.begin(WIFI_SSID, WIFI_PASS);
+      while (retry < 10 || WiFi.status() != WL_CONNECTED) {
+          retry++;
+          delay(500);
+      }
   }
-  dht.humidity().getEvent(&event);
-  if (isnan(event.relative_humidity)) {
-    // Error
-  } else {
-      sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
+
+  if ( WiFi.status() == WL_CONNECTED ) {
+      sensors_event_t event;
+      
+      dht.temperature().getEvent(&event);
+      if (isnan(event.temperature)) {
+        // Error
+      } else {
+          sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
+      }
+      dht.humidity().getEvent(&event);
+      if (isnan(event.relative_humidity)) {
+        // Error
+      } else {
+          sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
+      }
   }
-  
 }
 
 void sendToInfluxDB(String measure, String key, String value) {
index 5bcbc38..88b2462 100644 (file)
@@ -44,21 +44,33 @@ void setup() {
 void loop() {
   delay(POLL_INT);
 
-  sensors_event_t event;
-  
-  dht.temperature().getEvent(&event);
-  if (isnan(event.temperature)) {
-    // Error
-  } else {
-      sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
+  // Check wifi connexion
+  if ( WiFi.status() != WL_CONNECTED ) {
+      int retry = 0;
+      WiFi.mode(WIFI_STA);
+      WiFi.begin(WIFI_SSID, WIFI_PASS);
+      while (retry < 10 || WiFi.status() != WL_CONNECTED) {
+          retry++;
+          delay(500);
+      }
   }
-  dht.humidity().getEvent(&event);
-  if (isnan(event.relative_humidity)) {
-    // Error
-  } else {
-      sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
+
+  if ( WiFi.status() == WL_CONNECTED ) {
+      sensors_event_t event;
+      
+      dht.temperature().getEvent(&event);
+      if (isnan(event.temperature)) {
+        // Error
+      } else {
+          sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
+      }
+      dht.humidity().getEvent(&event);
+      if (isnan(event.relative_humidity)) {
+        // Error
+      } else {
+          sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
+      }
   }
-  
 }
 
 void sendToInfluxDB(String measure, String key, String value) {
index 41236ef..8c25eb8 100644 (file)
@@ -69,15 +69,14 @@ void setup(void)
     Serial.println("Please upgrade the firmware");
   }
 
+  Serial.print("Attempting to connect to SSID: ");
+  Serial.println(wifi_ssid);
+  // Connect to WPA/WPA2 network
+  WiFi.begin(wifi_ssid, wifi_pass);
+
   // attempt to connect to Wifi network:
-  while (wifi_status != WL_CONNECTED) {
-    Serial.print("Attempting to connect to SSID: ");
-    Serial.println(wifi_ssid);
-    // Connect to WPA/WPA2 network
-    wifi_status = WiFi.begin(wifi_ssid, wifi_pass);
-
-    // wait 10 seconds for connection:
-    delay(10000);
+  while (WiFi.status() != WL_CONNECTED) {
+    delay(500);
   }
 
   Serial.println("Connected to wifi");
@@ -91,42 +90,54 @@ void setup(void)
  */
 void loop(void)
 { 
-
-  String temp_influx_line;
-  String rain_influx_line;
-  float rainfall_1min(0);
-  float tempC(0);
+  delay(POLL_INT);
   
-  // Get rainfall height
-  rainfall_1min = rainfall_ticks * rain_incr;
-  rainfall_ticks = 0;
-  Serial.print("Rain mm/m2: ");
-  Serial.println(rainfall_1min);
-  Serial.println("Sending rain height to influxdb...");
-  rain_influx_line = String("rain,city="+city+"location="+location+" value=" + String(rainfall_1min, 2));
-  udp.beginPacket(influxdb_host, influxdb_port);
-  udp.print(rain_influx_line);
-  udp.endPacket();
-
-  // Temperature
-  tempC = get_temperature();
-  // Check if reading was successful
-  if(tempC != DEVICE_DISCONNECTED_C) 
-  {
-    Serial.print("Temperature for the device 1 (index 0) is: ");
-    Serial.println(tempC);
-    temp_influx_line = String("temperature,city="+city+",location="+location+" value=" + String(tempC, 2));
-    // send the packet
-    Serial.println("Sending UDP packet...");
-    udp.beginPacket(influxdb_host, influxdb_port);
-    udp.print(temp_influx_line);
-    udp.endPacket();
-  } 
-  else
-  {
-    Serial.println("Error: Could not read temperature data");
+  // Check wifi connexion
+  if ( WiFi.status() != WL_CONNECTED ) {
+      int retry = 0;
+      WiFi.begin(wifi_ssid, wifi_pass);
+      while (retry < 10 || WiFi.status() != WL_CONNECTED) {
+          retry++;
+          delay(500);
+      }
+  }
+
+  if ( WiFi.status() != WL_CONNECTED ) {
+      String temp_influx_line;
+      String rain_influx_line;
+      float rainfall_1min(0);
+      float tempC(0);
+      
+      // Get rainfall height
+      rainfall_1min = rainfall_ticks * rain_incr;
+      rainfall_ticks = 0;
+      Serial.print("Rain mm/m2: ");
+      Serial.println(rainfall_1min);
+      Serial.println("Sending rain height to influxdb...");
+      rain_influx_line = String("rain,city="+city+"location="+location+" value=" + String(rainfall_1min, 2));
+      udp.beginPacket(influxdb_host, influxdb_port);
+      udp.print(rain_influx_line);
+      udp.endPacket();
+
+      // Temperature
+      tempC = get_temperature();
+      // Check if reading was successful
+      if(tempC != DEVICE_DISCONNECTED_C) 
+      {
+        Serial.print("Temperature for the device 1 (index 0) is: ");
+        Serial.println(tempC);
+        temp_influx_line = String("temperature,city="+city+",location="+location+" value=" + String(tempC, 2));
+        // send the packet
+        Serial.println("Sending UDP packet...");
+        udp.beginPacket(influxdb_host, influxdb_port);
+        udp.print(temp_influx_line);
+        udp.endPacket();
+      } 
+      else
+      {
+        Serial.println("Error: Could not read temperature data");
+      }
   }
-  delay(POLL_INT);
 }