2467f07c189f0179ec215a8cecc6a983a9e5b655
[manu/arduino-maison.git] / garage / garage.ino
1 #include <WiFi.h>
2 #include <WiFiUdp.h>
3 #include <LibTeleinfo2Std.h>
4 #include <Adafruit_Sensor.h>
5 #include <DHT.h>
6 #include <DHT_U.h>
7 #include <HardwareSerial.h>
8
9 #include "garage-config.h"
10
11 // #define TI_DEBUG
12
13 const char*    wifi_ssid = WIFI_SSID;
14 const char*    wifi_pass = WIFI_PASS;
15
16 // Grafana
17 IPAddress      influxdb_ip(INFLUXDB_IP);
18 long           influxdb_port = INFLUXDB_PORT;
19 unsigned int   localPort = 2390;
20 // MilliSeconds
21 unsigned long previousMillis = millis();
22 long unsigned  poll_int = POLL_INT;
23 String city = CITY;
24 String location = LOCATION;
25 WiFiUDP Udp;
26
27 // Temp
28 #define DHTPIN 19
29 #define DHTTYPE DHT22
30 DHT_Unified dht(DHTPIN, DHTTYPE);
31
32 // Uptime timer
33 boolean tick1sec=0;// one for interrupt, don't mess with 
34 unsigned long uptime=0; // save value we can use in sketch even if we're interrupted
35
36 // Used to indicate if we need to send all date or just modified ones
37 boolean fulldata = false;
38
39 HardwareSerial TinfoSerial(2); 
40
41 TInfo tinfo;
42
43 String tinfo_SINSTS;
44 String tinfo_SINSTS1;
45 String tinfo_SINSTS2;
46 String tinfo_SINSTS3;
47 String tinfo_IRMS1;
48 String tinfo_IRMS2;
49 String tinfo_IRMS3;
50 String tinfo_URMS1;
51 String tinfo_URMS2;
52 String tinfo_URMS3;
53 String tinfo_EAST;
54
55
56 void setup() {
57
58   Serial.begin(115200);
59   TinfoSerial.begin(9600,SERIAL_7E1,22,23);
60   pinMode(22, INPUT_PULLUP);
61   
62   WiFi.mode(WIFI_STA);
63   WiFi.begin(WIFI_SSID, WIFI_PASS);
64
65   while (WiFi.status() != WL_CONNECTED) {
66     delay(500);
67   }
68
69   dht.begin();
70   sensor_t sensor;
71   dht.temperature().getSensor(&sensor);
72   dht.humidity().getSensor(&sensor);
73
74   tinfo.init(TINFO_MODE_STD);
75   tinfo.attachDataStd(DataCallback);
76
77 }
78
79 void loop() {
80
81   if (millis() - previousMillis > POLL_INT)
82   {
83     fulldata = true;
84     previousMillis = millis();   
85   
86   // Check wifi connexion
87     if ( WiFi.status() != WL_CONNECTED ) {
88         int retry = 0;
89         WiFi.mode(WIFI_STA);
90         WiFi.begin(WIFI_SSID, WIFI_PASS);
91         while (retry < 10 || WiFi.status() != WL_CONNECTED) {
92             retry++;
93             delay(500);
94         }
95     }
96
97     if ( WiFi.status() == WL_CONNECTED ) {
98         Serial.println("DEBUG wifi connected, start temp measure");
99         sensors_event_t event;
100         
101         dht.temperature().getEvent(&event);
102         if (isnan(event.temperature)) {
103           // Error
104         } else {
105             sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
106         }
107         dht.humidity().getEvent(&event);
108         if (isnan(event.relative_humidity)) {
109           // Error
110         } else {
111             sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
112         }
113         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS", tinfo_SINSTS);
114         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS1", tinfo_SINSTS1);
115         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS2", tinfo_SINSTS2);
116         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS3", tinfo_SINSTS3);
117         sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS1", tinfo_IRMS1);
118         sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS2", tinfo_IRMS2);
119         sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS3", tinfo_IRMS3);
120         sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS1", tinfo_URMS1);
121         sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS2", tinfo_URMS2);
122         sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS3", tinfo_URMS3);
123         sendToInfluxDB("teleinfo,city="+city+",location="+location, "EAST", tinfo_EAST);
124     }
125   }
126   if ( TinfoSerial.available() ) {
127      tinfo.process(TinfoSerial.read());
128   }
129 }
130
131 void sendToInfluxDB(String measure, String key, String value) {
132   String line = measure+" "+key+"="+value;
133   Serial.println("Sending to influxdb: "+line);
134   Udp.begin(localPort);
135   Udp.beginPacket(influxdb_ip, influxdb_port);
136   Udp.print(line);
137   Udp.endPacket();
138 }
139
140 void DataCallback(char * tilabel, char * tihoro, char * tivalue)
141 {
142       if (strcmp(tilabel, "SINSTS") == 0 ) {
143         tinfo_SINSTS = String(atol(tivalue));
144       }
145       if (strcmp(tilabel, "SINSTS1") == 0 ) {
146         tinfo_SINSTS1 = String(atol(tivalue));
147       }
148       if (strcmp(tilabel, "SINSTS2") == 0 ) {
149         tinfo_SINSTS2 = String(atol(tivalue));
150       }
151       if (strcmp(tilabel, "SINSTS3") == 0 ) {
152         tinfo_SINSTS3 = String(atol(tivalue));
153       }
154       if (strcmp(tilabel, "IRMS1") == 0 ) {
155         tinfo_IRMS1 = String(atol(tivalue));
156       }
157       if (strcmp(tilabel, "IRMS2") == 0 ) {
158         tinfo_IRMS2 = String(atol(tivalue));
159       }
160       if (strcmp(tilabel, "IRMS3") == 0 ) {
161         tinfo_IRMS3 = String(atol(tivalue));
162       }
163        if (strcmp(tilabel, "URMS1") == 0 ) {
164         tinfo_URMS1 = String(atol(tivalue));
165       }
166       if (strcmp(tilabel, "URMS2") == 0 ) {
167         tinfo_URMS2 = String(atol(tivalue));
168       }
169       if (strcmp(tilabel, "URMS3") == 0 ) {
170         tinfo_URMS3 = String(atol(tivalue));
171       }
172       if (strcmp(tilabel, "EAST") == 0 ) {
173         tinfo_EAST = String(atol(tivalue));
174       }
175 }