X-Git-Url: http://git.home-dn.net/?p=manu%2Farduino-maison.git;a=blobdiff_plain;f=cave%2Fcave.ino;h=35b3f276d911d763f4ab6a62916f4d3e4d28c295;hp=5abccb67c1053e9a095ccce45e628570a614fbf6;hb=HEAD;hpb=285e13015a9f9a9aeff812b34bb449972afa7b28 diff --git a/cave/cave.ino b/cave/cave.ino index 5abccb6..35b3f27 100644 --- a/cave/cave.ino +++ b/cave/cave.ino @@ -1,7 +1,9 @@ #include #include #include -#include +#include +#include +#include #include "cave-config.h" @@ -22,8 +24,9 @@ WiFiUDP Udp; UltraSonicDistanceSensor distanceSensor(14, 12); // Temp -int pinDHT22 = 27; -SimpleDHT22 dht22(pinDHT22); +#define DHTPIN 27 +#define DHTTYPE DHT22 +DHT_Unified dht(DHTPIN, DHTTYPE); void setup() { @@ -48,30 +51,59 @@ void setup() { Serial.println("IP address: "); Serial.println(WiFi.localIP()); #endif + + dht.begin(); + sensor_t sensor; + dht.temperature().getSensor(&sensor); + dht.humidity().getSensor(&sensor); + } void loop() { - float temperature = 0; - float humidity = 0; - int err = SimpleDHTErrSuccess; - if ((err = dht22.read2(&temperature, &humidity, NULL)) == SimpleDHTErrSuccess) { - #if defined(DEBUG) - Serial.println("Temperature: "+String(temperature)); - Serial.println("Humidity: "+String(humidity)); - #endif - sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(temperature)); - sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(humidity)); - } else { - #if defined(DEBUG) - Serial.println("DHT 22 error"); - #endif + delay(POLL_INT); + + // 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); + } + } + + if ( WiFi.status() == WL_CONNECTED ) { + sensors_event_t event; + + dht.temperature().getEvent(&event); + if (isnan(event.temperature)) { + #if defined(DEBUG) + Serial.println("DHT 22 temperature error"); + #endif + } else { + #if defined(DEBUG) + Serial.println("Temperature: "+String(event.temperature)); + #endif + sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature)); + double water_dist = distanceSensor.measureDistanceCm(event.temperature); + #if defined(DEBUG) + Serial.println("Distance: "+String(water_dist)); + #endif + sendToInfluxDB("water_well,city="+city+",location="+location, "distance", String(water_dist)); + } + dht.humidity().getEvent(&event); + if (isnan(event.relative_humidity)) { + #if defined(DEBUG) + Serial.println("DHT 22 humidity error"); + #endif + } else { + #if defined(DEBUG) + Serial.println("Humidity: "+String(event.relative_humidity)); + #endif + sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity)); + } } - double water_dist = distanceSensor.measureDistanceCm(temperature); - #if defined(DEBUG) - Serial.println("Distance: "+String(water_dist)); - #endif - sendToInfluxDB("water_well,city="+city+",location="+location, "distance", String(water_dist)); - delay(POLL_INT); } void sendToInfluxDB(String measure, String key, String value) {