Add "grande chambre" (using esp8266 nodemcu)
[manu/arduino-maison.git] / garage / garage.ino
1 #include <WiFi.h>
2 #include <WiFiUdp.h>
3 #include <LibTeleinfo2Std.h>
4 #include <OneWire.h>
5 #include <DallasTemperature.h>
6 #include <HardwareSerial.h>
7
8 #include "garage-config.h"
9
10 // #define TI_DEBUG
11
12 const char*    wifi_ssid = WIFI_SSID;
13 const char*    wifi_pass = WIFI_PASS;
14
15 // Grafana
16 IPAddress      influxdb_ip(INFLUXDB_IP);
17 long           influxdb_port = INFLUXDB_PORT;
18 unsigned int   localPort = 2390;
19 // MilliSeconds
20 unsigned long previousMillis = millis();
21 long unsigned  poll_int = POLL_INT;
22 String city = CITY;
23 String location = LOCATION;
24 WiFiUDP Udp;
25
26 // Temp
27 // GPIO where the DS18B20 is connected to
28 const int oneWireBus = 19;
29 // Setup a oneWire instance to communicate with any OneWire devices
30 //  ROM = 28 C6 C9 81 E3 6E 3C C ROM = 28 31 A7 1D 13 21 1 F6 ROM = 28 15 8E 4B 13 21 1 46 No more addresses.
31 DeviceAddress sensor_heater_out = {0x28, 0x31, 0xA7, 0x1D, 0x13, 0x21, 0x01, 0xF6};
32 DeviceAddress sensor_serre = {0x28, 0x15, 0x8E, 0x4B, 0x13, 0x21, 0x01, 0x46};
33 DeviceAddress sensor_heater_in = {0x28, 0xC6, 0xC9, 0x81, 0xE3, 0x6E, 0x3C ,0x0C};
34 OneWire oneWire(oneWireBus);
35 // Pass our oneWire reference to Dallas Temperature sensor 
36 DallasTemperature sensors(&oneWire);
37
38 // Uptime timer
39 boolean tick1sec=0;// one for interrupt, don't mess with 
40 unsigned long uptime=0; // save value we can use in sketch even if we're interrupted
41
42 // Used to indicate if we need to send all date or just modified ones
43 boolean fulldata = false;
44
45 HardwareSerial TinfoSerial(2); 
46
47 TInfo tinfo;
48
49 String tinfo_SINSTS;
50 String tinfo_SINSTS1;
51 String tinfo_SINSTS2;
52 String tinfo_SINSTS3;
53 String tinfo_IRMS1;
54 String tinfo_IRMS2;
55 String tinfo_IRMS3;
56 String tinfo_URMS1;
57 String tinfo_URMS2;
58 String tinfo_URMS3;
59 String tinfo_EAST;
60
61
62 void setup() {
63
64   Serial.begin(115200);
65   TinfoSerial.begin(9600,SERIAL_7E1,22,23);
66   pinMode(22, INPUT_PULLUP);
67   
68   WiFi.mode(WIFI_STA);
69   WiFi.begin(WIFI_SSID, WIFI_PASS);
70   delay(500);
71   
72   while (WiFi.status() != WL_CONNECTED) {
73     WiFi.begin(wifi_ssid, wifi_pass);
74     delay(500);
75     if (WiFi.status() != WL_CONNECTED) {
76       delay(2000);
77     }
78   }
79
80   sensors.begin();
81
82   tinfo.init(TINFO_MODE_STD);
83   tinfo.attachDataStd(DataCallback);
84
85 }
86
87 void loop() {
88
89   if (millis() - previousMillis > POLL_INT)
90   {
91     fulldata = true;
92     previousMillis = millis();   
93   
94     // Reconnect if needed
95     while (WiFi.status() != WL_CONNECTED) {
96         WiFi.begin(WIFI_SSID, WIFI_PASS);
97         delay(500);
98         if (WiFi.status() != WL_CONNECTED) {
99           delay(2000);
100         }
101     }
102
103     if ( WiFi.status() == WL_CONNECTED ) {
104         Serial.println("DEBUG wifi connected, start temp measure");
105
106         sensors.requestTemperatures(); 
107         float serre_tempC = sensors.getTempC(sensor_serre);
108         float heater_out_tempC = sensors.getTempC(sensor_heater_out);
109         float heater_in_tempC = sensors.getTempC(sensor_heater_in);
110         
111         if(serre_tempC == DEVICE_DISCONNECTED_C) {
112           // Error
113         } else {
114             sendToInfluxDB("temperature,city="+city+",location=serre-semis", "value", String(serre_tempC));
115         }
116         
117         if(heater_out_tempC == DEVICE_DISCONNECTED_C) {
118           // Error
119         } else {
120             sendToInfluxDB("temperature,city="+city+",location=heater,direction=out", "value", String(heater_out_tempC));
121         }
122         
123         if(heater_in_tempC == DEVICE_DISCONNECTED_C) {
124           // Error
125         } else {
126             sendToInfluxDB("temperature,city="+city+",location=heater,direction=in", "value", String(heater_in_tempC));
127         }
128         
129         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS", tinfo_SINSTS);
130         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS1", tinfo_SINSTS1);
131         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS2", tinfo_SINSTS2);
132         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS3", tinfo_SINSTS3);
133         sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS1", tinfo_IRMS1);
134         sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS2", tinfo_IRMS2);
135         sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS3", tinfo_IRMS3);
136         sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS1", tinfo_URMS1);
137         sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS2", tinfo_URMS2);
138         sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS3", tinfo_URMS3);
139         sendToInfluxDB("teleinfo,city="+city+",location="+location, "EAST", tinfo_EAST);
140     }
141   }
142   if ( TinfoSerial.available() ) {
143      tinfo.process(TinfoSerial.read());
144   }
145 }
146
147 void sendToInfluxDB(String measure, String key, String value) {
148   String line = measure+" "+key+"="+value;
149   Serial.println("Sending to influxdb: "+line);
150   Udp.begin(localPort);
151   Udp.beginPacket(influxdb_ip, influxdb_port);
152   Udp.print(line);
153   Udp.endPacket();
154 }
155
156 void DataCallback(char * tilabel, char * tihoro, char * tivalue)
157 {
158       if (strcmp(tilabel, "SINSTS") == 0 ) {
159         tinfo_SINSTS = String(atol(tivalue));
160       }
161       if (strcmp(tilabel, "SINSTS1") == 0 ) {
162         tinfo_SINSTS1 = String(atol(tivalue));
163       }
164       if (strcmp(tilabel, "SINSTS2") == 0 ) {
165         tinfo_SINSTS2 = String(atol(tivalue));
166       }
167       if (strcmp(tilabel, "SINSTS3") == 0 ) {
168         tinfo_SINSTS3 = String(atol(tivalue));
169       }
170       if (strcmp(tilabel, "IRMS1") == 0 ) {
171         tinfo_IRMS1 = String(atol(tivalue));
172       }
173       if (strcmp(tilabel, "IRMS2") == 0 ) {
174         tinfo_IRMS2 = String(atol(tivalue));
175       }
176       if (strcmp(tilabel, "IRMS3") == 0 ) {
177         tinfo_IRMS3 = String(atol(tivalue));
178       }
179        if (strcmp(tilabel, "URMS1") == 0 ) {
180         tinfo_URMS1 = String(atol(tivalue));
181       }
182       if (strcmp(tilabel, "URMS2") == 0 ) {
183         tinfo_URMS2 = String(atol(tivalue));
184       }
185       if (strcmp(tilabel, "URMS3") == 0 ) {
186         tinfo_URMS3 = String(atol(tivalue));
187       }
188       if (strcmp(tilabel, "EAST") == 0 ) {
189         tinfo_EAST = String(atol(tivalue));
190       }
191 }