diff --git a/.gitignore b/.gitignore index fab5a5780..54e17c93a 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ compile_commands.json CTestTestfile.cmake _deps build +tools/esp8266/tmp +tools/esp8266/binaries /**/Debug /**/v16/* *.db diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index a484987ed..7d1c42615 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -52,14 +52,15 @@ void app::setup(uint32_t timeout) { DPRINTLN(DBG_VERBOSE, F("app::setup")); Main::setup(timeout); - mWeb->on("/", std::bind(&app::showIndex, this)); - mWeb->on("/setup", std::bind(&app::showSetup, this)); - mWeb->on("/save", std::bind(&app::showSave, this)); - mWeb->on("/erase", std::bind(&app::showErase, this)); - mWeb->on("/cmdstat", std::bind(&app::showStatistics, this)); - mWeb->on("/hoymiles", std::bind(&app::showHoymiles, this)); - mWeb->on("/livedata", std::bind(&app::showLiveData, this)); - + mWeb->on("/", std::bind(&app::showIndex, this)); + mWeb->on("/setup", std::bind(&app::showSetup, this)); + mWeb->on("/save", std::bind(&app::showSave, this)); + mWeb->on("/erase", std::bind(&app::showErase, this)); + mWeb->on("/cmdstat", std::bind(&app::showStatistics, this)); + mWeb->on("/hoymiles", std::bind(&app::showHoymiles, this)); + mWeb->on("/livedata", std::bind(&app::showLiveData, this)); + mWeb->on("/json", std::bind(&app::showJSON, this)); + if(mSettingsValid) { mEep->read(ADDR_INV_INTERVAL, &mSendInterval); if(mSendInterval < 5) @@ -738,10 +739,35 @@ void app::showLiveData(void) { modHtml += F(""); #endif } - } + } + mWeb->send(200, F("text/html"), modHtml); +} - mWeb->send(200, F("text/html"), modHtml); +//----------------------------------------------------------------------------- +void app::showJSON(void) { + DPRINTLN(DBG_VERBOSE, F("app::showJSON")); + String modJson; + + modJson = F("{\n"); + for(uint8_t id = 0; id < mSys->getNumInverters(); id++) { + Inverter<> *iv = mSys->getInverterByPos(id); + if(NULL != iv) { + char topic[40], val[25]; + snprintf(topic, 30, "\"%s\": {\n", iv->name); + modJson += String(topic); + for(uint8_t i = 0; i < iv->listLen; i++) { + snprintf(topic, 40, "\t\"ch%d/%s\"", iv->assign[i].ch, iv->getFieldName(i)); + snprintf(val, 25, "[%.3f, \"%s\"]", iv->getValue(i), iv->getUnit(i)); + modJson += String(topic) + ": " + String(val) + F(",\n"); + } + modJson += F("\t\"last_msg\": \"") + getDateTimeStr(iv->ts) + F("\"\n\t},\n"); + } + } + modJson += F("\"json_ts\": \"") + String(getDateTimeStr(mTimestamp)) + F("\"\n}\n"); + + // mWeb->send(200, F("text/json"), modJson); + mWeb->send(200, F("application/json"), modJson); // the preferred content-type (https://stackoverflow.com/questions/22406077/what-is-the-exact-difference-between-content-type-text-json-and-application-jso) } diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index 5430271e8..46209603a 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -67,6 +67,8 @@ class app : public Main { void showStatistics(void); void showHoymiles(void); void showLiveData(void); + void showJSON(void); + void saveValues(bool webSend); void updateCrc(void);