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