v5.8.0q Fixes and Shrinking code

5.8.0q
 * Shrink code by 4k using command lookup tables
 * Add retry
counter to DHT11/21/22 sensors (#1082)
 * Add commands RfSync, RfLow,
RfHigh, RfHost and RfCode to allow sending custom RF codes (#1001)
This commit is contained in:
arendst 2017-10-29 18:18:46 +01:00
parent 133ba11a6c
commit ebe642c995
17 changed files with 596 additions and 330 deletions

View File

@ -1,7 +1,7 @@
## Sonoff-Tasmota
Provide ESP8266 based Sonoff by [iTead Studio](https://www.itead.cc/) and ElectroDragon IoT Relay with Serial, Web and MQTT control allowing 'Over the Air' or OTA firmware updates using Arduino IDE.
Current version is **5.8.0p** - See [sonoff/_releasenotes.ino](https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/_releasenotes.ino) for change information.
Current version is **5.8.0q** - See [sonoff/_releasenotes.ino](https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/_releasenotes.ino) for change information.
### ATTENTION All versions

View File

@ -1,4 +1,9 @@
/* 5.8.0p
/* 5.8.0q
* Shrink code by 4k using command lookup tables
* Add retry counter to DHT11/21/22 sensors (#1082)
* Add commands RfSync, RfLow, RfHigh, RfHost and RfCode to allow sending custom RF codes (#1001)
*
* 5.8.0p
* Fix initial PwmFrequency and PwmRange
* Add support for Arilux AL-LC01 RGB Led controller (#370)
* Add light turn Off Fade (#925)
@ -99,7 +104,7 @@
* Fix compile error when DOMOTICZ_UPDATE_TIMER is not defined (#930)
* Fix alignment of web page items in some browsers (#935)
* Add smoother movement of hour hand in WS2812 led clock (#936)
* Add support for Magic Home RGBW Led controller (#940)
* Add support for Magic Home RGBW and some Arilux Led controllers (#940)
*
* 5.8.0b
* Fix command FullTopic entry when using serial or console interface

View File

@ -30,6 +30,55 @@
#endif
// Common
enum UnitNames {
UNIT_AMPERE,
UNIT_HOUR,
UNIT_KILOWATTHOUR,
UNIT_LUX,
UNIT_MICROSECOND,
UNIT_MILLIAMPERE,
UNIT_MILLISECOND,
UNIT_MINUTE,
UNIT_PRESSURE,
UNIT_SECOND,
UNIT_SECTORS,
UNIT_VOLT,
UNIT_WATT,
UNIT_WATTHOUR };
const char kUnitNames[] PROGMEM =
D_UNIT_AMPERE "|"
D_UNIT_HOUR "|"
D_UNIT_KILOWATTHOUR "|"
D_UNIT_LUX "|"
D_UNIT_MICROSECOND "|"
D_UNIT_MILLIAMPERE "|"
D_UNIT_MILLISECOND "|"
D_UNIT_MINUTE "|"
D_UNIT_PRESSURE "|"
D_UNIT_SECOND "|"
D_UNIT_SECTORS "|"
D_UNIT_VOLT "|"
D_UNIT_WATT "|"
D_UNIT_WATTHOUR ;
const char S_JSON_COMMAND_NVALUE_SPACE_UNIT[] PROGMEM = "{\"%s\":\"%d %s\"}";
const char S_JSON_COMMAND_SVALUE_SPACE_UNIT[] PROGMEM = "{\"%s\":\"%s %s\"}";
const char S_JSON_COMMAND_NVALUE_UNIT[] PROGMEM = "{\"%s\":\"%d%s\"}";
const char S_JSON_COMMAND_NVALUE_UNIT_NVALUE_UNIT[] PROGMEM = "{\"%s\":\"%d%s (%d%s)\"}";
const char S_JSON_COMMAND_NVALUE_SVALUE[] PROGMEM = "{\"%s\":\"%d (%s)\"}";
const char S_JSON_COMMAND_NVALUE_ACTIVE_NVALUE[] PROGMEM = "{\"%s\":\"%d (" D_ACTIVE " %d)\"}";
const char S_JSON_COMMAND_NVALUE[] PROGMEM = "{\"%s\":%d}";
const char S_JSON_COMMAND_SVALUE[] PROGMEM = "{\"%s\":\"%s\"}";
const char S_JSON_COMMAND_INDEX_NVALUE[] PROGMEM = "{\"%s%d\":%d}";
const char S_JSON_COMMAND_INDEX_SVALUE[] PROGMEM = "{\"%s%d\":\"%s\"}";
const char S_JSON_COMMAND_INDEX_SVALUE_SVALUE[] PROGMEM = "{\"%s%d\":\"%s%s\"}";
const char JSON_SNS_TEMPHUM[] PROGMEM =
"%s, \"%s\":{\"" D_TEMPERATURE "\":%s, \"" D_HUMIDITY "\":%s}";
const char S_LOG_HTTP[] PROGMEM = D_LOG_HTTP;
const char S_LOG_WIFI[] PROGMEM = D_LOG_WIFI;
const char S_LOG_MQTT[] PROGMEM = D_LOG_MQTT;
@ -59,9 +108,6 @@ const char kPrefixes[3][PRFX_MAX_STRING_LENGTH] PROGMEM = {
D_STAT,
D_TELE };
const char JSON_SNS_TEMPHUM[] PROGMEM =
"%s, \"%s\":{\"" D_TEMPERATURE "\":%s, \"" D_HUMIDITY "\":%s}";
// support.ino
static const char kMonthNames[] = D_MONTH3LIST;

View File

@ -643,24 +643,26 @@
#define D_CMND_IRHVAC "IRHVAC"
// Commands xdrv_snfbridge.ino
#define D_CMND_RFCODE "RfCode"
#define D_CMND_RFHIGH "RfHigh"
#define D_CMND_RFHOST "RfHost"
#define D_CMND_RFKEY "RfKey"
#define D_CMND_RFDEFAULT "RfDefault"
#define D_CMND_RFLOW "RfLow"
#define D_CMND_RFSYNC "RfSync"
// Commands xdrv_snfled.ino
#define D_CMND_COLOR "Color"
#define D_CMND_COLORTEMPERATURE "CT"
#define D_CMND_DIMMER "Dimmer"
#define D_CMND_LED "Led"
#define D_CMND_LEDTABLE "LedTable"
#define D_CMND_FADE "Fade"
#define D_CMND_SPEED "Speed"
#define D_CMND_WAKEUPDURATION "WakeUpDuration"
#define D_CMND_WAKEUP "Wakeup"
// Commands xdrv_ws2812.ino
#define D_CMND_PIXELS "Pixels"
#define D_CMND_LED "Led"
#define D_CMND_WIDTH "Width"
#define D_CMND_SCHEME "Scheme"
#define D_CMND_SPEED "Speed"
#define D_CMND_WAKEUP "Wakeup"
#define D_CMND_WAKEUPDURATION "WakeUpDuration"
#define D_CMND_WIDTH "Width"
// Commands xsns_hlw8012.ino
#define D_CMND_POWERLOW "PowerLow"

View File

@ -643,24 +643,26 @@
#define D_CMND_IRHVAC "IRHVAC"
// Commands xdrv_snfbridge.ino
#define D_CMND_RFCODE "RfCode"
#define D_CMND_RFHIGH "RfHigh"
#define D_CMND_RFHOST "RfHost"
#define D_CMND_RFKEY "RfKey"
#define D_CMND_RFDEFAULT "RfDefault"
#define D_CMND_RFLOW "RfLow"
#define D_CMND_RFSYNC "RfSync"
// Commands xdrv_snfled.ino
#define D_CMND_COLOR "Color"
#define D_CMND_COLORTEMPERATURE "CT"
#define D_CMND_DIMMER "Dimmer"
#define D_CMND_LED "Led"
#define D_CMND_LEDTABLE "LedTable"
#define D_CMND_FADE "Fade"
#define D_CMND_SPEED "Speed"
#define D_CMND_WAKEUPDURATION "WakeUpDuration"
#define D_CMND_WAKEUP "Wakeup"
// Commands xdrv_ws2812.ino
#define D_CMND_PIXELS "Pixels"
#define D_CMND_LED "Led"
#define D_CMND_WIDTH "Width"
#define D_CMND_SCHEME "Scheme"
#define D_CMND_SPEED "Speed"
#define D_CMND_WAKEUP "Wakeup"
#define D_CMND_WAKEUPDURATION "WakeUpDuration"
#define D_CMND_WIDTH "Width"
// Commands xsns_hlw8012.ino
#define D_CMND_POWERLOW "PowerLow"

View File

@ -643,24 +643,26 @@
#define D_CMND_IRHVAC "IRHVAC"
// Commands xdrv_snfbridge.ino
#define D_CMND_RFCODE "RfCode"
#define D_CMND_RFHIGH "RfHigh"
#define D_CMND_RFHOST "RfHost"
#define D_CMND_RFKEY "RfKey"
#define D_CMND_RFDEFAULT "RfDefault"
#define D_CMND_RFLOW "RfLow"
#define D_CMND_RFSYNC "RfSync"
// Commands xdrv_snfled.ino
#define D_CMND_COLOR "Color"
#define D_CMND_COLORTEMPERATURE "CT"
#define D_CMND_DIMMER "Dimmer"
#define D_CMND_LED "Led"
#define D_CMND_LEDTABLE "LedTable"
#define D_CMND_FADE "Fade"
#define D_CMND_SPEED "Speed"
#define D_CMND_WAKEUPDURATION "WakeUpDuration"
#define D_CMND_WAKEUP "Wakeup"
// Commands xdrv_ws2812.ino
#define D_CMND_PIXELS "Pixels"
#define D_CMND_LED "Led"
#define D_CMND_WIDTH "Width"
#define D_CMND_SCHEME "Scheme"
#define D_CMND_SPEED "Speed"
#define D_CMND_WAKEUP "Wakeup"
#define D_CMND_WAKEUPDURATION "WakeUpDuration"
#define D_CMND_WIDTH "Width"
// Commands xsns_hlw8012.ino
#define D_CMND_POWERLOW "PowerLow"

View File

@ -185,12 +185,12 @@
#define D_TRUE "Prawda"
#define D_TYPE "Typ"
#define D_UNKNOWN "Nieznany"
#define D_UPGRADE "Aktualizacji"
#define D_UPGRADE "aktualizacji"
#define D_UPLOAD "Wgraj"
#define D_UPTIME "Uptime"
#define D_USER "Uzytkownik"
#define D_UTC_TIME "UTC"
#define D_UV_LEVEL "UV Level"
#define D_UV_LEVEL "Poziom UV"
#define D_VCC "VCC"
#define D_VERSION "Wersja"
#define D_VOLTAGE "Napiecie"
@ -216,7 +216,7 @@
#define D_SET_BAUDRATE_TO "Ustaw szybkosc transmisji na"
#define D_RECEIVED_TOPIC "Otrzymany temat"
#define D_DATA_SIZE "Wielkosc danych"
#define D_ANALOG_INPUT0 "Wejscie analogowe-0"
#define D_ANALOG_INPUT0 "Wej_analogowe"
#define D_FINGERPRINT "Weryfikuj odcisk TLS..."
#define D_TLS_CONNECT_FAILED_TO "Nieudane polaczenie TLS do"
@ -643,24 +643,26 @@
#define D_CMND_IRHVAC "IRHVAC"
// Commands xdrv_snfbridge.ino
#define D_CMND_RFCODE "RfCode"
#define D_CMND_RFHIGH "RfHigh"
#define D_CMND_RFHOST "RfHost"
#define D_CMND_RFKEY "RfKey"
#define D_CMND_RFDEFAULT "RfDefault"
#define D_CMND_RFLOW "RfLow"
#define D_CMND_RFSYNC "RfSync"
// Commands xdrv_snfled.ino
#define D_CMND_COLOR "Color"
#define D_CMND_COLORTEMPERATURE "CT"
#define D_CMND_DIMMER "Dimmer"
#define D_CMND_LED "Led"
#define D_CMND_LEDTABLE "LedTable"
#define D_CMND_FADE "Fade"
#define D_CMND_SPEED "Speed"
#define D_CMND_WAKEUPDURATION "WakeUpDuration"
#define D_CMND_WAKEUP "Wakeup"
// Commands xdrv_ws2812.ino
#define D_CMND_PIXELS "Pixels"
#define D_CMND_LED "Led"
#define D_CMND_WIDTH "Width"
#define D_CMND_SCHEME "Scheme"
#define D_CMND_SPEED "Speed"
#define D_CMND_WAKEUP "Wakeup"
#define D_CMND_WAKEUPDURATION "WakeUpDuration"
#define D_CMND_WIDTH "Width"
// Commands xsns_hlw8012.ino
#define D_CMND_POWERLOW "PowerLow"

View File

@ -21,8 +21,6 @@
#define DOMOTICZ_UPDATE_TIMER 0 // [DomoticzUpdateTimer] Send relay status (0 = disable, 1 - 3600 seconds) (Optional)
#endif
const uint8_t kDefaultRfCode[9] PROGMEM = { 0x21, 0x16, 0x01, 0x0E, 0x03, 0x48, 0x2E, 0x1A, 0x00 };
/*********************************************************************************************\
* RTC memory
\*********************************************************************************************/

View File

@ -77,7 +77,7 @@ typedef unsigned long power_t; // Power (Relay) type
#define OTA_ATTEMPTS 10 // Number of times to try fetching the new firmware
#define INPUT_BUFFER_SIZE 250 // Max number of characters in (serial) command buffer
#define CMDSZ 20 // Max number of characters in command
#define CMDSZ 24 // Max number of characters in command
#define TOPSZ 100 // Max number of characters in topic string
#ifdef USE_MQTT_TLS
#define MAX_LOG_LINES 10 // Max number of lines in weblog
@ -111,4 +111,6 @@ 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};
const uint8_t kDefaultRfCode[9] PROGMEM = { 0x21, 0x16, 0x01, 0x0E, 0x03, 0x48, 0x2E, 0x1A, 0x00 };
#endif // _SONOFF_H_

View File

@ -25,7 +25,7 @@
- Select IDE Tools - Flash Size: "1M (no SPIFFS)"
====================================================*/
#define VERSION 0x05080010 // 5.8.0p
#define VERSION 0x05080011 // 5.8.0q
// Location specific includes
#include "sonoff.h" // Enumaration used in user_config.h
@ -64,6 +64,46 @@
// Structs
#include "settings.h"
enum TasmotaCommands {
CMND_BACKLOG, CMND_DELAY, CMND_POWER, CMND_STATUS, CMND_POWERONSTATE, CMND_PULSETIME,
CMND_BLINKTIME, CMND_BLINKCOUNT, CMND_SAVEDATA, CMND_SETOPTION, CMND_TEMPERATURE_RESOLUTION, CMND_HUMIDITY_RESOLUTION,
CMND_PRESSURE_RESOLUTION, CMND_POWER_RESOLUTION, CMND_VOLTAGE_RESOLUTION, CMND_ENERGY_RESOLUTION, CMND_MODULE, CMND_MODULES,
CMND_GPIO, CMND_GPIOS, CMND_PWM, CMND_PWMFREQUENCY, CMND_PWMRANGE, CMND_COUNTER, CMND_COUNTERTYPE,
CMND_COUNTERDEBOUNCE, CMND_SLEEP, CMND_UPGRADE, CMND_UPLOAD, CMND_OTAURL, CMND_SERIALLOG, CMND_SYSLOG,
CMND_LOGHOST, CMND_LOGPORT, CMND_IPADDRESS, CMND_NTPSERVER, CMND_AP, CMND_SSID, CMND_PASSWORD, CMND_HOSTNAME,
CMND_WIFICONFIG, CMND_FRIENDLYNAME, CMND_SWITCHMODE, CMND_WEBSERVER, CMND_WEBPASSWORD, CMND_WEBLOG, CMND_EMULATION,
CMND_TELEPERIOD, CMND_RESTART, CMND_RESET, CMND_TIMEZONE, CMND_ALTITUDE, CMND_LEDPOWER, CMND_LEDSTATE,
CMND_CFGDUMP, CMND_I2CSCAN, CMND_EXCEPTION };
const char kTasmotaCommands[] PROGMEM =
D_CMND_BACKLOG "|" D_CMND_DELAY "|" D_CMND_POWER "|" D_CMND_STATUS "|" D_CMND_POWERONSTATE "|" D_CMND_PULSETIME "|"
D_CMND_BLINKTIME "|" D_CMND_BLINKCOUNT "|" D_CMND_SAVEDATA "|" D_CMND_SETOPTION "|" D_CMND_TEMPERATURE_RESOLUTION "|" D_CMND_HUMIDITY_RESOLUTION "|"
D_CMND_PRESSURE_RESOLUTION "|" D_CMND_POWER_RESOLUTION "|" D_CMND_VOLTAGE_RESOLUTION "|" D_CMND_ENERGY_RESOLUTION "|" D_CMND_MODULE "|" D_CMND_MODULES "|"
D_CMND_GPIO "|" D_CMND_GPIOS "|" D_CMND_PWM "|" D_CMND_PWMFREQUENCY "|" D_CMND_PWMRANGE "|" D_CMND_COUNTER "|" D_CMND_COUNTERTYPE "|"
D_CMND_COUNTERDEBOUNCE "|" D_CMND_SLEEP "|" D_CMND_UPGRADE "|" D_CMND_UPLOAD "|" D_CMND_OTAURL "|" D_CMND_SERIALLOG "|" D_CMND_SYSLOG "|"
D_CMND_LOGHOST "|" D_CMND_LOGPORT "|" D_CMND_IPADDRESS "|" D_CMND_NTPSERVER "|" D_CMND_AP "|" D_CMND_SSID "|" D_CMND_PASSWORD "|" D_CMND_HOSTNAME "|"
D_CMND_WIFICONFIG "|" D_CMND_FRIENDLYNAME "|" D_CMND_SWITCHMODE "|" D_CMND_WEBSERVER "|" D_CMND_WEBPASSWORD "|" D_CMND_WEBLOG "|" D_CMND_EMULATION "|"
D_CMND_TELEPERIOD "|" D_CMND_RESTART "|" D_CMND_RESET "|" D_CMND_TIMEZONE "|" D_CMND_ALTITUDE "|" D_CMND_LEDPOWER "|" D_CMND_LEDSTATE "|"
D_CMND_CFGDUMP "|" D_CMND_I2CSCAN
#ifdef DEBUG_THEO
"|" D_CMND_EXCEPTION
#endif
;
enum MqttCommands {
CMND_MQTTHOST, CMND_MQTTPORT, CMND_MQTTRETRY, CMND_STATETEXT, CMND_MQTTFINGERPRINT, CMND_MQTTCLIENT,
CMND_MQTTUSER, CMND_MQTTPASSWORD, CMND_FULLTOPIC, CMND_PREFIX, CMND_GROUPTOPIC, CMND_TOPIC,
CMND_BUTTONTOPIC, CMND_SWITCHTOPIC, CMND_BUTTONRETAIN, CMND_SWITCHRETAIN, CMND_POWERRETAIN, CMND_SENSORRETAIN };
const char kMqttCommands[] PROGMEM =
D_CMND_MQTTHOST "|" D_CMND_MQTTPORT "|" D_CMND_MQTTRETRY "|" D_CMND_STATETEXT "|" D_CMND_MQTTFINGERPRINT "|" D_CMND_MQTTCLIENT "|"
D_CMND_MQTTUSER "|" D_CMND_MQTTPASSWORD "|" D_CMND_FULLTOPIC "|" D_CMND_PREFIX "|" D_CMND_GROUPTOPIC "|" D_CMND_TOPIC "|"
D_CMND_BUTTONTOPIC "|" D_CMND_SWITCHTOPIC "|" D_CMND_BUTTONRETAIN "|" D_CMND_SWITCHRETAIN "|" D_CMND_POWERRETAIN "|" D_CMND_SENSORRETAIN ;
const char kOptionOff[] PROGMEM = "OFF|" D_OFF "|" D_FALSE "|" D_STOP "|" D_CELSIUS ;
const char kOptionOn[] PROGMEM = "ON|" D_ON "|" D_TRUE "|" D_START "|" D_FAHRENHEIT "|" D_USER ;
const char kOptionToggle[] PROGMEM = "TOGGLE|" D_TOGGLE "|" D_ADMIN ;
const char kOptionBlink[] PROGMEM = "BLINK|" D_BLINK ;
const char kOptionBlinkOff[] PROGMEM = "BLINKOFF|" D_BLINKOFF ;
// Global variables
int baudrate = APP_BAUDRATE; // Serial interface baud rate
byte serial_in_byte; // Received byte
@ -525,34 +565,36 @@ void MqttReconnect()
boolean MqttCommand(boolean grpflg, char *type, uint16_t index, char *dataBuf, uint16_t data_len, int16_t payload, uint16_t payload16)
{
char command [CMDSZ];
boolean serviced = true;
char stemp1[TOPSZ];
char stemp2[10];
char scommand[CMDSZ];
uint16_t i;
if (!strcasecmp_P(type, PSTR(D_CMND_MQTTHOST))) {
int command_code = GetCommandCode(command, sizeof(command), type, kMqttCommands);
if (CMND_MQTTHOST == command_code) {
if ((data_len > 0) && (data_len < sizeof(Settings.mqtt_host))) {
strlcpy(Settings.mqtt_host, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? MQTT_HOST : dataBuf, sizeof(Settings.mqtt_host));
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTHOST "\":\"%s\"}"), Settings.mqtt_host);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.mqtt_host);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_MQTTPORT))) {
else if (CMND_MQTTPORT == command_code) {
if (payload16 > 0) {
Settings.mqtt_port = (1 == payload16) ? MQTT_PORT : payload16;
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTPORT "\":%d}"), Settings.mqtt_port);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.mqtt_port);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_MQTTRETRY))) {
else if (CMND_MQTTRETRY == command_code) {
if ((payload >= MQTT_RETRY_SECS) && (payload < 32001)) {
Settings.mqtt_retry = payload;
mqtt_retry_counter = Settings.mqtt_retry;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTRETRY "\":%d}"), Settings.mqtt_retry);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.mqtt_retry);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_STATETEXT)) && (index > 0) && (index <= 4)) {
else if ((CMND_STATETEXT == command_code) && (index > 0) && (index <= 4)) {
if ((data_len > 0) && (data_len < sizeof(Settings.state_text[0]))) {
for(i = 0; i <= data_len; i++) {
if (dataBuf[i] == ' ') {
@ -561,39 +603,39 @@ boolean MqttCommand(boolean grpflg, char *type, uint16_t index, char *dataBuf, u
}
strlcpy(Settings.state_text[index -1], dataBuf, sizeof(Settings.state_text[0]));
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_STATETEXT "%d\":\"%s\"}"), index, GetStateText(index -1));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, GetStateText(index -1));
}
#ifdef USE_MQTT_TLS
else if (!strcasecmp_P(type, PSTR(D_CMND_MQTTFINGERPRINT))) {
else if (CMND_MQTTFINGERPRINT == command_code) {
if ((data_len > 0) && (data_len < sizeof(Settings.mqtt_fingerprint))) {
strlcpy(Settings.mqtt_fingerprint, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? MQTT_FINGERPRINT : dataBuf, sizeof(Settings.mqtt_fingerprint));
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTFINGERPRINT "\":\"%s\"}"), Settings.mqtt_fingerprint);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.mqtt_fingerprint);
}
#endif
else if (!grpflg && !strcasecmp_P(type, PSTR(D_CMND_MQTTCLIENT))) {
else if ((CMND_MQTTCLIENT == command_code) && !grpflg) {
if ((data_len > 0) && (data_len < sizeof(Settings.mqtt_client))) {
strlcpy(Settings.mqtt_client, (1 == payload) ? MQTT_CLIENT_ID : dataBuf, sizeof(Settings.mqtt_client));
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTCLIENT "\":\"%s\"}"), Settings.mqtt_client);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.mqtt_client);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_MQTTUSER))) {
else if (CMND_MQTTUSER == command_code) {
if ((data_len > 0) && (data_len < sizeof(Settings.mqtt_user))) {
strlcpy(Settings.mqtt_user, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? MQTT_USER : dataBuf, sizeof(Settings.mqtt_user));
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTUSER "\":\"%s\"}"), Settings.mqtt_user);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.mqtt_user);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_MQTTPASSWORD))) {
else if (CMND_MQTTPASSWORD == command_code) {
if ((data_len > 0) && (data_len < sizeof(Settings.mqtt_pwd))) {
strlcpy(Settings.mqtt_pwd, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? MQTT_PASS : dataBuf, sizeof(Settings.mqtt_pwd));
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MQTTPASSWORD "\":\"%s\"}"), Settings.mqtt_pwd);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.mqtt_pwd);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_FULLTOPIC))) {
else if (CMND_FULLTOPIC == command_code) {
if ((data_len > 0) && (data_len < sizeof(Settings.mqtt_fulltopic))) {
MakeValidMqtt(1, dataBuf);
if (!strcmp(dataBuf, mqtt_client)) {
@ -607,18 +649,18 @@ boolean MqttCommand(boolean grpflg, char *type, uint16_t index, char *dataBuf, u
restart_flag = 2;
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_FULLTOPIC "\":\"%s\"}"), Settings.mqtt_fulltopic);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.mqtt_fulltopic);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_PREFIX)) && (index > 0) && (index <= 3)) {
else if ((CMND_PREFIX == command_code) && (index > 0) && (index <= 3)) {
if ((data_len > 0) && (data_len < sizeof(Settings.mqtt_prefix[0]))) {
MakeValidMqtt(0, dataBuf);
strlcpy(Settings.mqtt_prefix[index -1], (1 == payload) ? (1==index)?SUB_PREFIX:(2==index)?PUB_PREFIX:PUB_PREFIX2 : dataBuf, sizeof(Settings.mqtt_prefix[0]));
// if (Settings.mqtt_prefix[index -1][strlen(Settings.mqtt_prefix[index -1])] == '/') Settings.mqtt_prefix[index -1][strlen(Settings.mqtt_prefix[index -1])] = 0;
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PREFIX "%d\":\"%s\"}"), index, Settings.mqtt_prefix[index -1]);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, Settings.mqtt_prefix[index -1]);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_GROUPTOPIC))) {
else if (CMND_GROUPTOPIC == command_code) {
if ((data_len > 0) && (data_len < sizeof(Settings.mqtt_grptopic))) {
MakeValidMqtt(0, dataBuf);
if (!strcmp(dataBuf, mqtt_client)) {
@ -627,9 +669,9 @@ boolean MqttCommand(boolean grpflg, char *type, uint16_t index, char *dataBuf, u
strlcpy(Settings.mqtt_grptopic, (1 == payload) ? MQTT_GRPTOPIC : dataBuf, sizeof(Settings.mqtt_grptopic));
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_GROUPTOPIC "\":\"%s\"}"), Settings.mqtt_grptopic);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.mqtt_grptopic);
}
else if (!grpflg && !strcasecmp_P(type, PSTR(D_CMND_TOPIC))) {
else if ((CMND_TOPIC == command_code) && !grpflg) {
if ((data_len > 0) && (data_len < sizeof(Settings.mqtt_topic))) {
MakeValidMqtt(0, dataBuf);
if (!strcmp(dataBuf, mqtt_client)) {
@ -643,9 +685,9 @@ boolean MqttCommand(boolean grpflg, char *type, uint16_t index, char *dataBuf, u
restart_flag = 2;
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_TOPIC "\":\"%s\"}"), Settings.mqtt_topic);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.mqtt_topic);
}
else if (!grpflg && !strcasecmp_P(type, PSTR(D_CMND_BUTTONTOPIC))) {
else if ((CMND_BUTTONTOPIC == command_code) && !grpflg) {
if ((data_len > 0) && (data_len < sizeof(Settings.button_topic))) {
MakeValidMqtt(0, dataBuf);
if (!strcmp(dataBuf, mqtt_client)) {
@ -653,9 +695,9 @@ boolean MqttCommand(boolean grpflg, char *type, uint16_t index, char *dataBuf, u
}
strlcpy(Settings.button_topic, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? Settings.mqtt_topic : dataBuf, sizeof(Settings.button_topic));
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_BUTTONTOPIC "\":\"%s\"}"), Settings.button_topic);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.button_topic);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SWITCHTOPIC))) {
else if (CMND_SWITCHTOPIC == command_code) {
if ((data_len > 0) && (data_len < sizeof(Settings.switch_topic))) {
MakeValidMqtt(0, dataBuf);
if (!strcmp(dataBuf, mqtt_client)) {
@ -663,9 +705,9 @@ boolean MqttCommand(boolean grpflg, char *type, uint16_t index, char *dataBuf, u
}
strlcpy(Settings.switch_topic, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? Settings.mqtt_topic : dataBuf, sizeof(Settings.switch_topic));
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SWITCHTOPIC "\":\"%s\"}"), Settings.switch_topic);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.switch_topic);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_BUTTONRETAIN))) {
else if (CMND_BUTTONRETAIN == command_code) {
if ((payload >= 0) && (payload <= 1)) {
strlcpy(Settings.button_topic, Settings.mqtt_topic, sizeof(Settings.button_topic));
if (!payload) {
@ -675,9 +717,9 @@ boolean MqttCommand(boolean grpflg, char *type, uint16_t index, char *dataBuf, u
}
Settings.flag.mqtt_button_retain = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_BUTTONRETAIN "\":\"%s\"}"), GetStateText(Settings.flag.mqtt_button_retain));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, GetStateText(Settings.flag.mqtt_button_retain));
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SWITCHRETAIN))) {
else if (CMND_SWITCHRETAIN == command_code) {
if ((payload >= 0) && (payload <= 1)) {
strlcpy(Settings.button_topic, Settings.mqtt_topic, sizeof(Settings.button_topic));
if (!payload) {
@ -687,9 +729,9 @@ boolean MqttCommand(boolean grpflg, char *type, uint16_t index, char *dataBuf, u
}
Settings.flag.mqtt_switch_retain = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SWITCHRETAIN "\":\"%s\"}"), GetStateText(Settings.flag.mqtt_switch_retain));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, GetStateText(Settings.flag.mqtt_switch_retain));
}
else if (!strcasecmp_P(type, PSTR(D_CMND_POWERRETAIN))) {
else if (CMND_POWERRETAIN == command_code) {
if ((payload >= 0) && (payload <= 1)) {
if (!payload) {
for(i = 1; i <= devices_present; i++) { // Clear MQTT retain in broker
@ -700,9 +742,9 @@ boolean MqttCommand(boolean grpflg, char *type, uint16_t index, char *dataBuf, u
}
Settings.flag.mqtt_power_retain = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_POWERRETAIN "\":\"%s\"}"), GetStateText(Settings.flag.mqtt_power_retain));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, GetStateText(Settings.flag.mqtt_power_retain));
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SENSORRETAIN))) {
else if (CMND_SENSORRETAIN == command_code) {
if ((payload >= 0) && (payload <= 1)) {
if (!payload) {
mqtt_data[0] = '\0';
@ -711,7 +753,7 @@ boolean MqttCommand(boolean grpflg, char *type, uint16_t index, char *dataBuf, u
}
Settings.flag.mqtt_sensor_retain = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SENSORRETAIN "\":\"%s\"}"), GetStateText(Settings.flag.mqtt_sensor_retain));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, GetStateText(Settings.flag.mqtt_sensor_retain));
}
#ifdef USE_DOMOTICZ
@ -745,6 +787,7 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
char topicBuf[TOPSZ];
char dataBuf[data_len+1];
char command [CMDSZ];
char stemp1[TOPSZ];
char *p;
char *mtopic = NULL;
@ -821,26 +864,27 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
backlog_delay = MIN_BACKLOG_DELAY; // Reset backlog delay
if (!strcasecmp_P(dataBuf, PSTR("OFF")) || !strcasecmp_P(dataBuf, PSTR(D_OFF)) || !strcasecmp_P(dataBuf, Settings.state_text[0]) || !strcasecmp_P(dataBuf, PSTR(D_FALSE)) || !strcasecmp_P(dataBuf, PSTR(D_STOP)) || !strcasecmp_P(dataBuf, PSTR(D_CELSIUS))) {
if ((GetCommandCode(command, sizeof(command), dataBuf, kOptionOff) >= 0) || !strcasecmp(dataBuf, Settings.state_text[0])) {
payload = 0;
}
if (!strcasecmp_P(dataBuf, PSTR("ON")) || !strcasecmp_P(dataBuf, PSTR(D_ON)) || !strcasecmp_P(dataBuf, Settings.state_text[1]) || !strcasecmp_P(dataBuf, PSTR(D_TRUE)) || !strcasecmp_P(dataBuf, PSTR(D_START)) || !strcasecmp_P(dataBuf, PSTR(D_FAHRENHEIT)) || !strcasecmp_P(dataBuf, PSTR(D_USER))) {
if ((GetCommandCode(command, sizeof(command), dataBuf, kOptionOn) >= 0) || !strcasecmp(dataBuf, Settings.state_text[1])) {
payload = 1;
}
if (!strcasecmp_P(dataBuf, PSTR("TOGGLE")) || !strcasecmp_P(dataBuf, PSTR(D_TOGGLE)) || !strcasecmp_P(dataBuf, Settings.state_text[2]) || !strcasecmp_P(dataBuf, PSTR(D_ADMIN))) {
if ((GetCommandCode(command, sizeof(command), dataBuf, kOptionToggle) >= 0) || !strcasecmp(dataBuf, Settings.state_text[2])) {
payload = 2;
}
if (!strcasecmp_P(dataBuf, PSTR("BLINK")) || !strcasecmp_P(dataBuf, PSTR(D_BLINK))) {
if (GetCommandCode(command, sizeof(command), dataBuf, kOptionBlink) >= 0) {
payload = 3;
}
if (!strcasecmp_P(dataBuf, PSTR("BLINKOFF")) || !strcasecmp_P(dataBuf, PSTR(D_BLINKOFF))) {
if (GetCommandCode(command, sizeof(command), dataBuf, kOptionBlinkOff) >= 0) {
payload = 4;
}
// snprintf_P(log_data, sizeof(log_data), PSTR("RSLT: Payload %d, Payload16 %d"), payload, payload16);
// AddLog(LOG_LEVEL_DEBUG);
if (!strcasecmp_P(type, PSTR(D_CMND_BACKLOG))) {
int command_code = GetCommandCode(command, sizeof(command), type, kTasmotaCommands);
if (CMND_BACKLOG == command_code) {
if (data_len) {
char *blcommand = strtok(dataBuf, ";");
while (blcommand != NULL) {
@ -854,20 +898,20 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
backlog_index &= 0xF;
blcommand = strtok(NULL, ";");
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_BACKLOG "\":\"" D_APPENDED "\"}"));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_APPENDED);
} else {
uint8_t blflag = (backlog_pointer == backlog_index);
backlog_pointer = backlog_index;
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_BACKLOG "\":\"%s\"}"), blflag ? D_EMPTY : D_ABORTED);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, blflag ? D_EMPTY : D_ABORTED);
}
}
else if (!strcasecmp_P(type, PSTR(D_CMND_DELAY))) {
else if (CMND_DELAY == command_code) {
if ((payload >= MIN_BACKLOG_DELAY) && (payload <= 3600)) {
backlog_delay = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DELAY "\":%d}"), backlog_delay);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, backlog_delay);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_POWER)) && (index > 0) && (index <= devices_present)) {
else if ((CMND_POWER == command_code) && (index > 0) && (index <= devices_present)) {
if ((payload < 0) || (payload > 4)) {
payload = 9;
}
@ -875,7 +919,7 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
fallback_topic_flag = 0;
return;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_STATUS))) {
else if (CMND_STATUS == command_code) {
if ((payload < 0) || (payload > MAX_STATUS)) {
payload = 99;
}
@ -883,7 +927,7 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
fallback_topic_flag = 0;
return;
}
else if ((Settings.module != MOTOR) && !strcasecmp_P(type, PSTR(D_CMND_POWERONSTATE))) {
else if ((CMND_POWERONSTATE == command_code) && (Settings.module != MOTOR)) {
/* 0 = Keep relays off after power on
* 1 = Turn relays on after power on
* 2 = Toggle relays after power on
@ -898,37 +942,37 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_POWERONSTATE "\":%d}"), Settings.poweronstate);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.poweronstate);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_PULSETIME)) && (index > 0) && (index <= MAX_PULSETIMERS)) {
else if ((CMND_PULSETIME == command_code) && (index > 0) && (index <= MAX_PULSETIMERS)) {
if (data_len > 0) {
Settings.pulse_timer[index -1] = payload16; // 0 - 65535
pulse_timer[index -1] = 0;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PULSETIME "%d\":%d}"), index, Settings.pulse_timer[index -1]);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_NVALUE, command, index, Settings.pulse_timer[index -1]);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_BLINKTIME))) {
else if (CMND_BLINKTIME == command_code) {
if ((payload > 2) && (payload <= 3600)) {
Settings.blinktime = payload;
if (blink_timer) {
blink_timer = Settings.blinktime;
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_BLINKTIME "\":%d}"), Settings.blinktime);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.blinktime);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_BLINKCOUNT))) {
else if (CMND_BLINKCOUNT == command_code) {
if (data_len > 0) {
Settings.blinkcount = payload16; // 0 - 65535
if (blink_counter) {
blink_counter = Settings.blinkcount *2;
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_BLINKCOUNT "\":%d}"), Settings.blinkcount);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.blinkcount);
}
else if (light_type && LightCommand(type, index, dataBuf, data_len, payload)) {
// Serviced
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SAVEDATA))) {
else if (CMND_SAVEDATA == command_code) {
if ((payload >= 0) && (payload <= 3600)) {
Settings.save_data = payload;
save_data_counter = Settings.save_data;
@ -940,9 +984,9 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
if (Settings.save_data > 1) {
snprintf_P(stemp1, sizeof(stemp1), PSTR(D_EVERY " %d " D_UNIT_SECOND), Settings.save_data);
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SAVEDATA "\":\"%s\"}"), (Settings.save_data > 1) ? stemp1 : GetStateText(Settings.save_data));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, (Settings.save_data > 1) ? stemp1 : GetStateText(Settings.save_data));
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SETOPTION)) && ((index >= 0) && (index <= 17)) || ((index > 31) && (index <= P_MAX_PARAM8 +31))) {
else if ((CMND_SETOPTION == command_code) && ((index >= 0) && (index <= 17)) || ((index > 31) && (index <= P_MAX_PARAM8 +31))) {
if (index <= 31) {
ptype = 0; // SetOption0 .. 31
} else {
@ -994,45 +1038,45 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
if (ptype) {
snprintf_P(stemp1, sizeof(stemp1), PSTR("%d"), Settings.param[index]);
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SETOPTION "%d\":\"%s\"}"), (ptype) ? index +32 : index, (ptype) ? stemp1 : GetStateText(bitRead(Settings.flag.data, index)));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, (ptype) ? index +32 : index, (ptype) ? stemp1 : GetStateText(bitRead(Settings.flag.data, index)));
}
else if (!strcasecmp_P(type, PSTR(D_CMND_TEMPERATURE_RESOLUTION))) {
else if (CMND_TEMPERATURE_RESOLUTION == command_code) {
if ((payload >= 0) && (payload <= 3)) {
Settings.flag.temperature_resolution = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_TEMPERATURE_RESOLUTION "\":%d}"), Settings.flag.temperature_resolution);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.flag.temperature_resolution);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_HUMIDITY_RESOLUTION))) {
else if (CMND_HUMIDITY_RESOLUTION == command_code) {
if ((payload >= 0) && (payload <= 3)) {
Settings.flag.humidity_resolution = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_HUMIDITY_RESOLUTION "\":%d}"), Settings.flag.humidity_resolution);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.flag.humidity_resolution);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_PRESSURE_RESOLUTION))) {
else if (CMND_PRESSURE_RESOLUTION == command_code) {
if ((payload >= 0) && (payload <= 3)) {
Settings.flag.pressure_resolution = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PRESSURE_RESOLUTION "\":%d}"), Settings.flag.pressure_resolution);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.flag.pressure_resolution);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_POWER_RESOLUTION))) {
else if (CMND_POWER_RESOLUTION == command_code) {
if ((payload >= 0) && (payload <= 1)) {
Settings.flag.wattage_resolution = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_POWER_RESOLUTION "\":%d}"), Settings.flag.wattage_resolution);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.flag.wattage_resolution);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_VOLTAGE_RESOLUTION))) {
else if (CMND_VOLTAGE_RESOLUTION == command_code) {
if ((payload >= 0) && (payload <= 1)) {
Settings.flag.voltage_resolution = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_VOLTAGE_RESOLUTION "\":%d}"), Settings.flag.voltage_resolution);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.flag.voltage_resolution);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_ENERGY_RESOLUTION))) {
else if (CMND_ENERGY_RESOLUTION == command_code) {
if ((payload >= 0) && (payload <= 5)) {
Settings.flag.energy_resolution = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_ENERGY_RESOLUTION "\":%d}"), Settings.flag.energy_resolution);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.flag.energy_resolution);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_MODULE))) {
else if (CMND_MODULE == command_code) {
if ((payload > 0) && (payload <= MAXMODULE)) {
payload--;
byte new_modflg = (Settings.module != payload);
@ -1045,9 +1089,9 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
restart_flag = 2;
}
snprintf_P(stemp1, sizeof(stemp1), kModules[Settings.module].name);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MODULE "\":\"%d (%s)\"}"), Settings.module +1, stemp1);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE_SVALUE, command, Settings.module +1, stemp1);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_MODULES))) {
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);
@ -1066,7 +1110,7 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
mqtt_data[0] = '\0';
}
else if (!strcasecmp_P(type, PSTR(D_CMND_GPIO)) && (index < MAX_GPIO_PIN)) {
else if ((CMND_GPIO == command_code) && (index < MAX_GPIO_PIN)) {
mytmplt cmodule;
memcpy_P(&cmodule, &kModules[Settings.module], sizeof(cmodule));
if ((GPIO_USER == cmodule.gp.io[index]) && (payload >= 0) && (payload < GPIO_SENSOR_END)) {
@ -1093,10 +1137,10 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
if (jsflg) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_GPIO "\":\"" D_NOT_SUPPORTED "\"}"));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_NOT_SUPPORTED);
}
}
else if (!strcasecmp_P(type, PSTR(D_CMND_GPIOS))) {
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);
@ -1115,7 +1159,7 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
mqtt_data[0] = '\0';
}
else if (!light_type && !strcasecmp_P(type, PSTR(D_CMND_PWM)) && (index > 0) && (index <= MAX_PWMS)) {
else if ((CMND_PWM == command_code) && !light_type && (index > 0) && (index <= MAX_PWMS)) {
if ((payload >= 0) && (payload <= Settings.pwm_range) && (pin[GPIO_PWM1 + index -1] < 99)) {
Settings.pwm_value[index -1] = payload;
analogWrite(pin[GPIO_PWM1 + index -1], bitRead(pwm_inverted, index -1) ? Settings.pwm_range - payload : payload);
@ -1130,14 +1174,14 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}}"),mqtt_data);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_PWMFREQUENCY))) {
else if (CMND_PWMFREQUENCY == command_code) {
if ((1 == payload) || ((payload >= 100) && (payload <= 4000))) {
Settings.pwm_frequency = (1 == payload) ? PWM_FREQ : payload;
analogWriteFreq(Settings.pwm_frequency); // Default is 1000 (core_esp8266_wiring_pwm.c)
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PWMFREQUENCY "\":%d}"), Settings.pwm_frequency);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.pwm_frequency);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_PWMRANGE))) {
else if (CMND_PWMRANGE == command_code) {
if ((1 == payload) || ((payload > 254) && (payload < 1024))) {
Settings.pwm_range = (1 == payload) ? PWM_RANGE : payload;
for (byte i; i < MAX_PWMS; i++) {
@ -1147,30 +1191,30 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
analogWriteRange(Settings.pwm_range); // Default is 1023 (Arduino.h)
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PWMRANGE "\":%d}"), Settings.pwm_range);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.pwm_range);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_COUNTER)) && (index > 0) && (index <= MAX_COUNTERS)) {
else if ((CMND_COUNTER == command_code) && (index > 0) && (index <= MAX_COUNTERS)) {
if ((data_len > 0) && (pin[GPIO_CNTR1 + index -1] < 99)) {
RtcSettings.pulse_counter[index -1] = payload16;
Settings.pulse_counter[index -1] = payload16;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_COUNTER "%d\":%d}"), index, RtcSettings.pulse_counter[index -1]);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_NVALUE, command, index, RtcSettings.pulse_counter[index -1]);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_COUNTERTYPE)) && (index > 0) && (index <= MAX_COUNTERS)) {
else if ((CMND_COUNTERTYPE == command_code) && (index > 0) && (index <= MAX_COUNTERS)) {
if ((payload >= 0) && (payload <= 1) && (pin[GPIO_CNTR1 + index -1] < 99)) {
bitWrite(Settings.pulse_counter_type, index -1, payload &1);
RtcSettings.pulse_counter[index -1] = 0;
Settings.pulse_counter[index -1] = 0;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_COUNTERTYPE "%d\":%d}"), index, bitRead(Settings.pulse_counter_type, index -1));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_NVALUE, command, index, bitRead(Settings.pulse_counter_type, index -1));
}
else if (!strcasecmp_P(type, PSTR(D_CMND_COUNTERDEBOUNCE))) {
else if (CMND_COUNTERDEBOUNCE == command_code) {
if ((data_len > 0) && (payload16 < 32001)) {
Settings.pulse_counter_debounce = payload16;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_COUNTERDEBOUNCE "\":%d}"), Settings.pulse_counter_debounce);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.pulse_counter_debounce);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SLEEP))) {
else if (CMND_SLEEP == command_code) {
if ((payload >= 0) && (payload < 251)) {
if ((!Settings.sleep && payload) || (Settings.sleep && !payload)) {
restart_flag = 2;
@ -1178,62 +1222,62 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
Settings.sleep = payload;
sleep = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SLEEP "\":\"%d%s (%d%s)\"}"), sleep, (Settings.flag.value_units) ? " " D_UNIT_MILLISECOND : "", Settings.sleep, (Settings.flag.value_units) ? " " D_UNIT_MILLISECOND : "");
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE_UNIT_NVALUE_UNIT, command, sleep, (Settings.flag.value_units) ? " " D_UNIT_MILLISECOND : "", Settings.sleep, (Settings.flag.value_units) ? " " D_UNIT_MILLISECOND : "");
}
else if (!strcasecmp_P(type, PSTR(D_CMND_UPGRADE)) || !strcasecmp_P(type, PSTR(D_CMND_UPLOAD))) {
else if ((CMND_UPGRADE == command_code) || (CMND_UPLOAD == command_code)) {
// Check if the payload is numerically 1, and had no trailing chars.
// e.g. "1foo" or "1.2.3" could fool us.
// Check if the version we have been asked to upgrade to is higher than our current version.
// We also need at least 3 chars to make a valid version number string.
if (((1 == data_len) && (1 == payload)) || ((data_len >= 3) && NewerVersion(dataBuf))) {
ota_state_flag = 3;
snprintf_P(mqtt_data, sizeof(mqtt_data), "{\"" D_CMND_UPGRADE "\":\"" D_VERSION " %s " D_FROM " %s\"}", version, Settings.ota_url);
snprintf_P(mqtt_data, sizeof(mqtt_data), "{\"%s\":\"" D_VERSION " %s " D_FROM " %s\"}", command, version, Settings.ota_url);
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), "{\"" D_CMND_UPGRADE "\":\"" D_ONE_OR_GT "\"}", version);
snprintf_P(mqtt_data, sizeof(mqtt_data), "{\"%s\":\"" D_ONE_OR_GT "\"}", command, version);
}
}
else if (!strcasecmp_P(type, PSTR(D_CMND_OTAURL))) {
else if (CMND_OTAURL == command_code) {
if ((data_len > 0) && (data_len < sizeof(Settings.ota_url)))
strlcpy(Settings.ota_url, (1 == payload) ? OTA_URL : dataBuf, sizeof(Settings.ota_url));
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_OTAURL "\":\"%s\"}"), Settings.ota_url);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.ota_url);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SERIALLOG))) {
else if (CMND_SERIALLOG == command_code) {
if ((payload >= LOG_LEVEL_NONE) && (payload <= LOG_LEVEL_ALL)) {
Settings.seriallog_level = payload;
seriallog_level = payload;
seriallog_timer = 0;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SERIALLOG "\":\"%d (" D_ACTIVE " %d)\"}"), Settings.seriallog_level, seriallog_level);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE_ACTIVE_NVALUE, command, Settings.seriallog_level, seriallog_level);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SYSLOG))) {
else if (CMND_SYSLOG == command_code) {
if ((payload >= LOG_LEVEL_NONE) && (payload <= LOG_LEVEL_ALL)) {
Settings.syslog_level = payload;
syslog_level = (Settings.flag.emulation) ? 0 : payload;
syslog_timer = 0;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SYSLOG "\":\"%d (" D_ACTIVE " %d)\"}"), Settings.syslog_level, syslog_level);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE_ACTIVE_NVALUE, command, Settings.syslog_level, syslog_level);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_LOGHOST))) {
else if (CMND_LOGHOST == command_code) {
if ((data_len > 0) && (data_len < sizeof(Settings.syslog_host))) {
strlcpy(Settings.syslog_host, (1 == payload) ? SYS_LOG_HOST : dataBuf, sizeof(Settings.syslog_host));
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_LOGHOST "\":\"%s\"}"), Settings.syslog_host);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.syslog_host);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_LOGPORT))) {
else if (CMND_LOGPORT == command_code) {
if (payload16 > 0) {
Settings.syslog_port = (1 == payload16) ? SYS_LOG_PORT : payload16;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_LOGPORT "\":%d}"), Settings.syslog_port);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.syslog_port);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_IPADDRESS)) && (index > 0) && (index <= 4)) {
else if ((CMND_IPADDRESS == command_code) && (index > 0) && (index <= 4)) {
if (ParseIp(&address, dataBuf)) {
Settings.ip_address[index -1] = address;
// restart_flag = 2;
}
snprintf_P(stemp1, sizeof(stemp1), PSTR(" (%s)"), WiFi.localIP().toString().c_str());
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_IPADDRESS "%d\":\"%s%s\"}"), index, IPAddress(Settings.ip_address[index -1]).toString().c_str(), (1 == index) ? stemp1:"");
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE_SVALUE, command, index, IPAddress(Settings.ip_address[index -1]).toString().c_str(), (1 == index) ? stemp1:"");
}
else if (!strcasecmp_P(type, PSTR(D_CMND_NTPSERVER)) && (index > 0) && (index <= 3)) {
else if ((CMND_NTPSERVER == command_code) && (index > 0) && (index <= 3)) {
if ((data_len > 0) && (data_len < sizeof(Settings.ntp_server[0]))) {
strlcpy(Settings.ntp_server[index -1], (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? (1==index)?NTP_SERVER1:(2==index)?NTP_SERVER2:NTP_SERVER3 : dataBuf, sizeof(Settings.ntp_server[0]));
for (i = 0; i < strlen(Settings.ntp_server[index -1]); i++) {
@ -1243,9 +1287,9 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_NTPSERVER "%d\":\"%s\"}"), index, Settings.ntp_server[index -1]);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, Settings.ntp_server[index -1]);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_AP))) {
else if (CMND_AP == command_code) {
if ((payload >= 0) && (payload <= 2)) {
switch (payload) {
case 0: // Toggle
@ -1257,25 +1301,25 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_AP "\":\"%d (%s)\"}"), Settings.sta_active +1, Settings.sta_ssid[Settings.sta_active]);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE_SVALUE, command, Settings.sta_active +1, Settings.sta_ssid[Settings.sta_active]);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SSID)) && (index > 0) && (index <= 2)) {
else if ((CMND_SSID == command_code) && (index > 0) && (index <= 2)) {
if ((data_len > 0) && (data_len < sizeof(Settings.sta_ssid[0]))) {
strlcpy(Settings.sta_ssid[index -1], (1 == payload) ? (1 == index) ? STA_SSID1 : STA_SSID2 : dataBuf, sizeof(Settings.sta_ssid[0]));
Settings.sta_active = index -1;
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SSID "%d\":\"%s\"}"), index, Settings.sta_ssid[index -1]);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, Settings.sta_ssid[index -1]);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_PASSWORD)) && (index > 0) && (index <= 2)) {
else if ((CMND_PASSWORD == command_code) && (index > 0) && (index <= 2)) {
if ((data_len > 0) && (data_len < sizeof(Settings.sta_pwd[0]))) {
strlcpy(Settings.sta_pwd[index -1], (1 == payload) ? (1 == index) ? STA_PASS1 : STA_PASS2 : dataBuf, sizeof(Settings.sta_pwd[0]));
Settings.sta_active = index -1;
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PASSWORD "%d\":\"%s\"}"), index, Settings.sta_pwd[index -1]);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, Settings.sta_pwd[index -1]);
}
else if (!grpflg && !strcasecmp_P(type, PSTR(D_CMND_HOSTNAME))) {
else if ((CMND_HOSTNAME == command_code) && !grpflg) {
if ((data_len > 0) && (data_len < sizeof(Settings.hostname))) {
strlcpy(Settings.hostname, (1 == payload) ? WIFI_HOSTNAME : dataBuf, sizeof(Settings.hostname));
if (strstr(Settings.hostname,"%")) {
@ -1283,9 +1327,9 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_HOSTNAME "\":\"%s\"}"), Settings.hostname);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.hostname);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_WIFICONFIG))) {
else if (CMND_WIFICONFIG == command_code) {
if ((payload >= WIFI_RESTART) && (payload < MAX_WIFI_OPTION)) {
Settings.sta_config = payload;
wifi_state_flag = Settings.sta_config;
@ -1297,10 +1341,10 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
} else {
snprintf_P(stemp1, sizeof(stemp1), kWifiConfig[Settings.sta_config]);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WIFICONFIG "\":\"%d (%s)\"}"), Settings.sta_config, stemp1);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE_SVALUE, command, Settings.sta_config, stemp1);
}
}
else if (!strcasecmp_P(type, PSTR(D_CMND_FRIENDLYNAME)) && (index > 0) && (index <= 4)) {
else if ((CMND_FRIENDLYNAME == command_code) && (index > 0) && (index <= 4)) {
if ((data_len > 0) && (data_len < sizeof(Settings.friendlyname[0]))) {
if (1 == index) {
snprintf_P(stemp1, sizeof(stemp1), PSTR(FRIENDLY_NAME));
@ -1309,16 +1353,16 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
strlcpy(Settings.friendlyname[index -1], (1 == payload) ? stemp1 : dataBuf, sizeof(Settings.friendlyname[index -1]));
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_FRIENDLYNAME "%d\":\"%s\"}"), index, Settings.friendlyname[index -1]);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, Settings.friendlyname[index -1]);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SWITCHMODE)) && (index > 0) && (index <= MAX_SWITCHES)) {
else if ((CMND_SWITCHMODE == command_code) && (index > 0) && (index <= MAX_SWITCHES)) {
if ((payload >= 0) && (payload < MAX_SWITCH_OPTION)) {
Settings.switchmode[index -1] = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SWITCHMODE "%d\":%d}"), index, Settings.switchmode[index-1]);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_NVALUE, command, index, Settings.switchmode[index-1]);
}
#ifdef USE_WEBSERVER
else if (!strcasecmp_P(type, PSTR(D_CMND_WEBSERVER))) {
else if (CMND_WEBSERVER == command_code) {
if ((payload >= 0) && (payload <= 2)) {
Settings.webserver = payload;
}
@ -1326,32 +1370,32 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WEBSERVER "\":\"" D_ACTIVE_FOR " %s " D_ON_DEVICE " %s " D_WITH_IP_ADDRESS " %s\"}"),
(2 == Settings.webserver) ? D_ADMIN : D_USER, my_hostname, WiFi.localIP().toString().c_str());
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WEBSERVER "\":\"%s\"}"), GetStateText(0));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, GetStateText(0));
}
}
else if (!strcasecmp_P(type, PSTR(D_CMND_WEBPASSWORD))) {
else if (CMND_WEBPASSWORD == command_code) {
if ((data_len > 0) && (data_len < sizeof(Settings.web_password))) {
strlcpy(Settings.web_password, (!strcmp(dataBuf,"0")) ? "" : (1 == payload) ? WEB_PASSWORD : dataBuf, sizeof(Settings.web_password));
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WEBPASSWORD "\":\"%s\"}"), Settings.web_password);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, Settings.web_password);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_WEBLOG))) {
else if (CMND_WEBLOG == command_code) {
if ((payload >= LOG_LEVEL_NONE) && (payload <= LOG_LEVEL_ALL)) {
Settings.weblog_level = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WEBLOG "\":%d}"), Settings.weblog_level);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.weblog_level);
}
#ifdef USE_EMULATION
else if (!strcasecmp_P(type, PSTR(D_CMND_EMULATION))) {
else if (CMND_EMULATION == command_code) {
if ((payload >= EMUL_NONE) && (payload < EMUL_MAX)) {
Settings.flag.emulation = payload;
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_EMULATION "\":%d}"), Settings.flag.emulation);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.flag.emulation);
}
#endif // USE_EMULATION
#endif // USE_WEBSERVER
else if (!strcasecmp_P(type, PSTR(D_CMND_TELEPERIOD))) {
else if (CMND_TELEPERIOD == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.tele_period = (1 == payload) ? TELE_PERIOD : payload;
if ((Settings.tele_period > 0) && (Settings.tele_period < 10)) {
@ -1359,49 +1403,49 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
tele_period = Settings.tele_period;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_TELEPERIOD "\":\"%d%s\"}"), Settings.tele_period, (Settings.flag.value_units) ? " " D_UNIT_SECOND : "");
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE_UNIT, command, Settings.tele_period, (Settings.flag.value_units) ? " " D_UNIT_SECOND : "");
}
else if (!strcasecmp_P(type, PSTR(D_CMND_RESTART))) {
else if (CMND_RESTART == command_code) {
switch (payload) {
case 1:
restart_flag = 2;
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RESTART "\":\"" D_RESTARTING "\"}"));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_RESTARTING);
break;
case 99:
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION D_RESTARTING));
ESP.restart();
break;
default:
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RESTART "\":\"" D_ONE_TO_RESTART "\"}"));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_ONE_TO_RESTART);
}
}
else if (!strcasecmp_P(type, PSTR(D_CMND_RESET))) {
else if (CMND_RESET == command_code) {
switch (payload) {
case 1:
restart_flag = 211;
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RESET "\":\"" D_RESET_AND_RESTARTING "\"}"));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command , D_RESET_AND_RESTARTING);
break;
case 2:
restart_flag = 212;
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RESET "\":\"" D_ERASE ", " D_RESET_AND_RESTARTING "\"}"));
break;
default:
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RESET "\":\"" D_ONE_TO_RESET "\"}"));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_ONE_TO_RESET);
}
}
else if (!strcasecmp_P(type, PSTR(D_CMND_TIMEZONE))) {
else if (CMND_TIMEZONE == command_code) {
if ((data_len > 0) && (((payload >= -13) && (payload <= 13)) || (99 == payload))) {
Settings.timezone = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_TIMEZONE "\":%d}"), Settings.timezone);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.timezone);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_ALTITUDE))) {
else if (CMND_ALTITUDE == command_code) {
if ((data_len > 0) && ((payload >= -30000) && (payload <= 30000))) {
Settings.altitude = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_ALTITUDE "\":%d}"), Settings.altitude);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.altitude);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_LEDPOWER))) {
else if (CMND_LEDPOWER == command_code) {
if ((payload >= 0) && (payload <= 2)) {
Settings.ledstate &= 8;
switch (payload) {
@ -1416,20 +1460,20 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
blinks = 0;
SetLedPower(Settings.ledstate &8);
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_LEDPOWER "\":\"%s\"}"), GetStateText(bitRead(Settings.ledstate, 3)));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, GetStateText(bitRead(Settings.ledstate, 3)));
}
else if (!strcasecmp_P(type, PSTR(D_CMND_LEDSTATE))) {
else if (CMND_LEDSTATE ==command_code) {
if ((payload >= 0) && (payload < MAX_LED_OPTION)) {
Settings.ledstate = payload;
if (!Settings.ledstate) {
SetLedPower(0);
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_LEDSTATE "\":%d}"), Settings.ledstate);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.ledstate);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_CFGDUMP))) {
else if (CMND_CFGDUMP == command_code) {
SettingsDump(dataBuf);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_CFGDUMP "\":\"" D_DONE "\"}"));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_DONE);
}
else if (Settings.flag.mqtt_enabled && MqttCommand(grpflg, type, index, dataBuf, data_len, payload, payload16)) {
// Serviced
@ -1441,7 +1485,7 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
// Serviced
}
#ifdef USE_I2C
else if (i2c_flg && !strcasecmp_P(type, PSTR(D_CMND_I2CSCAN))) {
else if ((CMND_I2CSCAN == command_code) && i2c_flg) {
I2cScan(mqtt_data, sizeof(mqtt_data));
}
#endif // USE_I2C
@ -1451,11 +1495,11 @@ void MqttDataCallback(char* topic, byte* data, unsigned int data_len)
}
#endif // USE_IR_REMOTE
#ifdef DEBUG_THEO
else if (!strcasecmp_P(type, PSTR(D_CMND_EXCEPTION))) {
else if (CMND_EXCEPTION == command_code) {
if (data_len > 0) {
ExceptionTest(payload);
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_EXCEPTION "\":\"" D_DONE "\"}"));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_DONE);
}
#endif // DEBUG_THEO
else {

View File

@ -1170,14 +1170,67 @@ double FastPrecisePow(double a, double b)
return r * u.d;
}
char* GetIndexedString(char* destination, const char* source, uint8_t index)
char* GetTextIndexed(char* destination, size_t destination_size, uint16_t index, const char* haystack)
{
strcpy_P(destination, source); // Copies Flash to Ram until end of string
char *indexed_string = strtok(destination, "|");
// Returns empty string if not found
// Returns text of found
char* write = destination;
const char* read = haystack;
index++;
while (index--) {
indexed_string = strtok(NULL, "|");
size_t size = destination_size -1;
write = destination;
char ch = '.';
while ((ch != '\0') && (ch != '|')) {
ch = pgm_read_byte(read++);
if (size && (ch != '|')) {
*write++ = ch;
size--;
}
}
if (0 == ch) {
if (index) {
write = destination;
}
break;
}
}
return indexed_string;
*write = '\0';
return destination;
}
int GetCommandCode(char* destination, size_t destination_size, const char* needle, const char* haystack)
{
// Returns -1 of not found
// Returns index and command if found
int result = -1;
const char* read = haystack;
char* write = destination;
size_t maxcopy = (strlen(needle) > destination_size) ? destination_size : strlen(needle);
while (true) {
result++;
size_t size = destination_size -1;
write = destination;
char ch = '.';
while ((ch != '\0') && (ch != '|')) {
ch = pgm_read_byte(read++);
if (size && (ch != '|')) {
*write++ = ch;
size--;
}
}
*write = '\0';
if (!strcasecmp(needle, destination)) {
break;
}
if (0 == ch) {
result = -1;
break;
}
}
return result;
}
/*********************************************************************************************\

View File

@ -35,6 +35,11 @@ const char HTTP_FORM_DOMOTICZ_TIMER[] PROGMEM =
"<tr><td width='260'><b>" D_DOMOTICZ_UPDATE_TIMER "</b> (" STR(DOMOTICZ_UPDATE_TIMER) ")</td><td width='70'><input id='ut' name='ut' placeholder='" STR(DOMOTICZ_UPDATE_TIMER) "' value='{6'</td></tr>";
#endif // USE_WEBSERVER
enum DomoticzCommands {
CMND_IDX, CMND_KEYIDX, CMND_SWITCHIDX, CMND_SENSORIDX, CMND_UPDATETIMER };
const char kDomoticzCommands[] PROGMEM =
D_CMND_IDX "|" D_CMND_KEYIDX "|" D_CMND_SWITCHIDX "|" D_CMND_SENSORIDX "|" D_CMND_UPDATETIMER ;
enum DomoticzSensors {DZ_TEMP, DZ_TEMP_HUM, DZ_TEMP_HUM_BARO, DZ_POWER_ENERGY, DZ_ILLUMINANCE, DZ_COUNT, DZ_VOLTAGE, DZ_CURRENT, DZ_MAX_SENSORS};
const char kDomoticzSensors[] PROGMEM =
@ -190,40 +195,42 @@ boolean DomoticzMqttData(char *topicBuf, uint16_t stopicBuf, char *dataBuf, uint
boolean DomoticzCommand(const char *type, uint16_t index, char *dataBuf, uint16_t data_len, int16_t payload)
{
char command [CMDSZ];
boolean serviced = true;
uint8_t dmtcz_len = strlen(D_CMND_DOMOTICZ); // Prep for string length change
if (!strncasecmp_P(type, PSTR(D_CMND_DOMOTICZ), dmtcz_len)) { // Prefix
if (!strcasecmp_P(type +dmtcz_len, PSTR(D_CMND_IDX)) && (index > 0) && (index <= MAX_DOMOTICZ_IDX)) {
int command_code = GetCommandCode(command, sizeof(command), type +dmtcz_len, kDomoticzCommands);
if ((CMND_IDX == command_code) && (index > 0) && (index <= MAX_DOMOTICZ_IDX)) {
if (payload >= 0) {
Settings.domoticz_relay_idx[index -1] = payload;
restart_flag = 2;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DOMOTICZ D_CMND_IDX "%d\":%d}"), index, Settings.domoticz_relay_idx[index -1]);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DOMOTICZ "%s%d\":%d}"), command, index, Settings.domoticz_relay_idx[index -1]);
}
else if (!strcasecmp_P(type +dmtcz_len, PSTR(D_CMND_KEYIDX)) && (index > 0) && (index <= MAX_DOMOTICZ_IDX)) {
else if ((CMND_KEYIDX == command_code) && (index > 0) && (index <= MAX_DOMOTICZ_IDX)) {
if (payload >= 0) {
Settings.domoticz_key_idx[index -1] = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DOMOTICZ D_CMND_KEYIDX "%d\":%d}"), index, Settings.domoticz_key_idx[index -1]);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DOMOTICZ "%s%d\":%d}"), command, index, Settings.domoticz_key_idx[index -1]);
}
else if (!strcasecmp_P(type +dmtcz_len, PSTR(D_CMND_SWITCHIDX)) && (index > 0) && (index <= MAX_DOMOTICZ_IDX)) {
else if ((CMND_SWITCHIDX == command_code) && (index > 0) && (index <= MAX_DOMOTICZ_IDX)) {
if (payload >= 0) {
Settings.domoticz_switch_idx[index -1] = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DOMOTICZ D_CMND_SWITCHIDX "%d\":%d}"), index, Settings.domoticz_key_idx[index -1]);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DOMOTICZ "%s%d\":%d}"), command, index, Settings.domoticz_key_idx[index -1]);
}
else if (!strcasecmp_P(type +dmtcz_len, PSTR(D_CMND_SENSORIDX)) && (index > 0) && (index <= DZ_MAX_SENSORS)) {
else if ((CMND_SENSORIDX == command_code) && (index > 0) && (index <= DZ_MAX_SENSORS)) {
if (payload >= 0) {
Settings.domoticz_sensor_idx[index -1] = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DOMOTICZ D_CMND_SENSORIDX "%d\":%d}"), index, Settings.domoticz_sensor_idx[index -1]);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DOMOTICZ "%s%d\":%d}"), command, index, Settings.domoticz_sensor_idx[index -1]);
}
else if (!strcasecmp_P(type +dmtcz_len, PSTR(D_CMND_UPDATETIMER))) {
else if (CMND_UPDATETIMER == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.domoticz_update_timer = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DOMOTICZ D_CMND_UPDATETIMER "\":%d}"), Settings.domoticz_update_timer);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DOMOTICZ "%s\":%d}"), command, Settings.domoticz_update_timer);
}
else serviced = false;
}
@ -309,7 +316,7 @@ void HandleDomoticzConfiguration()
}
AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONFIGURE_DOMOTICZ);
char stemp[sizeof(kDomoticzSensors)];
char stemp[32];
char *sensortype;
String page = FPSTR(HTTP_HEAD);
@ -330,7 +337,7 @@ void HandleDomoticzConfiguration()
for (int i = 0; i < DZ_MAX_SENSORS; i++) {
page += FPSTR(HTTP_FORM_DOMOTICZ_SENSOR);
page.replace("{1", String(i +1));
page.replace("{2", GetIndexedString(stemp, kDomoticzSensors, i));
page.replace("{2", GetTextIndexed(stemp, sizeof(stemp), i, kDomoticzSensors));
page.replace("{5", String((int)Settings.domoticz_sensor_idx[i]));
}
page += FPSTR(HTTP_FORM_DOMOTICZ_TIMER);

View File

@ -67,7 +67,7 @@ void IrSendInit(void)
#define IR_TIME_AVOID_DUPLICATE 500 // Milliseconds
// Based on IRremoteESP8266.h enum decode_type_t
const char kIrReceiveProtocols[] PROGMEM =
const char kIrRemoteProtocols[] PROGMEM =
"UNKNOWN|RC5|RC6|NEC|SONY|PANASONIC|JVC|SAMSUNG|WHYNTER|AIWA_RC_T501|LG|SANYO|MITSUBISHI|DISH|SHARP";
IRrecv *irrecv = NULL;
@ -83,7 +83,7 @@ void IrReceiveInit(void)
void IrReceiveCheck()
{
char sirtype[sizeof(kIrReceiveProtocols)];
char sirtype[14]; // Max is AIWA_RC_T501
int8_t iridx = 0;
decode_results results;
@ -103,7 +103,7 @@ void IrReceiveCheck()
iridx = 0;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_IRRECEIVED "\":{\"" D_IR_PROTOCOL "\":\"%s\", \"" D_IR_BITS "\":%d, \"" D_IR_DATA "\":\"%X\"}}"),
GetIndexedString(sirtype, kIrReceiveProtocols, iridx), results.bits, results.value);
GetTextIndexed(sirtype, sizeof(sirtype), iridx, kIrRemoteProtocols), results.bits, results.value);
MqttPublishPrefixTopic_P(6, PSTR(D_IRRECEIVED));
#ifdef USE_DOMOTICZ
unsigned long value = results.value | (iridx << 28); // [Protocol:4, Data:28]
@ -275,6 +275,7 @@ boolean IrSendCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_l
boolean serviced = true;
boolean error = false;
char dataBufUc[data_len];
char protocol_text[20];
const char *protocol;
uint32_t bits = 0;
uint32_t data = 0;
@ -301,24 +302,26 @@ boolean IrSendCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_l
bits = ir_json[D_IR_BITS];
data = ir_json[D_IR_DATA];
if (protocol && bits && data) {
if (!strcasecmp_P(protocol, PSTR("NEC")))
irsend->sendNEC(data, bits);
else if (!strcasecmp_P(protocol, PSTR("SONY")))
irsend->sendSony(data, bits);
else if (!strcasecmp_P(protocol, PSTR("RC5")))
irsend->sendRC5(data, bits);
else if (!strcasecmp_P(protocol, PSTR("RC6")))
irsend->sendRC6(data, bits);
else if (!strcasecmp_P(protocol, PSTR("DISH")))
irsend->sendDISH(data, bits);
else if (!strcasecmp_P(protocol, PSTR("JVC")))
irsend->sendJVC(data, bits, 1);
else if (!strcasecmp_P(protocol, PSTR("SAMSUNG")))
irsend->sendSAMSUNG(data, bits);
else if (!strcasecmp_P(protocol, PSTR("PANASONIC")))
irsend->sendPanasonic(bits, data);
else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_IRSEND "\":\"" D_PROTOCOL_NOT_SUPPORTED "\"}"));
int protocol_code = GetCommandCode(protocol_text, sizeof(protocol_text), protocol, kIrRemoteProtocols);
switch (protocol_code) {
case NEC:
irsend->sendNEC(data, bits); break;
case SONY:
irsend->sendSony(data, bits); break;
case RC5:
irsend->sendRC5(data, bits); break;
case RC6:
irsend->sendRC6(data, bits); break;
case DISH:
irsend->sendDISH(data, bits); break;
case JVC:
irsend->sendJVC(data, bits, 1); break;
case SAMSUNG:
irsend->sendSAMSUNG(data, bits); break;
case PANASONIC:
irsend->sendPanasonic(bits, data); break;
default:
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_IRSEND "\":\"" D_PROTOCOL_NOT_SUPPORTED "\"}"));
}
}
else {

View File

@ -23,6 +23,11 @@
#define SFB_TIME_AVOID_DUPLICATE 2000 // Milliseconds
enum SonoffBridgeCommands {
CMND_RFSYNC, CMND_RFLOW, CMND_RFHIGH, CMND_RFHOST, CMND_RFCODE, CMND_RFKEY };
const char kSonoffBridgeCommands[] PROGMEM =
D_CMND_RFSYNC "|" D_CMND_RFLOW "|" D_CMND_RFHIGH "|" D_CMND_RFHOST "|" D_CMND_RFCODE "|" D_CMND_RFKEY ;
uint8_t sonoff_bridge_receive_flag = 0;
uint8_t sonoff_bridge_learn_key = 1;
uint8_t sonoff_bridge_learn_active = 0;
@ -122,6 +127,20 @@ void SonoffBridgeSendAck()
Serial.write(0x55); // End of Text
}
void SonoffBridgeSendCode(uint32_t code)
{
Serial.write(0xAA); // Start of Text
Serial.write(0xA5); // Send following code
for (uint8_t i = 0; i < 6; i++) {
Serial.write(Settings.rf_code[0][i]);
}
Serial.write(code >> 16 & 0xff);
Serial.write(code >> 8 & 0xff);
Serial.write(code & 0xff);
Serial.write(0x55); // End of Text
Serial.flush();
}
void SonoffBridgeSend(uint8_t idx, uint8_t key)
{
uint8_t code;
@ -161,22 +180,54 @@ void SonoffBridgeLearn(uint8_t key)
boolean SonoffBridgeCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_len, int16_t payload)
{
char command [CMDSZ];
boolean serviced = true;
char *p;
if (!strcasecmp_P(type, PSTR(D_CMND_RFDEFAULT))) {
if (4 == data_len) {
uint16_t hexcode = strtol(dataBuf, &p, 16);
uint8_t msb = hexcode >> 8;
uint8_t lsb = hexcode & 0xFF;
if ((hexcode > 0) && (hexcode < 0x7FFF) && (msb != 0x55) && (lsb != 0x55)) {
Settings.rf_code[0][6] = msb;
Settings.rf_code[0][7] = lsb;
int command_code = GetCommandCode(command, sizeof(command), type, kSonoffBridgeCommands);
if ((command_code >= CMND_RFSYNC) && (command_code <= CMND_RFCODE)) { // RfSync, RfLow, RfHigh, RfHost and RfCode
char *p;
char stemp [10];
uint32_t code = 0;
uint8_t radix = 10;
uint8_t set_index = command_code *2;
if (dataBuf[0] == '#') {
dataBuf++;
data_len--;
radix = 16;
}
if (data_len) {
code = strtol(dataBuf, &p, radix);
if (code) {
if (CMND_RFCODE == command_code) {
SonoffBridgeSendCode(code);
} else {
if (1 == payload) {
code = pgm_read_byte(kDefaultRfCode + set_index) << 8 | pgm_read_byte(kDefaultRfCode + set_index +1);
}
uint8_t msb = code >> 8;
uint8_t lsb = code & 0xFF;
if ((code > 0) && (code < 0x7FFF) && (msb != 0x55) && (lsb != 0x55)) { // Check for End of Text codes
Settings.rf_code[0][set_index] = msb;
Settings.rf_code[0][set_index +1] = lsb;
}
}
}
} else {
if (CMND_RFCODE != command_code) {
code = Settings.rf_code[0][set_index] << 8 | Settings.rf_code[0][set_index +1];
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFDEFAULT "\":\"%0X%0X\"}"), Settings.rf_code[0][6], Settings.rf_code[0][7]);
if (10 == radix) {
snprintf_P(stemp, sizeof(stemp), PSTR("%d"), code);
} else {
snprintf_P(stemp, sizeof(stemp), PSTR("\"#%X\""), code);
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":%s}"), command, stemp);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_RFKEY)) && (index > 0) && (index <= 16)) {
else if ((CMND_RFKEY == command_code) && (index > 0) && (index <= 16)) {
if (!sonoff_bridge_learn_active) {
if (2 == payload) {
SonoffBridgeLearn(index);
@ -197,9 +248,9 @@ boolean SonoffBridgeCommand(char *type, uint16_t index, char *dataBuf, uint16_t
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFKEY "%d\":\"" D_LEARNING_ACTIVE "\"}"), sonoff_bridge_learn_key);
}
} else {
serviced = false;
}
else {
serviced = false; // Unknown command
}
return serviced;
}

View File

@ -51,6 +51,13 @@
*
\*********************************************************************************************/
enum LightCommands {
CMND_COLOR, CMND_COLORTEMPERATURE, CMND_DIMMER, CMND_LED, CMND_LEDTABLE, CMND_FADE,
CMND_PIXELS, CMND_SCHEME, CMND_SPEED, CMND_WAKEUP, CMND_WAKEUPDURATION, CMND_WIDTH, CMND_UNDOCA };
const char kLightCommands[] PROGMEM =
D_CMND_COLOR "|" D_CMND_COLORTEMPERATURE "|" D_CMND_DIMMER "|" D_CMND_LED "|" D_CMND_LEDTABLE "|" D_CMND_FADE "|"
D_CMND_PIXELS "|" D_CMND_SCHEME "|" D_CMND_SPEED "|" D_CMND_WAKEUP "|" D_CMND_WAKEUPDURATION "|" D_CMND_WIDTH "|UNDOCA" ;
uint8_t ledTable[] = {
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
@ -745,12 +752,14 @@ boolean LightColorEntry(char *buffer, uint8_t buffer_length)
boolean LightCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_len, int16_t payload)
{
char command [CMDSZ];
boolean serviced = true;
boolean coldim = false;
boolean valid_entry = false;
char scolor[25];
if ((light_subtype > LST_SINGLE) && !strcasecmp_P(type, PSTR(D_CMND_COLOR)) && (index > 0) && (index <= 4)) {
int command_code = GetCommandCode(command, sizeof(command), type, kLightCommands);
if ((CMND_COLOR == command_code) && (light_subtype > LST_SINGLE) && (index > 0) && (index <= 4)) {
if (data_len > 0) {
valid_entry = LightColorEntry(dataBuf, data_len);
if (valid_entry) {
@ -770,7 +779,7 @@ boolean LightCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_le
}
}
if (!valid_entry && (1 == index)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_COLOR "\":\"%s\"}"), LightGetColor(0, scolor));
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\"}"), command, LightGetColor(0, scolor));
}
if (index > 1) {
scolor[0] = '\0';
@ -781,41 +790,41 @@ boolean LightCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_le
snprintf_P(scolor, 25, PSTR("%s%02X"), scolor, Settings.ws_color[index -2][i]);
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_COLOR "%d\":\"%s\"}"), index, scolor);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, scolor);
}
}
#ifdef USE_WS2812 // ***********************************************************************
else if ((LT_WS2812 == light_type) && !strcasecmp_P(type, PSTR(D_CMND_LED)) && (index > 0) && (index <= Settings.light_pixels)) {
else if ((CMND_LED == command_code) && (LT_WS2812 == light_type) && (index > 0) && (index <= Settings.light_pixels)) {
if (data_len > 0) {
if (LightColorEntry(dataBuf, data_len)) {
Ws2812SetColor(index, light_entry_color[0], light_entry_color[1], light_entry_color[2]);
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_LED "%d\":\"%s\"}"), index, Ws2812GetColor(index, scolor));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, Ws2812GetColor(index, scolor));
}
else if ((LT_WS2812 == light_type) && !strcasecmp_P(type, PSTR(D_CMND_PIXELS))) {
else if ((CMND_PIXELS == command_code) && (LT_WS2812 == light_type)) {
if ((payload > 0) && (payload <= WS2812_MAX_LEDS)) {
Settings.light_pixels = payload;
Ws2812Clear();
light_update = 1;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_PIXELS "\":%d}"), Settings.light_pixels);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_pixels);
}
else if ((LT_WS2812 == light_type) && !strcasecmp_P(type, PSTR(D_CMND_WIDTH)) && (index > 0) && (index <= 4)) {
else if ((CMND_WIDTH == command_code) && (LT_WS2812 == light_type) && (index > 0) && (index <= 4)) {
if (1 == index) {
if ((payload >= 0) && (payload <= 4)) {
Settings.light_width = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WIDTH "\":%d}"), Settings.light_width);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_width);
} else {
if ((payload > 0) && (payload < 32)) {
Settings.ws_width[index -2] = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WIDTH "%d\":%d}"), index, Settings.ws_width[index -2]);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_NVALUE, command, index, Settings.ws_width[index -2]);
}
}
#endif // USE_WS2812 ************************************************************************
else if ((light_subtype >= LST_RGB) && !strcasecmp_P(type, PSTR(D_CMND_SCHEME))) {
else if ((CMND_SCHEME == command_code) && (light_subtype >= LST_RGB)) {
uint8_t max_scheme = (LT_WS2812 == light_type) ? LS_MAX +7 : LS_MAX -1;
if ((payload >= 0) && (payload <= max_scheme)) {
Settings.light_scheme = payload;
@ -825,34 +834,34 @@ boolean LightCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_le
LightPowerOn();
strip_timer_counter = 0;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SCHEME "\":%d}"), Settings.light_scheme);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_scheme);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_WAKEUP))) {
else if (CMND_WAKEUP == command_code) {
if ((payload >= 0) && (payload <= 100)) {
Settings.light_dimmer = payload;
}
light_wakeup_active = 3;
Settings.light_scheme = LS_WAKEUP;
LightPowerOn();
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WAKEUP "\":\"" D_STARTED "\"}"));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_STARTED);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_COLORTEMPERATURE)) && ((2 == light_subtype) || (5 == light_subtype))) { // ColorTemp
else if ((CMND_COLORTEMPERATURE == command_code) && ((2 == light_subtype) || (5 == light_subtype))) { // ColorTemp
if ((payload >= 153) && (payload <= 500)) { // https://developers.meethue.com/documentation/core-concepts
LightSetColorTemp(payload);
coldim = true;
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_COLORTEMPERATURE "\":%d}"), LightGetColorTemp());
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, LightGetColorTemp());
}
}
else if (!strcasecmp_P(type, PSTR(D_CMND_DIMMER))) {
else if (CMND_DIMMER == command_code) {
if ((payload >= 0) && (payload <= 100)) {
Settings.light_dimmer = payload;
coldim = true;
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_DIMMER "\":%d}"), Settings.light_dimmer);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_dimmer);
}
}
else if (!strcasecmp_P(type, PSTR(D_CMND_LEDTABLE))) {
else if (CMND_LEDTABLE == command_code) {
if ((payload >= 0) && (payload <= 2)) {
switch (payload) {
case 0: // Off
@ -865,9 +874,9 @@ boolean LightCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_le
}
light_update = 1;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_LEDTABLE "\":\"%s\"}"), GetStateText(Settings.light_correction));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, GetStateText(Settings.light_correction));
}
else if (!strcasecmp_P(type, PSTR(D_CMND_FADE))) {
else if (CMND_FADE == command_code) {
switch (payload) {
case 0: // Off
case 1: // On
@ -877,22 +886,22 @@ boolean LightCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_le
Settings.light_fade ^= 1;
break;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_FADE "\":\"%s\"}"), GetStateText(Settings.light_fade));
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, GetStateText(Settings.light_fade));
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SPEED))) { // 1 - fast, 20 - very slow
else if (CMND_SPEED == command_code) { // 1 - fast, 20 - very slow
if ((payload > 0) && (payload <= STATES)) {
Settings.light_speed = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SPEED "\":%d}"), Settings.light_speed);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.light_speed);
}
else if (!strcasecmp_P(type, PSTR(D_CMND_WAKEUPDURATION))) {
else if (CMND_WAKEUPDURATION == command_code) {
if ((payload > 0) && (payload < 3001)) {
Settings.light_wakeup = payload;
light_wakeup_active = 0;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_WAKEUPDURATION "\":%d}"), Settings.light_wakeup);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, Settings.light_wakeup);
}
else if (!strcasecmp_P(type, PSTR("UNDOCA"))) { // Theos legacy status
else if (CMND_UNDOCA == command_code) { // Theos legacy status
LightGetColor(1, scolor);
scolor[6] = '\0'; // RGB only
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s, %d, %d, %d, %d, %d"),

View File

@ -139,7 +139,9 @@ void Ws2812Clock()
int clksize = 600 / (int)Settings.light_pixels;
Ws2812UpdateHand((RtcTime.second * 10) / clksize, WS_SECOND);
Ws2812UpdateHand((RtcTime.minute * 10) / clksize, WS_MINUTE);
Ws2812UpdateHand((RtcTime.hour % 12) * (50 / clksize), WS_HOUR);
// Ws2812UpdateHand((RtcTime.hour % 12) * (50 / clksize), WS_HOUR);
Ws2812UpdateHand(((RtcTime.hour % 12) * (50 / clksize)) + ((RtcTime.minute * 10) / (12 * clksize)), WS_HOUR);
Ws2812StripShow();
}

View File

@ -33,6 +33,19 @@
#define HLW_POWER_PROBE_TIME 10 // Number of seconds to probe for power before deciding none used
enum Hlw8012Commands {
CMND_POWERLOW, CMND_POWERHIGH, CMND_VOLTAGELOW, CMND_VOLTAGEHIGH, CMND_CURRENTLOW, CMND_CURRENTHIGH,
CMND_HLWPCAL, CMND_HLWPSET, CMND_HLWUCAL, CMND_HLWUSET, CMND_HLWICAL, CMND_HLWISET,
CMND_ENERGYRESET, CMND_MAXENERGY, CMND_MAXENERGYSTART,
CMND_MAXPOWER, CMND_MAXPOWERHOLD, CMND_MAXPOWERWINDOW,
CMND_SAFEPOWER, CMND_SAFEPOWERHOLD, CMND_SAFEPOWERWINDOW };
const char kHlw8012Commands[] PROGMEM =
D_CMND_POWERLOW "|" D_CMND_POWERHIGH "|" D_CMND_VOLTAGELOW "|" D_CMND_VOLTAGEHIGH "|" D_CMND_CURRENTLOW "|" D_CMND_CURRENTHIGH "|"
D_CMND_HLWPCAL "|" D_CMND_HLWPSET "|" D_CMND_HLWUCAL "|" D_CMND_HLWUSET "|" D_CMND_HLWICAL "|" D_CMND_HLWISET "|"
D_CMND_ENERGYRESET "|" D_CMND_MAXENERGY "|" D_CMND_MAXENERGYSTART "|"
D_CMND_MAXPOWER "|" D_CMND_MAXPOWERHOLD "|" D_CMND_MAXPOWERWINDOW "|"
D_CMND_SAFEPOWER "|" D_CMND_SAFEPOWERHOLD "|" D_CMND_SAFEPOWERWINDOW ;
byte hlw_pmin_flag = 0;
byte hlw_pmax_flag = 0;
byte hlw_umin_flag = 0;
@ -443,46 +456,57 @@ void HlwMarginCheck()
boolean HlwCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_len, int16_t payload)
{
char command [CMDSZ];
char sunit[CMDSZ];
boolean serviced = true;
uint8_t caltext = 0;
uint8_t status_flag = 0;
uint8_t unit = 0;
unsigned long nvalue = 0;
if (!strcasecmp_P(type, PSTR(D_CMND_POWERLOW))) {
int command_code = GetCommandCode(command, sizeof(command), type, kHlw8012Commands);
if (CMND_POWERLOW == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_pmin = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_POWERLOW "\":\"%d%s\"}"), Settings.hlw_pmin, (Settings.flag.value_units) ? " " D_UNIT_WATT : "");
nvalue = Settings.hlw_pmin;
unit = UNIT_WATT;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_POWERHIGH))) {
else if (CMND_POWERHIGH == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_pmax = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_POWERHIGH "\":\"%d%s\"}"), Settings.hlw_pmax, (Settings.flag.value_units) ? " " D_UNIT_WATT : "");
nvalue = Settings.hlw_pmax;
unit = UNIT_WATT;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_VOLTAGELOW))) {
else if (CMND_VOLTAGELOW == command_code) {
if ((payload >= 0) && (payload < 501)) {
Settings.hlw_umin = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_VOLTAGELOW "\":\"%d%s\"}"), Settings.hlw_umin, (Settings.flag.value_units) ? " " D_UNIT_VOLT : "");
nvalue = Settings.hlw_umin;
unit = UNIT_VOLT;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_VOLTAGEHIGH))) {
else if (CMND_VOLTAGEHIGH == command_code) {
if ((payload >= 0) && (payload < 501)) {
Settings.hlw_umax = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_VOLTAGEHIGH "\":\"%d%s\"}"), Settings.hlw_umax, (Settings.flag.value_units) ? " " D_UNIT_VOLT : "");
nvalue = Settings.hlw_umax;
unit = UNIT_VOLT;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_CURRENTLOW))) {
else if (CMND_CURRENTLOW == command_code) {
if ((payload >= 0) && (payload < 16001)) {
Settings.hlw_imin = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_CURRENTLOW "\":\"%d%s\"}"), Settings.hlw_imin, (Settings.flag.value_units) ? " " D_UNIT_MILLIAMPERE : "");
nvalue = Settings.hlw_imin;
unit = UNIT_MILLIAMPERE;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_CURRENTHIGH))) {
else if (CMND_CURRENTHIGH == command_code) {
if ((payload >= 0) && (payload < 16001)) {
Settings.hlw_imax = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_CURRENTHIGH "\":\"%d%s\"}"), Settings.hlw_imax, (Settings.flag.value_units) ? " " D_UNIT_MILLIAMPERE : "");
nvalue = Settings.hlw_imax;
unit = UNIT_MILLIAMPERE;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_ENERGYRESET))) {
else if (CMND_ENERGYRESET == command_code) {
if ((payload >= 1) && (payload <= 3)) {
switch (payload) {
case 1:
@ -505,109 +529,123 @@ boolean HlwCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_len,
dtostrfd((float)Settings.hlw_kWhyesterday / 100000000, Settings.flag.energy_resolution, syesterday_energy);
dtostrfd((float)RtcSettings.hlw_kWhtoday / 100000000, Settings.flag.energy_resolution, stoday_energy);
dtostrfd((float)(RtcSettings.hlw_kWhtotal + (hlw_kWhtoday / 1000)) / 100000, Settings.flag.energy_resolution, stotal_energy);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_ENERGYRESET "\":{\"" D_TOTAL "\":%s, \"" D_YESTERDAY "\":%s, \"" D_TODAY "\":%s}}"),
stotal_energy, syesterday_energy, stoday_energy);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":{\"" D_TOTAL "\":%s, \"" D_YESTERDAY "\":%s, \"" D_TODAY "\":%s}}"),
command, stotal_energy, syesterday_energy, stoday_energy);
status_flag = 1;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_HLWPCAL))) {
else if (CMND_HLWPCAL == command_code) {
if ((payload > 0) && (payload < 32001)) {
Settings.hlw_power_calibration = (payload > 4000) ? payload : HLW_PREF_PULSE; // 12530
}
caltext = 1;
nvalue = Settings.hlw_power_calibration;
unit = UNIT_MICROSECOND;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_HLWPSET))) {
else if (CMND_HLWPSET == command_code) {
if ((payload > 0) && (payload < 3601) && hlw_cf_pulse_length) {
Settings.hlw_power_calibration = (payload * 10 * hlw_cf_pulse_length) / HLW_PREF;
}
caltext = 1;
snprintf_P(command, sizeof(command), PSTR(D_CMND_HLWPCAL));
nvalue = Settings.hlw_power_calibration;
unit = UNIT_MICROSECOND;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_HLWUCAL))) {
else if (CMND_HLWUCAL == command_code) {
if ((payload > 0) && (payload < 32001)) {
Settings.hlw_voltage_calibration = (payload > 999) ? payload : HLW_UREF_PULSE; // 1950
}
caltext = 2;
nvalue = Settings.hlw_voltage_calibration;
unit = UNIT_MICROSECOND;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_HLWUSET))) {
else if (CMND_HLWUSET == command_code) {
if ((payload > 0) && (payload < 501) && hlw_cf1_voltage_pulse_length) {
Settings.hlw_voltage_calibration = (payload * 10 * hlw_cf1_voltage_pulse_length) / HLW_UREF;
}
caltext = 2;
snprintf_P(command, sizeof(command), PSTR(D_CMND_HLWUCAL));
nvalue = Settings.hlw_voltage_calibration;
unit = UNIT_MICROSECOND;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_HLWICAL))) {
else if (CMND_HLWICAL == command_code) {
if ((payload > 0) && (payload < 32001)) {
Settings.hlw_current_calibration = (payload > 1100) ? payload : HLW_IREF_PULSE; // 3500
}
caltext = 3;
nvalue = Settings.hlw_current_calibration;
unit = UNIT_MICROSECOND;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_HLWISET))) {
else if (CMND_HLWISET == command_code) {
if ((payload > 0) && (payload < 16001) && hlw_cf1_current_pulse_length) {
Settings.hlw_current_calibration = (payload * hlw_cf1_current_pulse_length) / HLW_IREF;
}
caltext = 3;
snprintf_P(command, sizeof(command), PSTR(D_CMND_HLWPCAL));
nvalue = Settings.hlw_current_calibration;
unit = UNIT_MICROSECOND;
}
#if FEATURE_POWER_LIMIT
else if (!strcasecmp_P(type, PSTR(D_CMND_MAXPOWER))) {
else if (CMND_MAXPOWER == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_mpl = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MAXPOWER "\":\"%d%s\"}"), Settings.hlw_mpl, (Settings.flag.value_units) ? " " D_UNIT_WATT : "");
nvalue = Settings.hlw_mpl;
unit = UNIT_WATT;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_MAXPOWERHOLD))) {
else if (CMND_MAXPOWERHOLD == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_mplh = (1 == payload) ? MAX_POWER_HOLD : payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MAXPOWERHOLD "\":\"%d%s\"}"), Settings.hlw_mplh, (Settings.flag.value_units) ? " " D_UNIT_SECOND : "");
nvalue = Settings.hlw_mplh;
unit = UNIT_SECOND;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_MAXPOWERWINDOW))) {
else if (CMND_MAXPOWERWINDOW == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_mplw = (1 == payload) ? MAX_POWER_WINDOW : payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MAXPOWERWINDOW "\":\"%d%s\"}"), Settings.hlw_mplw, (Settings.flag.value_units) ? " " D_UNIT_SECOND : "");
nvalue = Settings.hlw_mplw;
unit = UNIT_SECOND;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SAFEPOWER))) {
else if (CMND_SAFEPOWER == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_mspl = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SAFEPOWER "\":\"%d%s\"}"), Settings.hlw_mspl, (Settings.flag.value_units) ? " " D_UNIT_WATT : "");
nvalue = Settings.hlw_mspl;
unit = UNIT_WATT;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SAFEPOWERHOLD))) {
else if (CMND_SAFEPOWERHOLD == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_msplh = (1 == payload) ? SAFE_POWER_HOLD : payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SAFEPOWERHOLD "\":\"%d%s\"}"), Settings.hlw_msplh, (Settings.flag.value_units) ? " " D_UNIT_SECOND : "");
nvalue = Settings.hlw_msplh;
unit = UNIT_SECOND;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_SAFEPOWERWINDOW))) {
else if (CMND_SAFEPOWERWINDOW == command_code) {
if ((payload >= 0) && (payload < 1440)) {
Settings.hlw_msplw = (1 == payload) ? SAFE_POWER_WINDOW : payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_SAFEPOWERWINDOW "\":\"%d%s\"}"), Settings.hlw_msplw, (Settings.flag.value_units) ? " " D_UNIT_MINUTE : "");
nvalue = Settings.hlw_msplw;
unit = UNIT_MINUTE;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_MAXENERGY))) {
else if (CMND_MAXENERGY == command_code) {
if ((payload >= 0) && (payload < 3601)) {
Settings.hlw_mkwh = payload;
hlw_mkwh_state = 3;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MAXENERGY "\":\"%d%s\"}"), Settings.hlw_mkwh, (Settings.flag.value_units) ? " " D_UNIT_WATTHOUR : "");
nvalue = Settings.hlw_mkwh;
unit = UNIT_WATTHOUR;
}
else if (!strcasecmp_P(type, PSTR(D_CMND_MAXENERGYSTART))) {
else if (CMND_MAXENERGYSTART == command_code) {
if ((payload >= 0) && (payload < 24)) {
Settings.hlw_mkwhs = payload;
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_MAXENERGYSTART "\":\"%d%s\"}"), Settings.hlw_mkwhs, (Settings.flag.value_units) ? " " D_UNIT_HOUR : "");
nvalue = Settings.hlw_mkwhs;
unit = UNIT_HOUR;
}
#endif // FEATURE_POWER_LIMIT
else {
serviced = false;
}
switch (caltext) {
case 1:
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_HLWPCAL "\":\"%d%s\"}"), Settings.hlw_power_calibration, (Settings.flag.value_units) ? " " D_UNIT_MICROSECOND : "");
break;
case 2:
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_HLWUCAL "\":\"%d%s\"}"), Settings.hlw_voltage_calibration, (Settings.flag.value_units) ? " " D_UNIT_MICROSECOND : "");
break;
case 3:
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_HLWICAL "\":\"%d%s\"}"), Settings.hlw_current_calibration, (Settings.flag.value_units) ? " " D_UNIT_MICROSECOND : "");
break;
if (!status_flag) {
if (Settings.flag.value_units) {
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE_SPACE_UNIT, command, nvalue, GetTextIndexed(sunit, sizeof(sunit), unit, kUnitNames));
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, nvalue);
}
}
return serviced;
}