From 76c102c09c4b7532e56b22fd1380a50f9bf942fc Mon Sep 17 00:00:00 2001 From: Erik Date: Fri, 27 Jul 2018 12:17:34 +0200 Subject: [PATCH] Support send / receive of binary serial data --- sonoff/settings.h | 2 +- sonoff/settings.ino | 4 ++-- sonoff/sonoff.ino | 27 ++++++++++++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/sonoff/settings.h b/sonoff/settings.h index e527d74b7..9abfa9fb9 100644 --- a/sonoff/settings.h +++ b/sonoff/settings.h @@ -48,7 +48,7 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu uint32_t not_power_linked : 1; // bit 20 (v5.11.1f) uint32_t no_power_on_check : 1; // bit 21 (v5.11.1i) uint32_t mqtt_serial : 1; // bit 22 (v5.12.0f) - uint32_t rules_enabled : 1; // bit 23 (v5.12.0j) - free since v5.14.0b + uint32_t mqtt_serial_raw : 1; // bit 23 (vTDB) uint32_t rules_once : 1; // bit 24 (v5.12.0k) - free since v5.14.0b uint32_t knx_enabled : 1; // bit 25 (v5.12.0l) KNX uint32_t device_index_enable : 1; // bit 26 (v5.13.1a) diff --git a/sonoff/settings.ino b/sonoff/settings.ino index d5541298d..10fbd10ed 100644 --- a/sonoff/settings.ino +++ b/sonoff/settings.ino @@ -756,7 +756,7 @@ void SettingsDelta() } if (Settings.version < 0x050E0002) { for (byte i = 1; i < MAX_RULE_SETS; i++) { Settings.rules[i][0] = '\0'; } - Settings.rule_enabled = Settings.flag.rules_enabled; + Settings.rule_enabled = Settings.flag.mqtt_serial_raw; Settings.rule_once = Settings.flag.rules_once; } if (Settings.version < 0x06000000) { @@ -778,7 +778,7 @@ void SettingsDelta() } } if (Settings.version < 0x06000003) { - Settings.flag.rules_enabled = 0; + Settings.flag.mqtt_serial_raw = 0; Settings.flag.rules_once = 0; Settings.flag3.data = 0; } diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 7b63160fb..a302d65fe 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -915,15 +915,19 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len) } snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_NVALUE, command, Settings.baudrate * 1200); } - else if ((CMND_SERIALSEND == command_code) && (index > 0) && (index <= 3)) { + else if ((CMND_SERIALSEND == command_code) && (index > 0) && (index <= 4)) { SetSeriallog(LOG_LEVEL_NONE); Settings.flag.mqtt_serial = 1; + Settings.flag.mqtt_serial_raw = (4 == index)?1:0; if (data_len > 0) { if (1 == index) { Serial.printf("%s\n", dataBuf); } - else if (2 == index) { - Serial.printf("%s", dataBuf); + else if (2 == index || 4 == index) { + for (int i=0; i 127) { // binary data... + if (serial_in_byte > 127 && !Settings.flag.mqtt_serial_raw) { // binary data... serial_in_byte_counter = 0; Serial.flush(); return; @@ -2190,8 +2194,9 @@ void SerialInput() } } } else { - if (serial_in_byte) { - if ((serial_in_byte_counter < INPUT_BUFFER_SIZE -1) && (serial_in_byte != Settings.serial_delimiter)) { // add char to string if it still fits + if (serial_in_byte || Settings.flag.mqtt_serial_raw) { + if ((serial_in_byte_counter < INPUT_BUFFER_SIZE -1) && + ((serial_in_byte != Settings.serial_delimiter) || Settings.flag.mqtt_serial_raw)) { // add char to string if it still fits serial_in_buffer[serial_in_byte_counter++] = serial_in_byte; serial_polling_window = millis(); } else { @@ -2231,7 +2236,15 @@ void SerialInput() if (Settings.flag.mqtt_serial && serial_in_byte_counter && (millis() > (serial_polling_window + SERIAL_POLLING))) { serial_in_buffer[serial_in_byte_counter] = 0; // serial data completed - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_JSON_SERIALRECEIVED "\":\"%s\"}"), serial_in_buffer); + if (!Settings.flag.mqtt_serial_raw) { + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_JSON_SERIALRECEIVED "\":\"%s\"}"), serial_in_buffer); + } else { + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_JSON_SERIALRECEIVED "\":\"")); + for (int i=0; i