Use Adafruit dht library instead of SimpleDHT everywhere
[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   sensors_event_t event;
48   
49   dht.temperature().getEvent(&event);
50   if (isnan(event.temperature)) {
51     // Error
52   } else {
53       sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
54   }
55   dht.humidity().getEvent(&event);
56   if (isnan(event.relative_humidity)) {
57     // Error
58   } else {
59       sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
60   }
61   
62 }
63
64 void sendToInfluxDB(String measure, String key, String value) {
65   String line = measure+" "+key+"="+value;
66   Udp.begin(localPort);
67   Udp.beginPacket(influxdb_ip, influxdb_port);
68   Udp.print(line);
69   Udp.endPacket();
70 }