diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index d45baa6..682cf15 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -10,6 +10,9 @@ #include "html/h/setup_html.h" #include "html/h/hoymiles_html.h" +//SD_logger +#include +#include //----------------------------------------------------------------------------- app::app() : Main() { @@ -287,27 +290,38 @@ void app::loop(void) { mMqtt.sendMsg("uptime", val); } - if(mSerialValues) { - if(++mSerialTicker >= mSerialInterval) { - mSerialTicker = 0; - char topic[30], val[10]; - for(uint8_t id = 0; id < mSys->getNumInverters(); id++) { - Inverter<> *iv = mSys->getInverterByPos(id); - if(NULL != iv) { - if(iv->isAvailable(mTimestamp)) { - for(uint8_t i = 0; i < iv->listLen; i++) { - if(0.0f != iv->getValue(i)) { - snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, iv->getFieldName(i)); - snprintf(val, 10, "%.3f %s", iv->getValue(i), iv->getUnit(i)); - DPRINTLN(DBG_INFO, String(topic) + ": " + String(val)); - } - yield(); - } - } - } + if (mSerialValues) { + if (++mSerialTicker >= mSerialInterval) { + String logFilename = (getDateTimeStr(mTimestamp)); //get current time in format yyyy-mm-dd hh:mm:ss + logFilename.remove(10, 9); //remove hh:mm:ss from yyyy-mm-dd hh:mm:ss including space between dd and hh + logFilename += ".csv"; + //Serial.println(logFilename); //for debug only + File dataFile = SD.open(logFilename, FILE_WRITE); //open or create file on SD card + mSerialTicker = 0; + char topic[30], val[10]; + for (uint8_t id = 0; id < mSys->getNumInverters(); id++) { + Inverter<> *iv = mSys->getInverterByPos(id); + if (NULL != iv) { + if (iv->isAvailable(mTimestamp)) { + for (uint8_t i = 0; i < iv->listLen; i++) { + if (0.0f != iv->getValue(i)) { + snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, iv->getFieldName(i)); + dataFile.print(topic); //write to SD card + dataFile.print(","); //write "," to SD card in same line for csv format + snprintf(val, 10, "%.3f %s", iv->getValue(i), iv->getUnit(i)); + dataFile.print(val); //write to SD card + dataFile.print(","); //write "," to SD card in same line for csv format + DPRINTLN(DBG_INFO, String(topic) + ": " + String(val)); } + yield(); + } } + } } + dataFile.println(" "); //start new line in logfile + dataFile.close(); //close file on SD card + } + } if(++mSendTicker >= mSendInterval) { mSendTicker = 0; diff --git a/tools/esp8266/esp8266.ino b/tools/esp8266/esp8266.ino index 4ddd706..822de03 100644 --- a/tools/esp8266/esp8266.ino +++ b/tools/esp8266/esp8266.ino @@ -14,24 +14,39 @@ #include "app.h" #include "config.h" +//SD_logger +#include +#include +const int chipSelect = 16; //SD-CS pin GPIO16 = D0 + app myApp; //----------------------------------------------------------------------------- void setup() { - myApp.setup(WIFI_TRY_CONNECT_TIME); + myApp.setup(WIFI_TRY_CONNECT_TIME); + + // TODO: move to HmRadio + attachInterrupt(digitalPinToInterrupt(myApp.getIrqPin()), handleIntr, FALLING); + + if (!SD.begin(chipSelect)) { + Serial.println("SD card failed, or not present"); + // don't do anything more: + return; + } + Serial.println("SD card initialized."); - // TODO: move to HmRadio - attachInterrupt(digitalPinToInterrupt(myApp.getIrqPin()), handleIntr, FALLING); } //----------------------------------------------------------------------------- void loop() { - myApp.loop(); + myApp.loop(); + + } //----------------------------------------------------------------------------- ICACHE_RAM_ATTR void handleIntr(void) { - myApp.handleIntr(); + myApp.handleIntr(); } diff --git a/tools/esp8266/main.cpp b/tools/esp8266/main.cpp index ce96cbc..193cf29 100644 --- a/tools/esp8266/main.cpp +++ b/tools/esp8266/main.cpp @@ -311,6 +311,7 @@ void Main::showUptime(void) { snprintf(time, 20, "%d Tage, %02d:%02d:%02d", upTimeDy, upTimeHr, upTimeMn, upTimeSc); mWeb->send(200, "text/plain", String(time)); + } @@ -447,6 +448,7 @@ String Main::getDateTimeStr(time_t t) { else sprintf(str, "%04d-%02d-%02d %02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t)); return String(str); + }