From 01ecc12f13283b4bef9202dc2723ac67aff360bf Mon Sep 17 00:00:00 2001 From: andrethomas Date: Thu, 17 Jan 2019 01:22:51 +0200 Subject: [PATCH 01/10] PN532: Prevent non AlphaNumberic on DATA --- sonoff/xsns_40_pn532_i2c.ino | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sonoff/xsns_40_pn532_i2c.ino b/sonoff/xsns_40_pn532_i2c.ino index c2fdf65e7..dd5fb4aba 100644 --- a/sonoff/xsns_40_pn532_i2c.ino +++ b/sonoff/xsns_40_pn532_i2c.ino @@ -406,7 +406,13 @@ void PN532_ScanForTag(void) uint8_t keyuniversal[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; if (mifareclassic_AuthenticateBlock (uid, uid_len, 1, 1, keyuniversal)) { if (mifareclassic_ReadDataBlock(1, card_data)) { - memcpy(&card_datas,&card_data,sizeof(card_data)); // Cast block 1 to a string + for (uint8_t i = 0;i < sizeof(card_data);i++) { + if ((isalpha(card_data[i])) || ((isdigit(card_data[i])))) { + card_datas[i] = char(card_data[i]); + } else { + card_datas[i] = '\0'; + } + } } if (pn532_i2c_function == 1) { // erase block 1 of card for (uint8_t i = 0;i<16;i++) { From 66c39ce88c60b628ff9d9109ea1d19b38d77ceca Mon Sep 17 00:00:00 2001 From: andrethomas Date: Thu, 17 Jan 2019 02:10:22 +0200 Subject: [PATCH 02/10] PN532: Prevent non AlphaNumberic on DATA --- sonoff/xsns_40_pn532_i2c.ino | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/sonoff/xsns_40_pn532_i2c.ino b/sonoff/xsns_40_pn532_i2c.ino index dd5fb4aba..a03b30e6b 100644 --- a/sonoff/xsns_40_pn532_i2c.ino +++ b/sonoff/xsns_40_pn532_i2c.ino @@ -67,6 +67,7 @@ uint8_t pn532_i2c_disable = 0; #ifdef USE_PN532_DATA_FUNCTION uint8_t pn532_i2c_function = 0; uint8_t pn532_i2c_newdata[16]; +uint8_t pn532_i2c_newdata_len = 0; #endif // USE_PN532_DATA_FUNCTION const uint8_t PROGMEM pn532_global_timeout = 10; @@ -426,12 +427,23 @@ void PN532_ScanForTag(void) } } if (pn532_i2c_function == 2) { - memcpy(&card_data,&pn532_i2c_newdata,sizeof(card_data)); - if (mifareclassic_WriteDataBlock(1, card_data)) { - set_success = true; - snprintf_P(log_data, sizeof(log_data),"I2C: PN532 NFC - Data write successful"); + boolean IsAlphaNumeric = true; + for (uint8_t i = 0;i < pn532_i2c_newdata_len;i++) { + if ((!isalpha(pn532_i2c_newdata[i])) || (!isdigit(pn532_i2c_newdata[i]))) { + IsAlphaNumeric = false; + } + } + if (IsAlphaNumeric) { + if (mifareclassic_WriteDataBlock(1, card_data)) { + memcpy(&card_data,&pn532_i2c_newdata,sizeof(card_data)); + set_success = true; + snprintf_P(log_data, sizeof(log_data),"I2C: PN532 NFC - Data write successful"); + AddLog(LOG_LEVEL_INFO); + memcpy(&card_datas,&card_data,sizeof(card_data)); // Cast block 1 to a string + } + } else { + snprintf_P(log_data, sizeof(log_data),"I2C: PN532 NFC - Data must be alphanumeric"); AddLog(LOG_LEVEL_INFO); - memcpy(&card_datas,&card_data,sizeof(card_data)); // Cast block 1 to a string } } } else { @@ -518,10 +530,10 @@ boolean PN532_Command(void) return serviced; } sprintf(sub_string_tmp,subStr(sub_string, XdrvMailbox.data, ",", 2)); - uint8_t dlen = strlen(sub_string_tmp); - if (dlen > 15) { dlen = 15; } - memcpy(&pn532_i2c_newdata,&sub_string_tmp,dlen); - pn532_i2c_newdata[dlen] = 0x00; // Null terminate the string + pn532_i2c_newdata_len = strlen(sub_string_tmp); + if (pn532_i2c_newdata_len > 15) { pn532_i2c_newdata_len = 15; } + memcpy(&pn532_i2c_newdata,&sub_string_tmp,pn532_i2c_newdata_len); + pn532_i2c_newdata[pn532_i2c_newdata_len] = 0x00; // Null terminate the string pn532_i2c_function = 2; snprintf_P(log_data, sizeof(log_data),"I2C: PN532 NFC - Next scanned tag data block 1 will be set to '%s'",pn532_i2c_newdata); AddLog(LOG_LEVEL_INFO); From 092aba945f87128141fb22fe5f41b71eaa8e1eb2 Mon Sep 17 00:00:00 2001 From: Daniel Schroeder Date: Thu, 17 Jan 2019 15:29:41 +0100 Subject: [PATCH 03/10] Tiny typo fix in comment --- sonoff/user_config_override_sample.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sonoff/user_config_override_sample.h b/sonoff/user_config_override_sample.h index b2b403e1a..9a48d2d50 100644 --- a/sonoff/user_config_override_sample.h +++ b/sonoff/user_config_override_sample.h @@ -20,7 +20,7 @@ #ifndef _USER_CONFIG_OVERRIDE_H_ #define _USER_CONFIG_OVERRIDE_H_ -// force the compiler to show a warning to confirm that this file is inlcuded +// force the compiler to show a warning to confirm that this file is included #warning **** user_config_override.h: Using Settings from this File **** /*****************************************************************************************************\ @@ -93,4 +93,4 @@ Examples : -#endif // _USER_CONFIG_OVERRIDE_H_ \ No newline at end of file +#endif // _USER_CONFIG_OVERRIDE_H_ From e7e378f463bf9a3232506d671d1b85be5aad69bd Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 17 Jan 2019 17:48:34 +0100 Subject: [PATCH 04/10] Add (S)SerialSend features * Fix mDNS addService (#4938, #4951) * Add (S)SerialSend3 escape sequence \x to allow hexadecimal byte value (#3560, #4947) * Add SerialBridge command SSerialSend5 --- sonoff/_changelog.ino | 4 +++- sonoff/sonoff.ino | 4 ++-- sonoff/support.ino | 27 ++++++++++++++++++++------- sonoff/xdrv_08_serial_bridge.ino | 24 ++++++++++++++++++++---- sonoff/xnrg_03_pzem004t.ino | 2 +- sonoff/xnrg_04_mcp39f501.ino | 2 +- sonoff/xnrg_05_pzem_ac.ino | 2 +- sonoff/xnrg_06_pzem_dc.ino | 2 +- sonoff/xsns_15_mhz19.ino | 2 +- sonoff/xsns_18_pms5003.ino | 2 +- sonoff/xsns_20_novasds.ino | 2 +- sonoff/xsns_38_az7798.ino | 2 +- 12 files changed, 53 insertions(+), 22 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 27b47a02f..2d77e925f 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,7 +1,9 @@ /* 6.4.1.9 20190115 * Add support for Mi LED Desk Lamp with rotary switch (#4887) - * Fix mDNS addService (#4938) + * Fix mDNS addService (#4938, #4951) * Fix allowable MAX_RULE_VARS to 16 (#4933) + * Add (S)SerialSend3 escape sequence \x to allow hexadecimal byte value (#3560, #4947) + * Add SerialBridge command SSerialSend5 * * 6.4.1.8 20190107 * Change sonoff_template.h layout regarding optional module flags like ADC0 diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index f16a3ba10..a4fe3bf38 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -1051,7 +1051,7 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len) Serial.printf("%s", Unescape(dataBuf, &dat_len)); // "Hello\f" } else if (5 == index) { - SerialSendRaw(RemoveSpace(dataBuf)); // "AA004566" + SerialSendRaw(RemoveSpace(dataBuf)); // "AA004566" as hex values } snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_JSON_DONE); } @@ -1427,7 +1427,7 @@ void ExecuteCommandPower(byte device, byte state, int source) interlock_mutex = 0; } } - if ( Settings.flag.interlock && !interlock_mutex && !Settings.flag3.split_interlock) { //execute regular interlock-mode as interlock-split is off + if ( Settings.flag.interlock && !interlock_mutex && !Settings.flag3.split_interlock) { //execute regular interlock-mode as interlock-split is off interlock_mutex = 1; for (byte i = 0; i < devices_present; i++) { power_t imask = 1 << i; diff --git a/sonoff/support.ino b/sonoff/support.ino index 55be05df7..148ae8783 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -210,10 +210,12 @@ char* Unescape(char* buffer, uint16_t* size) { uint8_t* read = (uint8_t*)buffer; uint8_t* write = (uint8_t*)buffer; - uint16_t start_size = *size; - uint16_t end_size = *size; + int16_t start_size = *size; + int16_t end_size = *size; uint8_t che = 0; +// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)buffer, *size); + while (start_size > 0) { uint8_t ch = *read++; start_size--; @@ -235,6 +237,14 @@ char* Unescape(char* buffer, uint16_t* size) case 's': che = ' '; break; // 20 Space case 't': che = '\t'; break; // 09 Horizontal tab case 'v': che = '\v'; break; // 0B Vertical tab + case 'x': { + uint8_t* start = read; + che = (uint8_t)strtol((const char*)read, (char**)&read, 16); + start_size -= (uint16_t)(read - start); + end_size -= (uint16_t)(read - start); + break; + } + case '"': che = '\"'; break; // 22 Quotation mark // case '?': che = '\?'; break; // 3F Question mark default : { che = chi; @@ -247,6 +257,9 @@ char* Unescape(char* buffer, uint16_t* size) } } *size = end_size; + +// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)buffer, *size); + return buffer; } @@ -511,7 +524,7 @@ void ModuleGpios(myio *gp) memcpy_P(&src, &kModules[Settings.module].gp, sizeof(mycfgio)); // 11 85 00 85 85 00 00 00 15 38 85 00 00 81 -// AddLogSerial(LOG_LEVEL_DEBUG, (uint8_t *)&src, sizeof(mycfgio)); +// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t *)&src, sizeof(mycfgio)); for (uint8_t i = 0; i < sizeof(mycfgio); i++) { if (i < 6) { @@ -526,7 +539,7 @@ void ModuleGpios(myio *gp) } // 11 85 00 85 85 00 00 00 00 00 00 00 15 38 85 00 00 81 -// AddLogSerial(LOG_LEVEL_DEBUG, (uint8_t *)gp, sizeof(myio)); +// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t *)gp, sizeof(myio)); } gpio_flag ModuleFlag() @@ -1197,9 +1210,9 @@ void AddLog_P(byte loglevel, const char *formatP, const char *formatP2) AddLog(loglevel); } -void AddLogSerial(byte loglevel, uint8_t *buffer, int count) +void AddLogBuffer(byte loglevel, uint8_t *buffer, int count) { - snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SERIAL D_RECEIVED)); + snprintf_P(log_data, sizeof(log_data), PSTR("DMP:")); for (int i = 0; i < count; i++) { snprintf_P(log_data, sizeof(log_data), PSTR("%s %02X"), log_data, *(buffer++)); } @@ -1208,7 +1221,7 @@ void AddLogSerial(byte loglevel, uint8_t *buffer, int count) void AddLogSerial(byte loglevel) { - AddLogSerial(loglevel, (uint8_t*)serial_in_buffer, serial_in_byte_counter); + AddLogBuffer(loglevel, (uint8_t*)serial_in_buffer, serial_in_byte_counter); } void AddLogMissed(char *sensor, uint8_t misses) diff --git a/sonoff/xdrv_08_serial_bridge.ino b/sonoff/xdrv_08_serial_bridge.ino index 08a4f7211..80ec37178 100644 --- a/sonoff/xdrv_08_serial_bridge.ino +++ b/sonoff/xdrv_08_serial_bridge.ino @@ -99,18 +99,34 @@ boolean SerialBridgeCommand(void) if (-1 == command_code) { serviced = false; // Unknown command } - else if ((CMND_SSERIALSEND == command_code) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= 3)) { - if (XdrvMailbox.data_len > 0) { + else if ((CMND_SSERIALSEND == command_code) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= 5)) { + if (XdrvMailbox.data_len > 0) { // "Hello Tiger\n" if (1 == XdrvMailbox.index) { SerialBridgeSerial->write(XdrvMailbox.data, XdrvMailbox.data_len); SerialBridgeSerial->write("\n"); } - else if (2 == XdrvMailbox.index) { + else if ((2 == XdrvMailbox.index) || (4 == XdrvMailbox.index)) { // "Hello Tiger" or "A0" SerialBridgeSerial->write(XdrvMailbox.data, XdrvMailbox.data_len); } - else if (3 == XdrvMailbox.index) { + else if (3 == XdrvMailbox.index) { // "Hello\f" SerialBridgeSerial->write(Unescape(XdrvMailbox.data, &XdrvMailbox.data_len), XdrvMailbox.data_len); } + else if (5 == XdrvMailbox.index) { // "AA004566" as hex values + char *p; + char stemp[3]; + uint8_t code; + + char *codes = RemoveSpace(XdrvMailbox.data); + int size = strlen(XdrvMailbox.data); + + while (size > 0) { + snprintf(stemp, sizeof(stemp), codes); + code = strtol(stemp, &p, 16); + SerialBridgeSerial->write(code); + size -= 2; + codes += 2; + } + } snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_JSON_DONE); } } diff --git a/sonoff/xnrg_03_pzem004t.ino b/sonoff/xnrg_03_pzem004t.ino index 846c14431..34bff43fe 100644 --- a/sonoff/xnrg_03_pzem004t.ino +++ b/sonoff/xnrg_03_pzem004t.ino @@ -122,7 +122,7 @@ bool PzemRecieve(uint8_t resp, float *data) } } - AddLogSerial(LOG_LEVEL_DEBUG_MORE, buffer, len); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, len); if (len != sizeof(PZEMCommand)) { // AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Pzem comms timeout")); diff --git a/sonoff/xnrg_04_mcp39f501.ino b/sonoff/xnrg_04_mcp39f501.ino index 0301bf040..1ff04fb7d 100644 --- a/sonoff/xnrg_04_mcp39f501.ino +++ b/sonoff/xnrg_04_mcp39f501.ino @@ -143,7 +143,7 @@ void McpSend(uint8_t *data) data[0] = MCP_START_FRAME; data[data[1] -1] = McpChecksum(data); -// AddLogSerial(LOG_LEVEL_DEBUG_MORE, data, data[1]); +// AddLogBuffer(LOG_LEVEL_DEBUG_MORE, data, data[1]); for (byte i = 0; i < data[1]; i++) { Serial.write(data[i]); diff --git a/sonoff/xnrg_05_pzem_ac.ino b/sonoff/xnrg_05_pzem_ac.ino index a171b4079..f01ebad3e 100644 --- a/sonoff/xnrg_05_pzem_ac.ino +++ b/sonoff/xnrg_05_pzem_ac.ino @@ -46,7 +46,7 @@ void PzemAcEverySecond(void) uint8_t buffer[26]; uint8_t error = PzemAcModbus->ReceiveBuffer(buffer, 10); - AddLogSerial(LOG_LEVEL_DEBUG_MORE, buffer, (buffer[2]) ? buffer[2] +5 : sizeof(buffer)); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, (buffer[2]) ? buffer[2] +5 : sizeof(buffer)); if (error) { snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "PzemAc response error %d"), error); diff --git a/sonoff/xnrg_06_pzem_dc.ino b/sonoff/xnrg_06_pzem_dc.ino index 2adda5092..536cb4b97 100644 --- a/sonoff/xnrg_06_pzem_dc.ino +++ b/sonoff/xnrg_06_pzem_dc.ino @@ -46,7 +46,7 @@ void PzemDcEverySecond(void) uint8_t buffer[22]; uint8_t error = PzemDcModbus->ReceiveBuffer(buffer, 8); - AddLogSerial(LOG_LEVEL_DEBUG_MORE, buffer, (buffer[2]) ? buffer[2] +5 : sizeof(buffer)); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, (buffer[2]) ? buffer[2] +5 : sizeof(buffer)); if (error) { snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "PzemDc response error %d"), error); diff --git a/sonoff/xsns_15_mhz19.ino b/sonoff/xsns_15_mhz19.ino index d04d5a0c5..bc8e46ada 100644 --- a/sonoff/xsns_15_mhz19.ino +++ b/sonoff/xsns_15_mhz19.ino @@ -198,7 +198,7 @@ void MhzEverySecond(void) } } - AddLogSerial(LOG_LEVEL_DEBUG_MORE, mhz_response, counter); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, mhz_response, counter); if (counter < 9) { // AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "MH-Z19 comms timeout")); diff --git a/sonoff/xsns_18_pms5003.ino b/sonoff/xsns_18_pms5003.ino index 50ad9f7af..1f048134a 100644 --- a/sonoff/xsns_18_pms5003.ino +++ b/sonoff/xsns_18_pms5003.ino @@ -62,7 +62,7 @@ boolean PmsReadData(void) PmsSerial->readBytes(buffer, 32); PmsSerial->flush(); // Make room for another burst - AddLogSerial(LOG_LEVEL_DEBUG_MORE, buffer, 32); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, 32); // get checksum ready for (uint8_t i = 0; i < 30; i++) { diff --git a/sonoff/xsns_20_novasds.ino b/sonoff/xsns_20_novasds.ino index e3e1e48e9..1f94dea6a 100644 --- a/sonoff/xsns_20_novasds.ino +++ b/sonoff/xsns_20_novasds.ino @@ -108,7 +108,7 @@ bool NovaSdsCommand(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint16_t sensor // read rest (9 of 10 bytes) of message NovaSdsSerial->readBytes(&recbuf[1], 9); - AddLogSerial(LOG_LEVEL_DEBUG_MORE, recbuf, sizeof(recbuf)); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, recbuf, sizeof(recbuf)); if ( NULL != buffer ) { // return data to buffer diff --git a/sonoff/xsns_38_az7798.ino b/sonoff/xsns_38_az7798.ino index d53b657c7..433b380b0 100644 --- a/sonoff/xsns_38_az7798.ino +++ b/sonoff/xsns_38_az7798.ino @@ -158,7 +158,7 @@ void AzEverySecond(void) } } while(((millis() - start) < AZ_READ_TIMEOUT) && (counter < sizeof(az_response)) && !az_received); - AddLogSerial(LOG_LEVEL_DEBUG_MORE, az_response, counter); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, az_response, counter); if (!az_received) { AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "AZ7798 comms timeout")); From c963900709ee8fe014ce71c670a5aea1dfff6055 Mon Sep 17 00:00:00 2001 From: Mike <7153163+hackbar@users.noreply.github.com> Date: Thu, 17 Jan 2019 19:09:58 -0800 Subject: [PATCH 05/10] If the MDNS hostname is set, use it to verify the cert name. --- sonoff/xdrv_02_mqtt.ino | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/sonoff/xdrv_02_mqtt.ino b/sonoff/xdrv_02_mqtt.ino index 4321cb7b0..0b903f463 100644 --- a/sonoff/xdrv_02_mqtt.ino +++ b/sonoff/xdrv_02_mqtt.ino @@ -495,7 +495,19 @@ boolean MqttCheckTls(void) AddLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR(D_VERIFIED "2")); result = true; } -#endif +#ifdef MDNS_HOSTNAME + // If the hostname is set, check that as well. + // This lets certs with the hostname for the CN be used. + else if (EspClient.verify(fingerprint1, MDNS_HOSTNAME)) { + AddLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR(D_VERIFIED "1")); + result = true; + } + else if (EspClient.verify(fingerprint2, MDNS_HOSTNAME)) { + AddLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR(D_VERIFIED "2")); + result = true; + } +#endif // MDNS_HOSTNAME +#endif // USE_MQTT_TLS_CA_CERT } if (!result) AddLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR(D_FAILED)); EspClient.stop(); From b5824fc361505ba645b5e84c9825202f03a98c6e Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 18 Jan 2019 10:52:18 +0100 Subject: [PATCH 06/10] Housekeeping --- sonoff/settings.h | 4 +--- sonoff/sonoff.ino | 6 ++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/sonoff/settings.h b/sonoff/settings.h index 714dee800..a6f846aec 100644 --- a/sonoff/settings.h +++ b/sonoff/settings.h @@ -271,9 +271,7 @@ struct SYSCFG { uint8_t ws_color[4][3]; // 475 uint8_t ws_width[3]; // 481 myio my_gp; // 484 - - byte free_495[1]; // 495 - + uint8_t test_step; // 495 uint16_t light_pixels; // 496 uint8_t light_color[5]; // 498 uint8_t light_correction; // 49D diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index a4fe3bf38..878a24d50 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -2445,12 +2445,14 @@ void setup(void) Settings.rule_enabled = 0; // Disable all rules } if (RtcReboot.fast_reboot_count > 4) { // Restarted 5 times - Settings.module = SONOFF_BASIC; // Reset module to Sonoff Basic -// Settings.last_module = SONOFF_BASIC; for (byte i = 0; i < sizeof(Settings.my_gp); i++) { Settings.my_gp.io[i] = GPIO_NONE; // Reset user defined GPIO disabling sensors } } + if (RtcReboot.fast_reboot_count > 5) { // Restarted 6 times + Settings.module = SONOFF_BASIC; // Reset module to Sonoff Basic +// Settings.last_module = SONOFF_BASIC; + } snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_LOG_SOME_SETTINGS_RESET " (%d)"), RtcReboot.fast_reboot_count); AddLog(LOG_LEVEL_DEBUG); } From 99bb6977bf9dad2b43c8455dfc65ffc36f03d979 Mon Sep 17 00:00:00 2001 From: Staars Date: Fri, 18 Jan 2019 22:10:22 +0100 Subject: [PATCH 07/10] make tsl2561 compatible with SHT3x --- sonoff/xsns_16_tsl2561.ino | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/sonoff/xsns_16_tsl2561.ino b/sonoff/xsns_16_tsl2561.ino index 063adfb87..07c95941e 100644 --- a/sonoff/xsns_16_tsl2561.ino +++ b/sonoff/xsns_16_tsl2561.ino @@ -48,7 +48,6 @@ bool Tsl2561Read(void) uint16_t scaledFull, scaledIr; uint32_t full, ir; - if (Tsl.available()) { if (Tsl.on()) { if (Tsl.id(id) && Tsl2561Util::autoGain(Tsl, gain, exposure, scaledFull, scaledIr) @@ -58,7 +57,6 @@ bool Tsl2561Read(void) tsl2561_milliLux = 0; } } - } tsl2561_valid = SENSOR_MAX_MISS; return true; } @@ -67,9 +65,9 @@ void Tsl2561Detect(void) { if (tsl2561_type) { return; } - if (!Tsl.available()) { + if (I2cDevice(0x29) || I2cDevice(0x39) || I2cDevice(0x49)) { Tsl.begin(); - if (Tsl.available()) { + if (Tsl.on()) { tsl2561_type = 1; snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, tsl2561_types, Tsl.address()); AddLog(LOG_LEVEL_DEBUG); @@ -88,7 +86,7 @@ void Tsl2561EverySecond(void) if (tsl2561_type) { if (!Tsl2561Read()) { AddLogMissed(tsl2561_types, tsl2561_valid); -// if (!tsl2561_valid) { tsl2561_type = 0; } + if (!tsl2561_valid) { tsl2561_type = 0; } } } } From a5059a7a93e7f774361808993ebafed13840ec9e Mon Sep 17 00:00:00 2001 From: Mikka20 Date: Sat, 19 Jan 2019 22:32:12 +0100 Subject: [PATCH 08/10] Update README.md Please fix this typo :-) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 09354028a..1d2ad635b 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ If you want to compile Sonoff-Tasmota yourself keep in mind the following: - Only Flash Mode **DOUT** is supported. Do not use Flash Mode DIO / QIO / QOUT as it might seem to brick your device. See [Wiki](https://github.com/arendst/Sonoff-Tasmota/wiki/Theo's-Tasmota-Tips) for background information. - Sonoff-Tasmota uses a 1M linker script WITHOUT spiffs **1M (no SPIFFS)** for optimal code space. If you compile using ESP/Arduino library 2.3.0 then download the provided new linker script to your Arduino IDE or Platformio base folder. Later version of ESP/Arduino library already contain the correct linker script. See [Wiki > Prerequisite](https://github.com/arendst/Sonoff-Tasmota/wiki/Prerequisite). -- To make compile time changes to Sonoff-Tasmota it can use the ``user_config_override.h`` file. It assures keeping your settings when you download and compile a new version. To use ``user_config.override.h`` you will have to make a copy of the provided ``user_config.override_sample.h`` file and add your setting overrides. To enable the override file you will need to use a compile define as documented in the ``user_config_override_sample.h`` file. +- To make compile time changes to Sonoff-Tasmota it can use the ``user_config_override.h`` file. It assures keeping your settings when you download and compile a new version. To use ``user_config.override.h`` you will have to make a copy of the provided ``user_config_override_sample.h`` file and add your setting overrides. To enable the override file you will need to use a compile define as documented in the ``user_config_override_sample.h`` file. ### Version Information - Sonoff-Tasmota provides all (Sonoff) modules in one file and starts with module Sonoff Basic. From 2b7fbe22e3f3373857e266b7d07250c27a1ce0f3 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 20 Jan 2019 16:57:07 +0100 Subject: [PATCH 09/10] Fix MQTT host detection Fix MQTT host detection --- sonoff/xdrv_02_mqtt.ino | 47 +++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/sonoff/xdrv_02_mqtt.ino b/sonoff/xdrv_02_mqtt.ino index 0b903f463..b6ceec2f6 100644 --- a/sonoff/xdrv_02_mqtt.ino +++ b/sonoff/xdrv_02_mqtt.ino @@ -76,6 +76,7 @@ const char kMqttCommands[] PROGMEM = uint16_t mqtt_retry_counter = 1; // MQTT connection retry counter uint8_t mqtt_initial_connection_state = 2; // MQTT connection messages state bool mqtt_connected = false; // MQTT virtual connection status +bool mqtt_allowed = false; // MQTT enabled and parameters valid /*********************************************************************************************\ * MQTT driver specific code need to provide the following functions: @@ -210,9 +211,9 @@ void MqttLoop(void) #ifdef USE_DISCOVERY #ifdef MQTT_HOST_DISCOVERY -boolean MqttDiscoverServer(void) +void MqttDiscoverServer(void) { - if (!mdns_begun) { return false; } + if (!mdns_begun) { return; } int n = MDNS.queryService("mqtt", "tcp"); // Search for mqtt service @@ -220,26 +221,21 @@ boolean MqttDiscoverServer(void) AddLog(LOG_LEVEL_INFO); if (n > 0) { - #ifdef MDNS_HOSTNAME - for (int i = 0; i < n; i++) { + uint8_t i = 0; // If the hostname isn't set, use the first record found. +#ifdef MDNS_HOSTNAME + for (i = n; i > 0; i--) { // Search from last to first and use first if not found if (!strcmp(MDNS.hostname(i).c_str(), MDNS_HOSTNAME)) { - snprintf_P(Settings.mqtt_host, sizeof(Settings.mqtt_host), MDNS.IP(i).toString().c_str()); - Settings.mqtt_port = MDNS.port(i); - break; // stop at the first matching record + break; // Stop at matching record } } - #else - // If the hostname isn't set, use the first record found. - snprintf_P(Settings.mqtt_host, sizeof(Settings.mqtt_host), MDNS.IP(0).toString().c_str()); - Settings.mqtt_port = MDNS.port(0); - #endif // MDNS_HOSTNAME +#endif // MDNS_HOSTNAME + snprintf_P(Settings.mqtt_host, sizeof(Settings.mqtt_host), MDNS.IP(i).toString().c_str()); + Settings.mqtt_port = MDNS.port(i); snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_MDNS D_MQTT_SERVICE_FOUND " %s, " D_IP_ADDRESS " %s, " D_PORT " %d"), - MDNS.hostname(0).c_str(), Settings.mqtt_host, Settings.mqtt_port); + MDNS.hostname(i).c_str(), Settings.mqtt_host, Settings.mqtt_port); AddLog(LOG_LEVEL_INFO); } - - return n > 0; } #endif // MQTT_HOST_DISCOVERY #endif // USE_DISCOVERY @@ -398,7 +394,7 @@ void MqttConnected(void) { char stopic[TOPSZ]; - if (Settings.flag.mqtt_enabled) { + if (mqtt_allowed) { AddLog_P(LOG_LEVEL_INFO, S_LOG_MQTT, PSTR(D_CONNECTED)); mqtt_connected = true; mqtt_retry_counter = 0; @@ -520,7 +516,18 @@ void MqttReconnect(void) { char stopic[TOPSZ]; - if (!Settings.flag.mqtt_enabled) { + mqtt_allowed = Settings.flag.mqtt_enabled; + if (mqtt_allowed) { +#ifdef USE_DISCOVERY +#ifdef MQTT_HOST_DISCOVERY + MqttDiscoverServer(); +#endif // MQTT_HOST_DISCOVERY +#endif // USE_DISCOVERY + if (!strlen(Settings.mqtt_host) || !Settings.mqtt_port) { + mqtt_allowed = false; + } + } + if (!mqtt_allowed) { MqttConnected(); return; } @@ -535,12 +542,6 @@ void MqttReconnect(void) mqtt_retry_counter = Settings.mqtt_retry; global_state.mqtt_down = 1; -#ifdef USE_DISCOVERY -#ifdef MQTT_HOST_DISCOVERY - if (!MqttDiscoverServer() && !strlen(Settings.mqtt_host)) { return; } -#endif // MQTT_HOST_DISCOVERY -#endif // USE_DISCOVERY - char *mqtt_user = NULL; char *mqtt_pwd = NULL; if (strlen(Settings.mqtt_user) > 0) mqtt_user = Settings.mqtt_user; From dcabb9c6dc1668209a6a770446253b79efbe1cc5 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 21 Jan 2019 22:18:19 +0100 Subject: [PATCH 10/10] 6.4.1.10 Fix Hass discovery of MHZ19(B) 6.4.1.10 20190121 * Fix Hass discovery of MHZ19(B) sensors (#4992) --- sonoff/_changelog.ino | 5 ++++- sonoff/i18n.h | 1 + sonoff/sonoff_version.h | 2 +- sonoff/xsns_15_mhz19.ino | 13 +++++++------ 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 2d77e925f..8db379f8b 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,4 +1,7 @@ -/* 6.4.1.9 20190115 +/* 6.4.1.10 20190121 + * Fix Hass discovery of MHZ19(B) sensors (#4992) + * + * 6.4.1.9 20190115 * Add support for Mi LED Desk Lamp with rotary switch (#4887) * Fix mDNS addService (#4938, #4951) * Fix allowable MAX_RULE_VARS to 16 (#4933) diff --git a/sonoff/i18n.h b/sonoff/i18n.h index a90028e8d..1003a3996 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -92,6 +92,7 @@ #define D_JSON_MAC "Mac" #define D_JSON_MASK "Mask" #define D_JSON_MINIMAL "minimal" +#define D_JSON_MODEL "Model" #define D_JSON_NO "No" #define D_JSON_NOISE "Noise" #define D_JSON_NONE "None" diff --git a/sonoff/sonoff_version.h b/sonoff/sonoff_version.h index 534276418..47bbb82f2 100644 --- a/sonoff/sonoff_version.h +++ b/sonoff/sonoff_version.h @@ -20,7 +20,7 @@ #ifndef _SONOFF_VERSION_H_ #define _SONOFF_VERSION_H_ -#define VERSION 0x06040109 +#define VERSION 0x0604010A #define D_PROGRAMNAME "Sonoff-Tasmota" #define D_AUTHOR "Theo Arends" diff --git a/sonoff/xsns_15_mhz19.ino b/sonoff/xsns_15_mhz19.ino index bc8e46ada..7585ff3d9 100644 --- a/sonoff/xsns_15_mhz19.ino +++ b/sonoff/xsns_15_mhz19.ino @@ -70,7 +70,7 @@ enum MhzFilterOptions {MHZ19_FILTER_OFF, MHZ19_FILTER_OFF_ALLSAMPLES, MHZ19_FILT TasmotaSerial *MhzSerial; -const char kMhzTypes[] PROGMEM = "MHZ19|MHZ19B"; +const char kMhzModels[] PROGMEM = "|B"; enum MhzCommands { MHZ_CMND_READPPM, MHZ_CMND_ABCENABLE, MHZ_CMND_ABCDISABLE, MHZ_CMND_ZEROPOINT, MHZ_CMND_RESET, MHZ_CMND_RANGE_1000, MHZ_CMND_RANGE_2000, MHZ_CMND_RANGE_3000, MHZ_CMND_RANGE_5000 }; const uint8_t kMhzCommands[][4] PROGMEM = { @@ -90,7 +90,6 @@ uint16_t mhz_last_ppm = 0; uint8_t mhz_filter = MHZ19_FILTER_OPTION; bool mhz_abc_enable = MHZ19_ABC_ENABLE; bool mhz_abc_must_apply = false; -char mhz_types[7]; float mhz_temperature = 0; uint8_t mhz_retry = MHZ19_RETRY_COUNT; @@ -322,19 +321,21 @@ void MhzInit(void) void MhzShow(boolean json) { + char types[7] = "MHZ19B"; // MHZ19B for legacy reasons. Prefered is MHZ19 char temperature[33]; dtostrfd(mhz_temperature, Settings.flag2.temperature_resolution, temperature); - GetTextIndexed(mhz_types, sizeof(mhz_types), mhz_type -1, kMhzTypes); + char model[3]; + GetTextIndexed(model, sizeof(model), mhz_type -1, kMhzModels); if (json) { - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"" D_JSON_CO2 "\":%d,\"" D_JSON_TEMPERATURE "\":%s}"), mqtt_data, mhz_types, mhz_last_ppm, temperature); + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"" D_JSON_MODEL "\":\"%s\",\"" D_JSON_CO2 "\":%d,\"" D_JSON_TEMPERATURE "\":%s}"), mqtt_data, types, model, mhz_last_ppm, temperature); #ifdef USE_DOMOTICZ if (0 == tele_period) DomoticzSensor(DZ_AIRQUALITY, mhz_last_ppm); #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER } else { - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_CO2, mqtt_data, mhz_types, mhz_last_ppm); - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, mhz_types, temperature, TempUnit()); + snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_CO2, mqtt_data, types, mhz_last_ppm); + snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, types, temperature, TempUnit()); #endif // USE_WEBSERVER } }