X-Git-Url: http://git.home-dn.net/?p=manu%2Farduino-maison.git;a=blobdiff_plain;f=serre-semis%2Fserre-semis.ino;fp=serre-semis%2Fserre-semis.ino;h=7ce44c9a6e4e03af745d261283ed3d394a23339b;hp=0000000000000000000000000000000000000000;hb=7e56da37c7137b18930e7b51c24c907a1458e7fc;hpb=a0bf5b9992c1f7da88d7c8fdd6b7ae63143d4a73 diff --git a/serre-semis/serre-semis.ino b/serre-semis/serre-semis.ino new file mode 100644 index 0000000..7ce44c9 --- /dev/null +++ b/serre-semis/serre-semis.ino @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include + +#include "serre-semis-config.h" + +const char* wifi_ssid = WIFI_SSID; +const char* wifi_pass = WIFI_PASS; + +// Grafana +IPAddress influxdb_ip(INFLUXDB_IP); +long influxdb_port = INFLUXDB_PORT; +unsigned int localPort = 2390; +// MilliSeconds +long unsigned poll_int = POLL_INT; +String city = CITY; +String location = LOCATION; +WiFiUDP Udp; + +// Temp +#define DHTPIN 13 +#define DHTTYPE DHT22 +DHT_Unified dht(DHTPIN, DHTTYPE); + +void setup() { + + Serial.begin(1200); + WiFi.mode(WIFI_STA); + WiFi.begin(WIFI_SSID, WIFI_PASS); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + } + + dht.begin(); + sensor_t sensor; + dht.temperature().getSensor(&sensor); + dht.humidity().getSensor(&sensor); + +} + +void loop() { + 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)) { + // 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) { + String line = measure+" "+key+"="+value; + Udp.begin(localPort); + Udp.beginPacket(influxdb_ip, influxdb_port); + Udp.print(line); + Udp.endPacket(); +}