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