Add wifi auto-reconnect
[manu/arduino-maison.git] / salon / salon.ino
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) {