Add wifi auto-reconnect
[manu/arduino-maison.git] / garage / garage.ino
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();
+      }
   }
 }