#include #include #include #include #include #include #include "garage-config.h" const char* wifi_ssid = WIFI_SSID; const char* wifi_pass = WIFI_PASS; // Grafana IPAddress influxdb_ip(INFLUXDB_IP); long influxdb_port = INFLUXDB_PORT; unsigned int localPort = 2390; // MilliSeconds long unsigned poll_int = POLL_INT; String city = CITY; String location = LOCATION; WiFiUDP Udp; // Temp #define DHTPIN 19 #define DHTTYPE DHT22 DHT_Unified dht(DHTPIN, DHTTYPE); TInfo tinfo; void setup() { Serial.begin(9600); WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); } dht.begin(); sensor_t sensor; dht.temperature().getSensor(&sensor); dht.humidity().getSensor(&sensor); tinfo.init(TINFO_MODE_STANDARD); tinfo.attachNewFrame(NewTinfoFrame); } void loop() { delay(POLL_INT); // Check wifi connexion if ( WiFi.status() != WL_CONNECTED ) { int retry = 0; WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASS); while (retry < 10 || WiFi.status() != WL_CONNECTED) { retry++; delay(500); } } if ( WiFi.status() == WL_CONNECTED ) { sensors_event_t event; dht.temperature().getEvent(&event); if (isnan(event.temperature)) { // Error } else { sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature)); } dht.humidity().getEvent(&event); if (isnan(event.relative_humidity)) { // Error } else { sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity)); } if ( Serial.available() ) { tinfo.process(Serial.read()); } } } void sendToInfluxDB(String measure, String key, String value) { String line = measure+" "+key+"="+value; Udp.begin(localPort); Udp.beginPacket(influxdb_ip, influxdb_port); Udp.print(line); Udp.endPacket(); } void NewTinfoFrame(ValueList * me) { Serial.print("DEBUG NewFrame"); if ( me ) { while (me->next) { me = me->next; Serial.print("DEBUG value"); if (me->value && strlen(me->value)) { Serial.print("DEBUG name: "+String(me->name)+",value: "+String(me->value)); boolean isNumber = true; uint8_t c; char * p = me->value; // check if value is number while (*p && isNumber) { if ( *p < '0' || *p > '9' ) { isNumber = false; } p++; } // this will add "" on not number values if (!isNumber) { // FIXME } // this will remove leading zero on numbers else { Serial.print("DEBUG influx, name: "+String(me->name)+",value: "+String(atol(me->value))); sendToInfluxDB("teleinfo,city="+city, String(me->name), String(atol(me->value))); } } } } }