Add "grande chambre" (using esp8266 nodemcu)
[manu/arduino-maison.git] / palier / palier.ino
1 #include <WiFi.h>
2 #include <WiFiUdp.h>
3 #include <Adafruit_Sensor.h>
4 #include <DHT.h>
5 #include <DHT_U.h>
6
7 #include "palier-config.h"
8
9 const char*    wifi_ssid = WIFI_SSID;
10 const char*    wifi_pass = WIFI_PASS;
11
12 // Grafana
13 IPAddress      influxdb_ip(INFLUXDB_IP);
14 long           influxdb_port = INFLUXDB_PORT;
15 unsigned int   localPort = 2390;
16 // MilliSeconds
17 long unsigned  poll_int = POLL_INT;
18 String city = CITY;
19 String location = LOCATION;
20 WiFiUDP Udp;
21
22 // Temp
23 #define DHTPIN 13
24 #define DHTTYPE DHT22
25 DHT_Unified dht(DHTPIN, DHTTYPE);
26
27 void setup() {
28
29   Serial.begin(1200);
30   WiFi.mode(WIFI_STA);
31   WiFi.begin(WIFI_SSID, WIFI_PASS);
32
33   while (WiFi.status() != WL_CONNECTED) {
34     delay(500);
35   }
36
37   dht.begin();
38   sensor_t sensor;
39   dht.temperature().getSensor(&sensor);
40   dht.humidity().getSensor(&sensor);
41
42 }
43
44 void loop() {
45   delay(POLL_INT);
46
47   // Check wifi connexion
48   if ( WiFi.status() != WL_CONNECTED ) {
49       int retry = 0;
50       WiFi.mode(WIFI_STA);
51       WiFi.begin(WIFI_SSID, WIFI_PASS);
52       while (retry < 10 || WiFi.status() != WL_CONNECTED) {
53           retry++;
54           delay(500);
55       }
56   }
57
58   if ( WiFi.status() == WL_CONNECTED ) {
59       sensors_event_t event;
60       
61       dht.temperature().getEvent(&event);
62       if (isnan(event.temperature)) {
63         // Error
64       } else {
65           sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
66       }
67       dht.humidity().getEvent(&event);
68       if (isnan(event.relative_humidity)) {
69         // Error
70       } else {
71           sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
72       }
73   }
74 }
75
76 void sendToInfluxDB(String measure, String key, String value) {
77   String line = measure+" "+key+"="+value;
78   Udp.begin(localPort);
79   Udp.beginPacket(influxdb_ip, influxdb_port);
80   Udp.print(line);
81   Udp.endPacket();
82 }