Use Adafruit dht library instead of SimpleDHT
[manu/arduino-maison.git] / palier / palier.ino
1 #include <WiFi.h>
2 #include <WiFiUdp.h>
3 #include <SimpleDHT.h>
4
5 #include "palier-config.h"
6
7 const char*    wifi_ssid = WIFI_SSID;
8 const char*    wifi_pass = WIFI_PASS;
9
10 // Grafana
11 IPAddress      influxdb_ip(INFLUXDB_IP);
12 long           influxdb_port = INFLUXDB_PORT;
13 unsigned int   localPort = 2390;
14 // MilliSeconds
15 long unsigned  poll_int = POLL_INT;
16 String city = CITY;
17 String location = LOCATION;
18 WiFiUDP Udp;
19
20 int pinDHT22 = 13;
21 SimpleDHT22 dht22(pinDHT22);
22
23 void setup() {
24
25   Serial.begin(1200);
26   WiFi.mode(WIFI_STA);
27   WiFi.begin(WIFI_SSID, WIFI_PASS);
28
29   while (WiFi.status() != WL_CONNECTED) {
30     delay(500);
31   }
32 }
33
34 void loop() {
35   float temperature = 0;
36   float humidity = 0;
37   int err = SimpleDHTErrSuccess;
38   if ((err = dht22.read2(&temperature, &humidity, NULL)) == SimpleDHTErrSuccess) {
39     sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(temperature));
40     sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(humidity));
41   }
42   
43   delay(POLL_INT);
44 }
45
46 void sendToInfluxDB(String measure, String key, String value) {
47   String line = measure+" "+key+"="+value;
48   Udp.begin(localPort);
49   Udp.beginPacket(influxdb_ip, influxdb_port);
50   Udp.print(line);
51   Udp.endPacket();
52 }