0c11eaab2ec4aa86bfe5044a775d43f4d6c9fdde
[manu/arduino-maison.git] / garage / garage.ino
1 #include <WiFi.h>
2 #include <WiFiUdp.h>
3 #include <TeleInfo.h>
4 #include <SimpleDHT.h>
5
6 #include "garage-config.h"
7
8 const char*    wifi_ssid = WIFI_SSID;
9 const char*    wifi_pass = WIFI_PASS;
10
11 // Grafana
12 IPAddress      influxdb_ip(INFLUXDB_IP);
13 long           influxdb_port = INFLUXDB_PORT;
14 unsigned int   localPort = 2390;
15 // MilliSeconds
16 long unsigned  poll_int = POLL_INT;
17 String city = CITY;
18 String location = LOCATION;
19 WiFiUDP Udp;
20
21 int pinDHT22 = 13;
22 SimpleDHT22 dht22(pinDHT22);
23
24 TeleInfo teleinfo(&Serial);
25
26 void setup() {
27
28   Serial.begin(1200);
29   WiFi.mode(WIFI_STA);
30   WiFi.begin(WIFI_SSID, WIFI_PASS);
31
32   while (WiFi.status() != WL_CONNECTED) {
33     delay(500);
34   }
35 }
36
37 void loop() {
38   float temperature = 0;
39   float humidity = 0;
40   int err = SimpleDHTErrSuccess;
41   if ((err = dht22.read2(&temperature, &humidity, NULL)) == SimpleDHTErrSuccess) {
42     sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(temperature));
43     sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(humidity));
44   }
45   
46   teleinfo.process();
47   if(teleinfo.available()){
48     long I1 = teleinfo.getLongVal("IINST1");
49     long I2 = teleinfo.getLongVal("IINST2");
50     long I3 = teleinfo.getLongVal("IINST3");
51     long PAPP = teleinfo.getLongVal("PAPP");
52     long BASE = teleinfo.getLongVal("BASE");
53     sendToInfluxDB("teleinfo,city="+city+",phase=1", "IINST", String(I1));
54     sendToInfluxDB("teleinfo,city="+city+",phase=2", "IINST", String(I2));
55     sendToInfluxDB("teleinfo,city="+city+",phase=3", "IINST", String(I3));
56     sendToInfluxDB("teleinfo,city="+city, "PAPP", String(PAPP));
57     sendToInfluxDB("teleinfo,city="+city, "BASE", String(BASE));
58     teleinfo.resetAvailable();
59   }
60   delay(POLL_INT);
61 }
62
63 void sendToInfluxDB(String measure, String key, String value) {
64   String line = measure+" "+key+"="+value;
65   Udp.begin(localPort);
66   Udp.beginPacket(influxdb_ip, influxdb_port);
67   Udp.print(line);
68   Udp.endPacket();
69 }