Skip to content

Commit

Permalink
Add JSON output; Available via HTTP data point /json
Browse files Browse the repository at this point in the history
  • Loading branch information
wtl0 committed Jun 26, 2022
1 parent fb9f51f commit e5adf23
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ compile_commands.json
CTestTestfile.cmake
_deps
build
tools/esp8266/tmp
tools/esp8266/binaries
/**/Debug
/**/v16/*
*.db
Expand Down
46 changes: 36 additions & 10 deletions tools/esp8266/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -738,10 +739,35 @@ void app::showLiveData(void) {
modHtml += F("</pre>");
#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)
}


Expand Down
2 changes: 2 additions & 0 deletions tools/esp8266/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e5adf23

Please sign in to comment.