X-Git-Url: http://git.home-dn.net/?p=manu%2Farduino-maison.git;a=blobdiff_plain;f=station-meteo%2Fstation-meteo.ino;h=dc4cc211e92e8bc9a67fd681f59a94ad4e52fd66;hp=41236ef3835b9166706d4cd9b7a91b7c8cc6c993;hb=HEAD;hpb=8197c446bbf56d9ee1a4eeb98971a7babbaf3a84 diff --git a/station-meteo/station-meteo.ino b/station-meteo/station-meteo.ino index 41236ef..dc4cc21 100644 --- a/station-meteo/station-meteo.ino +++ b/station-meteo/station-meteo.ino @@ -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; @@ -69,15 +67,19 @@ 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); + delay(500); + // 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) { + WiFi.begin(wifi_ssid, wifi_pass); + delay(500); + if (WiFi.status() != WL_CONNECTED) { + delay(2000); + } } Serial.println("Connected to wifi"); @@ -91,42 +93,53 @@ 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"); + // Reconnect if needed + while (WiFi.status() != WL_CONNECTED) { + WiFi.begin(wifi_ssid, wifi_pass); + delay(500); + if (WiFi.status() != WL_CONNECTED) { + delay(2000); + } + } + + 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); } @@ -163,5 +176,8 @@ void printWifiStatus() { void getRain () { - rainfall_ticks ++ ; + if((long)(micros() - last_micros) >= debouncing_time * 1000) { + rainfall_ticks ++ ; + last_micros = micros(); + } }