Change some output to JSON Array (#1363)

* Change output to valid JSON Array if needed (#1363)
This commit is contained in:
arendst 2017-12-18 18:20:28 +01:00
parent df397e9add
commit 0129fc9825
29 changed files with 77 additions and 77 deletions

View File

@ -1,8 +1,9 @@
/* 5.10.0b
* Add optional support for PZEM004T energy sensor
* Change Sonoff Pow Enenrgy MQTT data message and consolidate Status 8 into Status 10
* Change Sonoff Pow Energy MQTT data message and consolidate Status 8 into Status 10
* Change Wemo SetBinaryState to distinguish from GetBinaryState (#1357)
* Change output of HTTP command to valid JSON only (#1363)
* Change output to valid JSON Array if needed (#1363)
*
* 5.10.0a
* Add (experimental) support for sensor SHT3x

View File

@ -182,7 +182,7 @@ void SettingsSaveAll()
} else {
Settings.power = 0;
}
XsnsCall(FUNC_XSNS_SAVE_STATE);
XsnsCall(FUNC_XSNS_SAVE_BEFORE_RESTART);
SettingsSave(0);
}

View File

@ -132,7 +132,7 @@ enum LightTypes {LT_BASIC, LT_PWM1, LT_PWM2, LT_PWM3, LT_PWM4, LT_PWM5, LT_PWM6,
enum LichtSubtypes {LST_NONE, LST_SINGLE, LST_COLDWARM, LST_RGB, LST_RGBW, LST_RGBWC};
enum LichtSchemes {LS_POWER, LS_WAKEUP, LS_CYCLEUP, LS_CYCLEDN, LS_RANDOM, LS_MAX};
enum XsnsFunctions {FUNC_XSNS_INIT, FUNC_XSNS_EVERY_SECOND, FUNC_XSNS_PREP, FUNC_XSNS_JSON_APPEND, FUNC_XSNS_WEB, FUNC_XSNS_SAVE_STATE};
enum XsnsFunctions {FUNC_XSNS_INIT, FUNC_XSNS_EVERY_SECOND, FUNC_XSNS_PREP_BEFORE_TELEPERIOD, FUNC_XSNS_JSON_APPEND, FUNC_XSNS_WEB_APPEND, FUNC_XSNS_SAVE_BEFORE_RESTART};
const uint8_t kDefaultRfCode[9] PROGMEM = { 0x21, 0x16, 0x01, 0x0E, 0x03, 0x48, 0x2E, 0x1A, 0x00 };

View File

@ -1100,15 +1100,15 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
else if (CMND_MODULES == command_code) {
for (byte i = 0; i < MAXMODULE; i++) {
if (!jsflg) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MODULES "%d\":\""), lines);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MODULES "%d\":["), lines);
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,"), mqtt_data);
}
jsflg = 1;
snprintf_P(stemp1, sizeof(stemp1), kModules[i].name);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%d (%s)"), mqtt_data, i +1, stemp1);
if ((strlen(mqtt_data) > 200) || (i == MAXMODULE -1)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"}"), mqtt_data);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"%d (%s)\""), mqtt_data, i +1, stemp1);
if ((strlen(mqtt_data) > 300) || (i == MAXMODULE -1)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s]}"), mqtt_data);
MqttPublishPrefixTopic_P(5, type);
jsflg = 0;
lines++;
@ -1149,15 +1149,15 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
else if (CMND_GPIOS == command_code) {
for (byte i = 0; i < GPIO_SENSOR_END; i++) {
if (!jsflg) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_GPIOS "%d\":\""), lines);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_GPIOS "%d\":["), lines);
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,"), mqtt_data);
}
jsflg = 1;
snprintf_P(stemp1, sizeof(stemp1), kSensors[i]);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%d (%s)"), mqtt_data, i, stemp1);
if ((strlen(mqtt_data) > 200) || (i == GPIO_SENSOR_END -1)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"}"), mqtt_data);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"%d (%s)\""), mqtt_data, i, stemp1);
if ((strlen(mqtt_data) > 300) || (i == GPIO_SENSOR_END -1)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s]}"), mqtt_data);
MqttPublishPrefixTopic_P(5, type);
jsflg = 0;
lines++;
@ -1871,7 +1871,7 @@ void PerformEverySecond()
if (Settings.tele_period) {
tele_period++;
if (tele_period == Settings.tele_period -1) {
XsnsCall(FUNC_XSNS_PREP);
XsnsCall(FUNC_XSNS_PREP_BEFORE_TELEPERIOD);
}
if (tele_period >= Settings.tele_period) {
tele_period = 0;

View File

@ -1339,13 +1339,13 @@ boolean Xsns02(byte function)
switch (function) {
// case FUNC_XSNS_INIT:
// break;
// case FUNC_XSNS_PREP:
// case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
// break;
case FUNC_XSNS_JSON_APPEND:
AdcShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
AdcShow(0);
break;
#endif // USE_WEBSERVER

View File

@ -511,7 +511,7 @@ void HandleAjaxStatusRefresh()
String page = "";
mqtt_data[0] = '\0';
XsnsCall(FUNC_XSNS_WEB);
XsnsCall(FUNC_XSNS_WEB_APPEND);
if (strlen(mqtt_data)) {
page += FPSTR(HTTP_TABLE100);
page += mqtt_data;

View File

@ -133,17 +133,17 @@ boolean Xsns01(byte function)
case FUNC_XSNS_INIT:
CounterInit();
break;
// case FUNC_XSNS_PREP:
// case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
// break;
case FUNC_XSNS_JSON_APPEND:
CounterShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
CounterShow(0);
break;
#endif // USE_WEBSERVER
case FUNC_XSNS_SAVE_STATE:
case FUNC_XSNS_SAVE_BEFORE_RESTART:
CounterSaveState();
break;
}

View File

@ -401,9 +401,6 @@ void PzemSerialRxRead() {
#define PZEM_POWER_ALARM (uint8_t)0xB5
#define RESP_POWER_ALARM (uint8_t)0xA5
#define RESPONSE_SIZE sizeof(PZEMCommand)
#define RESPONSE_DATA_SIZE RESPONSE_SIZE - 2
#define PZEM_DEFAULT_READ_TIMEOUT 500
#define PZEM_ERROR_VALUE -1.0
@ -418,7 +415,7 @@ IPAddress pzem_ip(192, 168, 1, 1);
float PZEM004T_voltage_rcv()
{
uint8_t data[RESPONSE_DATA_SIZE];
uint8_t data[sizeof(PZEMCommand) -2];
if (!PZEM004T_recieve(RESP_VOLTAGE, data)) {
return PZEM_ERROR_VALUE;
@ -428,7 +425,7 @@ float PZEM004T_voltage_rcv()
float PZEM004T_current_rcv()
{
uint8_t data[RESPONSE_DATA_SIZE];
uint8_t data[sizeof(PZEMCommand) -2];
if (!PZEM004T_recieve(RESP_CURRENT, data)) {
return PZEM_ERROR_VALUE;
@ -438,7 +435,7 @@ float PZEM004T_current_rcv()
float PZEM004T_power_rcv()
{
uint8_t data[RESPONSE_DATA_SIZE];
uint8_t data[sizeof(PZEMCommand) -2];
if (!PZEM004T_recieve(RESP_POWER, data)) {
return PZEM_ERROR_VALUE;
@ -448,7 +445,7 @@ float PZEM004T_power_rcv()
float PZEM004T_energy_rcv()
{
uint8_t data[RESPONSE_DATA_SIZE];
uint8_t data[sizeof(PZEMCommand) -2];
if (!PZEM004T_recieve(RESP_ENERGY, data)) {
return PZEM_ERROR_VALUE;
@ -482,17 +479,16 @@ void PZEM004T_send(uint8_t cmd)
bool PZEM004T_isReady()
{
return PzemSerialAvailable() >= RESPONSE_SIZE;
return PzemSerialAvailable() >= sizeof(PZEMCommand);
}
bool PZEM004T_recieve(uint8_t resp, uint8_t *data)
{
uint8_t buffer[RESPONSE_SIZE];
uint8_t buffer[sizeof(PZEMCommand)];
unsigned long startTime = millis();
uint8_t len = 0;
// while ((len < RESPONSE_SIZE) && (millis() - startTime < PZEM_DEFAULT_READ_TIMEOUT)) {
while ((len < RESPONSE_SIZE) && (millis() - startTime < PZEM_DEFAULT_READ_TIMEOUT)) {
while ((len < sizeof(PZEMCommand)) && (millis() - startTime < PZEM_DEFAULT_READ_TIMEOUT)) {
if (PzemSerialAvailable() > 0) {
uint8_t c = (uint8_t)PzemSerialRead();
if (!c && !len) {
@ -503,17 +499,20 @@ bool PZEM004T_recieve(uint8_t resp, uint8_t *data)
// yield(); // do background netw tasks while blocked for IO (prevents ESP watchdog trigger) - This triggers Watchdog!!!
}
if (len != RESPONSE_SIZE) {
if (len != sizeof(PZEMCommand)) {
// AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Pzem comms timeout"));
return false;
}
if (buffer[6] != PZEM004T_crc(buffer, len - 1)) {
// AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Pzem crc error"));
return false;
}
if (buffer[0] != resp) {
// AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Pzem bad response"));
return false;
}
if (data) {
for (int i = 0; i < RESPONSE_DATA_SIZE; i++) {
for (int i = 0; i < sizeof(PZEMCommand) -2; i++) {
data[i] = buffer[1 + i];
}
}
@ -1150,17 +1149,17 @@ boolean Xsns03(byte function)
case FUNC_XSNS_EVERY_SECOND:
EnergyMarginCheck();
break;
// case FUNC_XSNS_PREP:
// case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
// break;
case FUNC_XSNS_JSON_APPEND:
EnergyShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
EnergyShow(0);
break;
#endif // USE_WEBSERVER
case FUNC_XSNS_SAVE_STATE:
case FUNC_XSNS_SAVE_BEFORE_RESTART:
EnergySaveState();
break;
}

View File

@ -151,13 +151,13 @@ boolean Xsns04(byte function)
case FUNC_XSNS_INIT:
SonoffScInit();
break;
// case FUNC_XSNS_PREP:
// case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
// break;
case FUNC_XSNS_JSON_APPEND:
SonoffScShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
SonoffScShow(0);
break;
#endif // USE_WEBSERVER

View File

@ -222,14 +222,14 @@ boolean Xsns05(byte function)
case FUNC_XSNS_INIT:
Ds18x20Init();
break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
Ds18x20Convert(); // Start conversion, takes up to one second
break;
case FUNC_XSNS_JSON_APPEND:
Ds18b20Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Ds18b20Show(0);
Ds18x20Convert(); // Start conversion, takes up to one second
break;

View File

@ -404,14 +404,14 @@ boolean Xsns05(byte function)
case FUNC_XSNS_INIT:
Ds18x20Init();
break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
Ds18x20Convert(); // Start conversion, takes up to one second
break;
case FUNC_XSNS_JSON_APPEND:
Ds18x20Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Ds18x20Show(0);
Ds18x20Convert(); // Start conversion, takes up to one second
break;

View File

@ -222,7 +222,7 @@ boolean Xsns05(byte function)
case FUNC_XSNS_INIT:
Ds18x20Init();
break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
Ds18x20Search(); // Check for changes in sensors number
Ds18x20Convert(); // Start Conversion, takes up to one second
break;
@ -230,7 +230,7 @@ boolean Xsns05(byte function)
Ds18x20Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Ds18x20Show(0);
break;
#endif // USE_WEBSERVER

View File

@ -256,14 +256,14 @@ boolean Xsns06(byte function)
case FUNC_XSNS_INIT:
DhtInit();
break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
DhtReadPrep();
break;
case FUNC_XSNS_JSON_APPEND:
DhtShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
DhtShow(0);
break;
#endif // USE_WEBSERVER

View File

@ -225,14 +225,14 @@ boolean Xsns07(byte function)
switch (function) {
// case FUNC_XSNS_INIT:
// break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
ShtDetect();
break;
case FUNC_XSNS_JSON_APPEND:
ShtShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
ShtShow(0);
break;
#endif // USE_WEBSERVER

View File

@ -286,14 +286,14 @@ boolean Xsns08(byte function)
switch (function) {
// case FUNC_XSNS_INIT:
// break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
HtuDetect();
break;
case FUNC_XSNS_JSON_APPEND:
HtuShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
HtuShow(0);
break;
#endif // USE_WEBSERVER

View File

@ -504,7 +504,7 @@ boolean Xsns09(byte function)
switch (function) {
// case FUNC_XSNS_INIT:
// break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
BmpDetect();
#ifdef USE_BME680
Bme680PerformReading();
@ -514,7 +514,7 @@ boolean Xsns09(byte function)
BmpShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
BmpShow(0);
#ifdef USE_BME680
Bme680PerformReading();

View File

@ -101,14 +101,14 @@ boolean Xsns10(byte function)
switch (function) {
// case FUNC_XSNS_INIT:
// break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
Bh1750Detect();
break;
case FUNC_XSNS_JSON_APPEND:
Bh1750Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Bh1750Show(0);
break;
#endif // USE_WEBSERVER

View File

@ -106,14 +106,14 @@ boolean Xsns11(byte function)
switch (function) {
// case FUNC_XSNS_INIT:
// break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
Veml6070Detect();
break;
case FUNC_XSNS_JSON_APPEND:
Veml6070Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Veml6070Show(0);
break;
#endif // USE_WEBSERVER

View File

@ -220,14 +220,14 @@ boolean Xsns12(byte function)
switch (function) {
// case FUNC_XSNS_INIT:
// break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
Ads1115Detect();
break;
case FUNC_XSNS_JSON_APPEND:
Ads1115Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Ads1115Show(0);
break;
#endif // USE_WEBSERVER

View File

@ -140,14 +140,14 @@ boolean Xsns12(byte function)
switch (function) {
// case FUNC_XSNS_INIT:
// break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
Ads1115Detect();
break;
case FUNC_XSNS_JSON_APPEND:
Ads1115Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Ads1115Show(0);
break;
#endif // USE_WEBSERVER

View File

@ -221,14 +221,14 @@ boolean Xsns13(byte function)
switch (function) {
// case FUNC_XSNS_INIT:
// break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
Ina219Detect();
break;
case FUNC_XSNS_JSON_APPEND:
Ina219Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Ina219Show(0);
break;
#endif // USE_WEBSERVER

View File

@ -128,14 +128,14 @@ boolean Xsns14(byte function)
case FUNC_XSNS_INIT:
Sht3xDetect();
break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
Sht3xConvert();
break;
case FUNC_XSNS_JSON_APPEND:
Sht3xShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Sht3xShow(0);
Sht3xConvert();
break;

View File

@ -125,13 +125,13 @@ boolean Xsns14(byte function)
case FUNC_XSNS_INIT:
Sht3xDetect();
break;
// case FUNC_XSNS_PREP:
// case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
// break;
case FUNC_XSNS_JSON_APPEND:
Sht3xShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Sht3xShow(0);
break;
#endif // USE_WEBSERVER

View File

@ -135,14 +135,14 @@ boolean Xsns14(byte function)
case FUNC_XSNS_INIT:
Sht3xDetect();
break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
Sht3xConvert();
break;
case FUNC_XSNS_JSON_APPEND:
Sht3xShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Sht3xShow(0);
Sht3xConvert();
break;

View File

@ -257,14 +257,14 @@ boolean Xsns15(byte function)
case FUNC_XSNS_INIT:
Mhz19Init();
break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
// Mhz19Prep();
break;
case FUNC_XSNS_JSON_APPEND:
Mhz19Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Mhz19Show(0);
// Mhz19Prep();
break;

View File

@ -357,14 +357,14 @@ boolean Xsns15(byte function)
case FUNC_XSNS_INIT:
Mhz19Init();
break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
// Mhz19Prep();
break;
case FUNC_XSNS_JSON_APPEND:
Mhz19Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Mhz19Show(0);
// Mhz19Prep();
break;

View File

@ -257,14 +257,14 @@ boolean Xsns15(byte function)
case FUNC_XSNS_INIT:
Mhz19Init();
break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
// Mhz19Prep();
break;
case FUNC_XSNS_JSON_APPEND:
Mhz19Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Mhz19Show(0);
// Mhz19Prep();
break;

View File

@ -93,14 +93,14 @@ boolean Xsns16(byte function)
switch (function) {
// case FUNC_XSNS_INIT:
// break;
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
Tsl2561Detect();
break;
case FUNC_XSNS_JSON_APPEND:
Tsl2561Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Tsl2561Show(0);
break;
#endif // USE_WEBSERVER

View File

@ -118,10 +118,10 @@ boolean XsnsCall(byte Function)
switch (Function) {
case FUNC_XSNS_INIT:
case FUNC_XSNS_EVERY_SECOND:
case FUNC_XSNS_PREP:
case FUNC_XSNS_PREP_BEFORE_TELEPERIOD:
case FUNC_XSNS_JSON_APPEND:
case FUNC_XSNS_WEB:
case FUNC_XSNS_SAVE_STATE:
case FUNC_XSNS_WEB_APPEND:
case FUNC_XSNS_SAVE_BEFORE_RESTART:
for (byte x = 0; x < xsns_present; x++) {
xsns_func_ptr[x](Function);
}