a6c20cc35335481a47b3859d8d191187b6cdb12f
[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
67   while (WiFi.status() != WL_CONNECTED) {
68     delay(500);
69   }
70
71   sensors.begin();
72
73   tinfo.init(TINFO_MODE_STD);
74   tinfo.attachDataStd(DataCallback);
75
76 }
77
78 void loop() {
79
80   if (millis() - previousMillis > POLL_INT)
81   {
82     fulldata = true;
83     previousMillis = millis();   
84   
85   // Check wifi connexion
86     if ( WiFi.status() != WL_CONNECTED ) {
87         int retry = 0;
88         WiFi.mode(WIFI_STA);
89         WiFi.begin(WIFI_SSID, WIFI_PASS);
90         while (retry < 10 || WiFi.status() != WL_CONNECTED) {
91             retry++;
92             delay(500);
93         }
94     }
95
96     if ( WiFi.status() == WL_CONNECTED ) {
97         Serial.println("DEBUG wifi connected, start temp measure");
98
99         sensors.requestTemperatures(); 
100         float tempC = sensors.getTempCByIndex(0);
101         
102         if(tempC == DEVICE_DISCONNECTED_C) 
103           // Error
104         } else {
105             sendToInfluxDB("temperature,city="+city+",location=serre_semis", "value", String(tempC));
106         }
107         
108         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS", tinfo_SINSTS);
109         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS1", tinfo_SINSTS1);
110         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS2", tinfo_SINSTS2);
111         sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS3", tinfo_SINSTS3);
112         sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS1", tinfo_IRMS1);
113         sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS2", tinfo_IRMS2);
114         sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS3", tinfo_IRMS3);
115         sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS1", tinfo_URMS1);
116         sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS2", tinfo_URMS2);
117         sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS3", tinfo_URMS3);
118         sendToInfluxDB("teleinfo,city="+city+",location="+location, "EAST", tinfo_EAST);
119     }
120   }
121   if ( TinfoSerial.available() ) {
122      tinfo.process(TinfoSerial.read());
123   }
124 }
125
126 void sendToInfluxDB(String measure, String key, String value) {
127   String line = measure+" "+key+"="+value;
128   Serial.println("Sending to influxdb: "+line);
129   Udp.begin(localPort);
130   Udp.beginPacket(influxdb_ip, influxdb_port);
131   Udp.print(line);
132   Udp.endPacket();
133 }
134
135 void DataCallback(char * tilabel, char * tihoro, char * tivalue)
136 {
137       if (strcmp(tilabel, "SINSTS") == 0 ) {
138         tinfo_SINSTS = String(atol(tivalue));
139       }
140       if (strcmp(tilabel, "SINSTS1") == 0 ) {
141         tinfo_SINSTS1 = String(atol(tivalue));
142       }
143       if (strcmp(tilabel, "SINSTS2") == 0 ) {
144         tinfo_SINSTS2 = String(atol(tivalue));
145       }
146       if (strcmp(tilabel, "SINSTS3") == 0 ) {
147         tinfo_SINSTS3 = String(atol(tivalue));
148       }
149       if (strcmp(tilabel, "IRMS1") == 0 ) {
150         tinfo_IRMS1 = String(atol(tivalue));
151       }
152       if (strcmp(tilabel, "IRMS2") == 0 ) {
153         tinfo_IRMS2 = String(atol(tivalue));
154       }
155       if (strcmp(tilabel, "IRMS3") == 0 ) {
156         tinfo_IRMS3 = String(atol(tivalue));
157       }
158        if (strcmp(tilabel, "URMS1") == 0 ) {
159         tinfo_URMS1 = String(atol(tivalue));
160       }
161       if (strcmp(tilabel, "URMS2") == 0 ) {
162         tinfo_URMS2 = String(atol(tivalue));
163       }
164       if (strcmp(tilabel, "URMS3") == 0 ) {
165         tinfo_URMS3 = String(atol(tivalue));
166       }
167       if (strcmp(tilabel, "EAST") == 0 ) {
168         tinfo_EAST = String(atol(tivalue));
169       }
170 }