Skip to content

Commit

Permalink
Change some output to JSON Array (arendst#1363)
Browse files Browse the repository at this point in the history
* Change output to valid JSON Array if needed (arendst#1363)
  • Loading branch information
arendst committed Dec 18, 2017
1 parent 0e72d9a commit a22fe52
Show file tree
Hide file tree
Showing 29 changed files with 77 additions and 77 deletions.
3 changes: 2 additions & 1 deletion sonoff/_releasenotes.ino
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion sonoff/settings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void SettingsSaveAll()
} else {
Settings.power = 0;
}
XsnsCall(FUNC_XSNS_SAVE_STATE);
XsnsCall(FUNC_XSNS_SAVE_BEFORE_RESTART);
SettingsSave(0);
}

Expand Down
2 changes: 1 addition & 1 deletion sonoff/sonoff.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down
18 changes: 9 additions & 9 deletions sonoff/sonoff.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions sonoff/support.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sonoff/webserver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions sonoff/xsns_01_counter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
31 changes: 15 additions & 16 deletions sonoff/xsns_03_energy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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];
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions sonoff/xsns_04_snfsc.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sonoff/xsns_05_ds18b20.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions sonoff/xsns_05_ds18x20.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions sonoff/xsns_05_ds18x20_legacy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,15 @@ 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;
case FUNC_XSNS_JSON_APPEND:
Ds18x20Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_XSNS_WEB:
case FUNC_XSNS_WEB_APPEND:
Ds18x20Show(0);
break;
#endif // USE_WEBSERVER
Expand Down
4 changes: 2 additions & 2 deletions sonoff/xsns_06_dht.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sonoff/xsns_07_sht1x.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sonoff/xsns_08_htu21.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sonoff/xsns_09_bmp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions sonoff/xsns_10_bh1750.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit a22fe52

Please sign in to comment.