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 OneWire oneWire(oneWireBus);
31 // Pass our oneWire reference to Dallas Temperature sensor 
32 DallasTemperature sensors(&oneWire);
33
34 // Uptime timer
35 boolean tick1sec=0;// one for interrupt, don't mess with 
36 unsigned long uptime=0; // save value we can use in sketch even if we're interrupted
37
38 // Used to indicate if we need to send all date or just modified ones
39 boolean fulldata = false;
40
41 HardwareSerial TinfoSerial(2); 
42
43 TInfo tinfo;
44
45 String tinfo_SINSTS;
46 String tinfo_SINSTS1;
47 String tinfo_SINSTS2;
48 String tinfo_SINSTS3;
49 String tinfo_IRMS1;
50 String tinfo_IRMS2;
51 String tinfo_IRMS3;
52 String tinfo_URMS1;
53 String tinfo_URMS2;
54 String tinfo_URMS3;
55 String tinfo_EAST;
56
57
58 void setup() {
59
60   Serial.begin(115200);
61   TinfoSerial.begin(9600,SERIAL_7E1,22,23);
62   pinMode(22, INPUT_PULLUP);
63   
64   WiFi.mode(WIFI_STA);
65   WiFi.begin(WIFI_SSID, WIFI_PASS);
66   delay(500);
67   
68   while (WiFi.status() != WL_CONNECTED) {
69     WiFi.begin(wifi_ssid, wifi_pass);
70     delay(500);
71     if (WiFi.status() != WL_CONNECTED) {
72       delay(2000);
73     }
74   }
75
76   sensors.begin();
77
78   tinfo.init(TINFO_MODE_STD);
79   tinfo.attachDataStd(DataCallback);
80
81 }
82
83 void loop() {
84
85   if (millis() - previousMillis > POLL_INT)
86   {
87     fulldata = true;
88     previousMillis = millis();   
89   
90     // Reconnect if needed
91     while (WiFi.status() != WL_CONNECTED) {
92         WiFi.begin(WIFI_SSID, WIFI_PASS);
93         delay(500);
94         if (WiFi.status() != WL_CONNECTED) {
95           delay(2000);
96         }
97     }
98
99     if ( WiFi.status() == WL_CONNECTED ) {
100         Serial.println("DEBUG wifi connected, start temp measure");
101
102         sensors.requestTemperatures(); 
103         float tempC = sensors.getTempCByIndex(0);
104         
105         if(tempC == DEVICE_DISCONNECTED_C) 
106           // Error
107         } else {
108             sendToInfluxDB("temperature,city="+city+",location=serre_semis", "value", String(tempC));
109         }
110         
111         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS", tinfo_SINSTS);
112         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS1", tinfo_SINSTS1);
113         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS2", tinfo_SINSTS2);
114         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS3", tinfo_SINSTS3);
115         sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS1", tinfo_IRMS1);
116         sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS2", tinfo_IRMS2);
117         sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS3", tinfo_IRMS3);
118         sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS1", tinfo_URMS1);
119         sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS2", tinfo_URMS2);
120         sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS3", tinfo_URMS3);
121         sendToInfluxDB("teleinfo,city="+city+",location="+location, "EAST", tinfo_EAST);
122     }
123   }
124   if ( TinfoSerial.available() ) {
125      tinfo.process(TinfoSerial.read());
126   }
127 }
128
129 void sendToInfluxDB(String measure, String key, String value) {
130   String line = measure+" "+key+"="+value;
131   Serial.println("Sending to influxdb: "+line);
132   Udp.begin(localPort);
133   Udp.beginPacket(influxdb_ip, influxdb_port);
134   Udp.print(line);
135   Udp.endPacket();
136 }
137
138 void DataCallback(char * tilabel, char * tihoro, char * tivalue)
139 {
140       if (strcmp(tilabel, "SINSTS") == 0 ) {
141         tinfo_SINSTS = String(atol(tivalue));
142       }
143       if (strcmp(tilabel, "SINSTS1") == 0 ) {
144         tinfo_SINSTS1 = String(atol(tivalue));
145       }
146       if (strcmp(tilabel, "SINSTS2") == 0 ) {
147         tinfo_SINSTS2 = String(atol(tivalue));
148       }
149       if (strcmp(tilabel, "SINSTS3") == 0 ) {
150         tinfo_SINSTS3 = String(atol(tivalue));
151       }
152       if (strcmp(tilabel, "IRMS1") == 0 ) {
153         tinfo_IRMS1 = String(atol(tivalue));
154       }
155       if (strcmp(tilabel, "IRMS2") == 0 ) {
156         tinfo_IRMS2 = String(atol(tivalue));
157       }
158       if (strcmp(tilabel, "IRMS3") == 0 ) {
159         tinfo_IRMS3 = String(atol(tivalue));
160       }
161        if (strcmp(tilabel, "URMS1") == 0 ) {
162         tinfo_URMS1 = String(atol(tivalue));
163       }
164       if (strcmp(tilabel, "URMS2") == 0 ) {
165         tinfo_URMS2 = String(atol(tivalue));
166       }
167       if (strcmp(tilabel, "URMS3") == 0 ) {
168         tinfo_URMS3 = String(atol(tivalue));
169       }
170       if (strcmp(tilabel, "EAST") == 0 ) {
171         tinfo_EAST = String(atol(tivalue));
172       }
173 }