5128f1fdf7899452ce054103e7d2db3a9aa703b4
[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 19
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   // Check wifi connexion
51   if ( WiFi.status() != WL_CONNECTED ) {
52       int retry = 0;
53       WiFi.mode(WIFI_STA);
54       WiFi.begin(WIFI_SSID, WIFI_PASS);
55       while (retry < 10 || WiFi.status() != WL_CONNECTED) {
56           retry++;
57           delay(500);
58       }
59   }
60
61   if ( WiFi.status() == WL_CONNECTED ) {
62       sensors_event_t event;
63       
64       dht.temperature().getEvent(&event);
65       if (isnan(event.temperature)) {
66         // Error
67       } else {
68           sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
69       }
70       dht.humidity().getEvent(&event);
71       if (isnan(event.relative_humidity)) {
72         // Error
73       } else {
74           sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
75       }
76       
77       teleinfo.process();
78       if(teleinfo.available()){
79         long I1 = teleinfo.getLongVal("IINST1");
80         long I2 = teleinfo.getLongVal("IINST2");
81         long I3 = teleinfo.getLongVal("IINST3");
82         long PAPP = teleinfo.getLongVal("PAPP");
83         long BASE = teleinfo.getLongVal("BASE");
84         sendToInfluxDB("teleinfo,city="+city+",phase=1", "IINST", String(I1));
85         sendToInfluxDB("teleinfo,city="+city+",phase=2", "IINST", String(I2));
86         sendToInfluxDB("teleinfo,city="+city+",phase=3", "IINST", String(I3));
87         sendToInfluxDB("teleinfo,city="+city, "PAPP", String(PAPP));
88         sendToInfluxDB("teleinfo,city="+city, "BASE", String(BASE));
89         teleinfo.resetAvailable();
90       }
91   }
92 }
93
94 void sendToInfluxDB(String measure, String key, String value) {
95   String line = measure+" "+key+"="+value;
96   Udp.begin(localPort);
97   Udp.beginPacket(influxdb_ip, influxdb_port);
98   Udp.print(line);
99   Udp.endPacket();
100 }