Merge branch 'master' of manu.home-dn.net:/home/manu/git/arduino-maison
[manu/arduino-maison.git] / garage / garage.ino
index 5128f1f..2467f07 100644 (file)
@@ -1,12 +1,15 @@
 #include <WiFi.h>
 #include <WiFiUdp.h>
-#include <TeleInfo.h>
+#include <LibTeleinfo2Std.h>
 #include <Adafruit_Sensor.h>
 #include <DHT.h>
 #include <DHT_U.h>
+#include <HardwareSerial.h>
 
 #include "garage-config.h"
 
+// #define TI_DEBUG
+
 const char*    wifi_ssid = WIFI_SSID;
 const char*    wifi_pass = WIFI_PASS;
 
@@ -15,6 +18,7 @@ IPAddress      influxdb_ip(INFLUXDB_IP);
 long           influxdb_port = INFLUXDB_PORT;
 unsigned int   localPort = 2390;
 // MilliSeconds
+unsigned long previousMillis = millis();
 long unsigned  poll_int = POLL_INT;
 String city = CITY;
 String location = LOCATION;
@@ -25,11 +29,36 @@ WiFiUDP Udp;
 #define DHTTYPE DHT22
 DHT_Unified dht(DHTPIN, DHTTYPE);
 
-TeleInfo teleinfo(&Serial);
+// Uptime timer
+boolean tick1sec=0;// one for interrupt, don't mess with 
+unsigned long uptime=0; // save value we can use in sketch even if we're interrupted
+
+// Used to indicate if we need to send all date or just modified ones
+boolean fulldata = false;
+
+HardwareSerial TinfoSerial(2); 
+
+TInfo tinfo;
+
+String tinfo_SINSTS;
+String tinfo_SINSTS1;
+String tinfo_SINSTS2;
+String tinfo_SINSTS3;
+String tinfo_IRMS1;
+String tinfo_IRMS2;
+String tinfo_IRMS3;
+String tinfo_URMS1;
+String tinfo_URMS2;
+String tinfo_URMS3;
+String tinfo_EAST;
+
 
 void setup() {
 
-  Serial.begin(1200);
+  Serial.begin(115200);
+  TinfoSerial.begin(9600,SERIAL_7E1,22,23);
+  pinMode(22, INPUT_PULLUP);
+  
   WiFi.mode(WIFI_STA);
   WiFi.begin(WIFI_SSID, WIFI_PASS);
 
@@ -42,59 +71,105 @@ void setup() {
   dht.temperature().getSensor(&sensor);
   dht.humidity().getSensor(&sensor);
 
+  tinfo.init(TINFO_MODE_STD);
+  tinfo.attachDataStd(DataCallback);
+
 }
 
 void loop() {
-  delay(POLL_INT);
 
+  if (millis() - previousMillis > POLL_INT)
+  {
+    fulldata = true;
+    previousMillis = millis();   
+  
   // 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 ) {
+        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));
-      }
-      
-      teleinfo.process();
-      if(teleinfo.available()){
-        long I1 = teleinfo.getLongVal("IINST1");
-        long I2 = teleinfo.getLongVal("IINST2");
-        long I3 = teleinfo.getLongVal("IINST3");
-        long PAPP = teleinfo.getLongVal("PAPP");
-        long BASE = teleinfo.getLongVal("BASE");
-        sendToInfluxDB("teleinfo,city="+city+",phase=1", "IINST", String(I1));
-        sendToInfluxDB("teleinfo,city="+city+",phase=2", "IINST", String(I2));
-        sendToInfluxDB("teleinfo,city="+city+",phase=3", "IINST", String(I3));
-        sendToInfluxDB("teleinfo,city="+city, "PAPP", String(PAPP));
-        sendToInfluxDB("teleinfo,city="+city, "BASE", String(BASE));
-        teleinfo.resetAvailable();
-      }
+    if ( WiFi.status() == WL_CONNECTED ) {
+        Serial.println("DEBUG wifi connected, start temp measure");
+        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));
+        }
+        sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS", tinfo_SINSTS);
+        sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS1", tinfo_SINSTS1);
+        sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS2", tinfo_SINSTS2);
+        sendToInfluxDB("teleinfo,city="+city+",location="+location, "SINSTS3", tinfo_SINSTS3);
+        sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS1", tinfo_IRMS1);
+        sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS2", tinfo_IRMS2);
+        sendToInfluxDB("teleinfo,city="+city+",location="+location, "IRMS3", tinfo_IRMS3);
+        sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS1", tinfo_URMS1);
+        sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS2", tinfo_URMS2);
+        sendToInfluxDB("teleinfo,city="+city+",location="+location, "URMS3", tinfo_URMS3);
+        sendToInfluxDB("teleinfo,city="+city+",location="+location, "EAST", tinfo_EAST);
+    }
+  }
+  if ( TinfoSerial.available() ) {
+     tinfo.process(TinfoSerial.read());
   }
 }
 
 void sendToInfluxDB(String measure, String key, String value) {
   String line = measure+" "+key+"="+value;
+  Serial.println("Sending to influxdb: "+line);
   Udp.begin(localPort);
   Udp.beginPacket(influxdb_ip, influxdb_port);
   Udp.print(line);
   Udp.endPacket();
 }
+
+void DataCallback(char * tilabel, char * tihoro, char * tivalue)
+{
+      if (strcmp(tilabel, "SINSTS") == 0 ) {
+        tinfo_SINSTS = String(atol(tivalue));
+      }
+      if (strcmp(tilabel, "SINSTS1") == 0 ) {
+        tinfo_SINSTS1 = String(atol(tivalue));
+      }
+      if (strcmp(tilabel, "SINSTS2") == 0 ) {
+        tinfo_SINSTS2 = String(atol(tivalue));
+      }
+      if (strcmp(tilabel, "SINSTS3") == 0 ) {
+        tinfo_SINSTS3 = String(atol(tivalue));
+      }
+      if (strcmp(tilabel, "IRMS1") == 0 ) {
+        tinfo_IRMS1 = String(atol(tivalue));
+      }
+      if (strcmp(tilabel, "IRMS2") == 0 ) {
+        tinfo_IRMS2 = String(atol(tivalue));
+      }
+      if (strcmp(tilabel, "IRMS3") == 0 ) {
+        tinfo_IRMS3 = String(atol(tivalue));
+      }
+       if (strcmp(tilabel, "URMS1") == 0 ) {
+        tinfo_URMS1 = String(atol(tivalue));
+      }
+      if (strcmp(tilabel, "URMS2") == 0 ) {
+        tinfo_URMS2 = String(atol(tivalue));
+      }
+      if (strcmp(tilabel, "URMS3") == 0 ) {
+        tinfo_URMS3 = String(atol(tivalue));
+      }
+      if (strcmp(tilabel, "EAST") == 0 ) {
+        tinfo_EAST = String(atol(tivalue));
+      }
+}