X-Git-Url: http://git.home-dn.net/?p=manu%2Farduino-maison.git;a=blobdiff_plain;f=station-meteo%2Fstation-meteo.ino;h=8c25eb830f4468961dd482b8c97a7b993ec0d78e;hp=41236ef3835b9166706d4cd9b7a91b7c8cc6c993;hb=53f952dbf7aaf1800c3632f6916393d94f1ca432;hpb=cf209e608c5a2e6d2662db6861e1ba5cecb11e4d diff --git a/station-meteo/station-meteo.ino b/station-meteo/station-meteo.ino index 41236ef..8c25eb8 100644 --- a/station-meteo/station-meteo.ino +++ b/station-meteo/station-meteo.ino @@ -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); }