diff --git a/src/main.cpp.bak b/src/main.cpp.bak deleted file mode 100644 index 5b32f08..0000000 --- a/src/main.cpp.bak +++ /dev/null @@ -1,341 +0,0 @@ -#include "proj.h" -//#define DEBUG -#ifndef DEBUG - #define eprintf( fmt, args... ) - #define DBGOUT(...) -#else - #if DEBUGHW>0 - #define FOO(...) __VA_ARGS__ - #define DBGOUT dbg_string+= FOO - #if (DEBUGHW==2) - #define eprintf(fmt, args...) S.printf(fmt, ##args) - #elif (DEBUGHW==1 || DEBUGHW==3) - #define eprintf(fmt, args...) {sprintf(dbg,fmt, ##args);dbg_string+=dbg;dbg[0]=0;} - #endif - #else - #define eprintf( fmt, args... ) - #define DBGOUT(...) - #endif -#endif - -void secTick(); -#if DEBUGHW==1 - WiFiServer dbg_server(10000); - WiFiClient dbg_client; -#endif -#ifdef STROMPREIS -String strompreis=""; -#endif // strompreis -Ticker uniTicker,secTicker; -strConfig config; -//AsyncMqttClient mq_client; // ThingsPeak Client -WiFiClient thp_client; -unsigned things_cycle; -uint32_t a_result[9]; -uint8_t key[16]; -String things_up; -unsigned thingspeak_watch; -bool new_data,new_data3,ledbit,ledflag; -unsigned first_frame; -uint8_t dow_local,dow; -uint8_t mon,year,mon_local; -unsigned kwh_day_in[7]; -unsigned kwh_day_out[7]; -unsigned last_mon_in; -unsigned last_mon_out; -uint32_t clientId; -unsigned prev_millis; -int logPage=-1; -uint8_t updates; -String lastMonth; -#if DEBUGHW>0 - char dbg[128]; - String dbg_string; -#endif // DEBUGHW -kwhstruct kwh_hist[7]; -bool inAPMode,mqttStatus,hwTest; -ADC_MODE(ADC_VCC); - -void setup(){ - Serial.begin(9600,SERIAL_8E1); // Schnittstelle zu Amis-Z?er - Serial.setTimeout(10); // f. readBytes in amis.cpp - pinMode(AP_PIN,INPUT_PULLUP); - #if LEDPIN - digitalWrite(LEDPIN,HIGH); - pinMode(LEDPIN, OUTPUT); - #endif // if LEDPIN == Serial1.txd: reroute pin function - #if DEBUGHW==2 - #if DEBUG_OUTPUT==0 - Serial.begin(115200); - #elif DEBUG_OUTPUT==1 - Serial1.begin(115200); - #endif - #endif // DEBUGHW - #ifdef OTA - initOTA(); - #endif // OTA - LittleFS.begin(); // always true! SPIFF.begin does autoformat!!! - bool test=false; - File f = LittleFS.open("/index.html", "r"); - if(f) f.close(); - else test=true; - if (!test) { - f = LittleFS.open("/custom.css", "r"); - if(f) f.close(); - else test=true; - } - if (test) { // keine html-Files - serverInit(1); // /upgrade.html als / - upgrade(0); - return; - } - serverInit(0); // /init.html als / - generalInit(); - histInit(); - connectToWifi(); // and MQTT and NTP - secTicker.attach_scheduled(1,secTick); - meter_init(); - if (config.log_sys) writeEvent("INFO", "sys", "System setup completed, running", ""); -} - -void loop(){ - #if DEBUGHW==1 - if (dbg_string.length()) { // Debug-Ausgaben TCP - dbg_string+="\n"; - if (!dbg_client.connected()) dbg_client.stop(); - if (!dbg_client) dbg_client = dbg_server.available(); - if (dbg_client) dbg_client.print(dbg_string); - dbg_string=""; - } - #elif DEBUGHW==2 - if (dbg_string.length()) { // Debug-Ausgaben Serial - S.print(dbg_string); - dbg_string=""; - } - #elif DEBUGHW==3 - if (dbg_string.length()) { // Debug-Ausgaben Websock - ws.text(clientId,dbg_string); - //Serial1.println(dbg_string); - dbg_string=""; - } - #endif - - #ifdef OTA - ArduinoOTA.handle(); - #endif - if(shouldReboot){ - secTicker.detach(); - mqttTimer.detach(); - if (config.log_sys) writeEvent("INFO", "sys", "System is going to reboot", ""); - DBGOUT("Rebooting..."); - delay(300); - ESP.wdtDisable(); // bootet 2x ??? - //ESP.restart(); - while (1) delay(1); - } - if (config.thingspeak_aktiv && thingspeak_watch>10) { - if (config.log_sys) writeEvent("INFO", "mqtt", "Connection lost long time", "reboot"); - DBGOUT("thingspeak_watch reset"); - shouldReboot=true; - } - if (ws.count()) { // ws-connections - if (new_data3) { - new_data3=false; - sendZData(); - } - } - if (WiFi.isConnected()) amis_poll(); - #ifdef LEDPIN - if (inAPMode) { - if (millis() > prev_millis) { - prev_millis=millis()+500; - digitalWrite(LEDPIN,digitalRead(LEDPIN)^1); - } - } - else { - if (ledflag && ledbit) { - digitalWrite(LEDPIN,LOW); - } - } - #endif - if (hwTest) { - for (unsigned i=0;i < 200; i++) { - Serial.write(i); - delay(1); - } - } - if (logPage >=0) { - sendEventLog(clientId,logPage); - logPage=-1; - } - delay(10); - #ifdef LEDPIN - if (ledflag) { - ledflag=false; - digitalWrite(LEDPIN,HIGH); - } - #endif -} - -void writeHistFileIn(int x, long val) { - DBGOUT("hist_in "+String(x)+" "+String(val)+"\n"); - File f = LittleFS.open("/hist_in"+String(x), "w"); - if(f) { - f.print(val); - f.close(); - } -} -void writeHistFileOut(int x, long val) { - DBGOUT("hist_out "+String(x)+" "+String(val)+"\n"); - File f = LittleFS.open("/hist_out"+String(x), "w"); - if(f) { - f.print(val); - f.close(); - } -} - -void writeMonthFile(uint8_t y,uint8_t m) { - String s=String(m); - if (s.length()<2) s="0"+s; - s=String(y)+s; - eprintf("F: %u %u %s",year, mon, s.c_str()); - File f = LittleFS.open("/monate", "a"); - f.print(s+" "); - f.print(a_result[0]); - f.print(" "); - f.print(a_result[1]); - f.print('\n'); // f.println würde \r anfügen! - f.close(); -} - -void secTick() { - things_cycle++; - #ifdef LEDPIN - if (things_cycle % 4==0) ledflag=true; - #endif - if (ws.count()) { // ws-connections - if (first_frame==0) { - sendZDataWait(); - } - } - if (valid==5){ - if (first_frame==3) { // 1. Zählerdatensatz nach reset - first_frame=2; // nächste action beim nächsten secTick - int x=dow-2; // gestern - if (x < 0) x=6; - if (x>6) x=0; - if (kwh_day_in[x] ==0) { // gestern noch keine Werte: momentanen Stand wegschreiben - kwh_day_in[x]=a_result[0]; // 1.8.0 Bezug - writeHistFileIn(x,a_result[0]); - } - if (kwh_day_out[x] ==0) { // gestern noch keine Werte: momentanen Stand wegschreiben - kwh_day_out[x]=a_result[1]; // 2.8.0 Lieferung - writeHistFileOut(x,a_result[1]); - } - dow_local=dow; - String s=String(mon); - if (s.length()<2) s="0"+s; - s=String(year)+s; - if (s.compareTo(lastMonth)!=0) writeMonthFile(year,mon); // Monat noch nicht im File - mon_local=mon; - } - else if (first_frame==2) { // Wochentabelle Energie erzeugen - int x=dow-2; // gestern, idx ab 0 - if (x < 0) x=6; - for (int i=0; i<7;i++) { - int vg=x-1; if (vg <0) vg=6; - if ((kwh_day_in[x] > kwh_day_in[vg]) && kwh_day_in[vg]) { - kwh_hist[i].kwh_in = kwh_day_in[x] - kwh_day_in[vg]; - kwh_hist[i].dow = x; - } - else kwh_hist[i].kwh_in = 0; - if ((kwh_day_out[x] > kwh_day_out[vg]) && kwh_day_out[vg]) { - kwh_hist[i].kwh_out = kwh_day_out[x] - kwh_day_out[vg]; - kwh_hist[i].dow = x; - } - else kwh_hist[i].kwh_out = 0; - x--; if (x <0) x=6; - } - updates=3; // Trigger WebClients Update - first_frame=1; // Tabelle erzeugt, initialisierung abgeschlossen - } - if (dow_local != dow) { // Tageswechsel, dow 1..7 - int x=dow-2; // gestern, idx ab 0 - if (x < 0) x=6; // x zeigt auf gestern - kwh_day_in[x]=a_result[0]; // 1.8.0 - writeHistFileIn(x,a_result[0]); - kwh_day_out[x]=a_result[1]; // 2.8.0 - writeHistFileOut(x,a_result[1]); - dow_local=dow; - if (mon_local != mon) { // Monatswechsel - writeMonthFile(year,mon); - mon_local=mon; - } - first_frame=2; // Wochen- + Monatstabelle Energie neu erzeugen - } - } - - /// Thingspeak aktualisieren - if (config.thingspeak_aktiv && things_cycle >= config.thingspeak_iv && new_data && valid==5) { - things_cycle=0; - thingspeak_watch++; - new_data = false; - - thp_client.stop(); - if (thp_client.connect("api.thingspeak.com", 80)) { - String data="api_key=" + String(config.write_api_key); - #ifdef STROMPREIS - for (unsigned i=0;i<7;i++) - data += "&field" + (String(i+1))+"="+(String)(a_result[i]); - data += "&field8="+strompreis; - #else - for (unsigned i=0;i<8;i++) - data += "&field" + (String(i+1))+"="+(String)(a_result[i]); - #endif // strompreis - thp_client.println( "POST /update HTTP/1.1" ); - thp_client.println( "Host: api.thingspeak.com" ); - thp_client.println( "Connection: close" ); - thp_client.println( "Content-Type: application/x-www-form-urlencoded" ); - thp_client.println( "Content-Length: " + String( data.length() ) ); - thp_client.println(); - thp_client.println( data ); - //DBGOUT(data+"\n"); - things_up=timecode; - } - else things_up="failed"; - thingspeak_watch=0; - } - else { - if (updates){ - switch (updates) { - case 2: - histUpdate(); // Wochentabelle Energie senden - break; - case 1: - monthUpdate(); // Wochentabelle Energie senden - break; - } - updates--; - } - } - ws.cleanupClients(); // beendete Webclients nicht mehr updaten -} - -void writeEvent(String type, String src, String desc, String data) { - DynamicJsonBuffer jsonBuffer; - JsonObject &root = jsonBuffer.createObject(); - root[F("type")] = type; - root[F("src")] = src; - root[F("desc")] = desc; - root[F("data")] = data; - root[F("time")] = timecode; - File eventlog = LittleFS.open("/eventlog.json", "a"); - if(eventlog.size() > 50000) { - eventlog.close(); - LittleFS.remove("/eventlog.json"); - File eventlog = LittleFS.open("/eventlog.json", "a"); - } - root.printTo(eventlog); - eventlog.print("\n"); - eventlog.close(); -}