270d8d04b71be30ce52a8e9f19d6ea1a349d59d0
[manu/arduino-maison.git] / garage / garage.ino
1 #include <WiFi.h>
2 #include <WiFiUdp.h>
3 #include <LibTeleinfo.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 const char*    wifi_ssid = WIFI_SSID;
12 const char*    wifi_pass = WIFI_PASS;
13
14 // Grafana
15 IPAddress      influxdb_ip(INFLUXDB_IP);
16 long           influxdb_port = INFLUXDB_PORT;
17 unsigned int   localPort = 2390;
18 // MilliSeconds
19 long unsigned  poll_int = POLL_INT;
20 String city = CITY;
21 String location = LOCATION;
22 WiFiUDP Udp;
23
24 // Temp
25 #define DHTPIN 19
26 #define DHTTYPE DHT22
27 DHT_Unified dht(DHTPIN, DHTTYPE);
28
29 HardwareSerial TinfoSerial(1); 
30
31 TInfo tinfo;
32
33 void setup() {
34
35   Serial.begin(115200);
36   TinfoSerial.begin(9600,SERIAL_8N1, 22, 23);
37   WiFi.mode(WIFI_STA);
38   WiFi.begin(WIFI_SSID, WIFI_PASS);
39
40   while (WiFi.status() != WL_CONNECTED) {
41     delay(500);
42   }
43
44   dht.begin();
45   sensor_t sensor;
46   dht.temperature().getSensor(&sensor);
47   dht.humidity().getSensor(&sensor);
48
49   tinfo.init(TINFO_MODE_STANDARD);
50   // tinfo.attachNewFrame(NewTinfoFrame);
51   // tinfo.attachUpdatedFrame(NewTinfoFrame);
52   tinfo.attachData(NewTinfoFrame);
53
54 }
55
56 void loop() {
57   
58   delay(POLL_INT);
59
60   // Check wifi connexion
61   if ( WiFi.status() != WL_CONNECTED ) {
62       int retry = 0;
63       WiFi.mode(WIFI_STA);
64       WiFi.begin(WIFI_SSID, WIFI_PASS);
65       while (retry < 10 || WiFi.status() != WL_CONNECTED) {
66           retry++;
67           delay(500);
68       }
69   }
70
71   if ( WiFi.status() == WL_CONNECTED ) {
72       Serial.println("DEBUG");
73       sensors_event_t event;
74       
75       dht.temperature().getEvent(&event);
76       if (isnan(event.temperature)) {
77         // Error
78       } else {
79           sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature));
80       }
81       dht.humidity().getEvent(&event);
82       if (isnan(event.relative_humidity)) {
83         // Error
84       } else {
85           sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity));
86       }
87       if ( TinfoSerial.available() ) {
88         Serial.println("DEBUG: process tinfo");
89         tinfo.process(TinfoSerial.read());
90       }
91   }
92 }
93
94 void sendToInfluxDB(String measure, String key, String value) {
95   String line = measure+" "+key+"="+value;
96   Udp.begin(localPort);
97   Udp.beginPacket(influxdb_ip, influxdb_port);
98   Udp.print(line);
99   Udp.endPacket();
100 }
101
102 void NewTinfoFrame(ValueList * me, uint8_t flags)
103 {
104   Serial.println("DEBUG NewFrame");
105   if ( me ) {
106     while (me->next) {
107       me = me->next;
108       Serial.println("DEBUG value");
109       if (me->value && strlen(me->value)) {
110         Serial.println("DEBUG name: "+String(me->name)+",value: "+String(me->value));
111         boolean isNumber = true;
112         uint8_t c;
113         char * p = me->value;
114
115         // check if value is number
116         while (*p && isNumber) {
117           if ( *p < '0' || *p > '9' ) {
118             isNumber = false;
119           }
120           p++;
121         }
122   
123         // this will add "" on not number values
124         if (!isNumber) {
125             // FIXME
126         }
127         // this will remove leading zero on numbers
128         else {
129           Serial.println("DEBUG influx, name: "+String(me->name)+",value: "+String(atol(me->value)));
130           sendToInfluxDB("teleinfo,city="+city, String(me->name), String(atol(me->value)));
131         }
132       }
133     }
134   }
135 }