Add "grande chambre" (using esp8266 nodemcu)
[manu/arduino-maison.git] / station-meteo / station-meteo.ino
index 589c4bb..dc4cc21 100644 (file)
@@ -29,10 +29,8 @@ String location = LOCATION;
 int rainfall_sensor_pin = 2;
 float rain_incr = 0.2794; // this is mm/m2 for each sensor tick 
 int rainfall_ticks = 0;
-
-// Timing
-unsigned long previousMillis=   0;
-unsigned long previousMillis2=  0;
+long debouncing_time = 30;
+volatile unsigned long last_micros;
 
 // Initiate udp client
 WiFiUDP udp;
@@ -73,10 +71,15 @@ void setup(void)
   Serial.println(wifi_ssid);
   // Connect to WPA/WPA2 network
   WiFi.begin(wifi_ssid, wifi_pass);
+  delay(500);
 
   // attempt to connect to Wifi network:
   while (WiFi.status() != WL_CONNECTED) {
+    WiFi.begin(wifi_ssid, wifi_pass);
     delay(500);
+    if (WiFi.status() != WL_CONNECTED) {
+        delay(2000);
+    }
   }
 
   Serial.println("Connected to wifi");
@@ -92,13 +95,12 @@ void loop(void)
 { 
   delay(POLL_INT);
   
-  // Check wifi connexion
-  if ( WiFi.status() != WL_CONNECTED ) {
-      int retry = 0;
+  // Reconnect if needed
+  while (WiFi.status() != WL_CONNECTED) {
       WiFi.begin(wifi_ssid, wifi_pass);
-      while (retry < 10 || WiFi.status() != WL_CONNECTED) {
-          retry++;
-          delay(500);
+      delay(500);
+      if (WiFi.status() != WL_CONNECTED) {
+          delay(2000);
       }
   }
 
@@ -174,5 +176,8 @@ void printWifiStatus() {
 
 void getRain ()
 {
-  rainfall_ticks ++ ; 
+  if((long)(micros() - last_micros) >= debouncing_time * 1000) {
+    rainfall_ticks ++ ; 
+    last_micros = micros();
+  }
 }