Skip to content

Commit

Permalink
Merge pull request #3345 from emontnemery/serial_bridge_raw
Browse files Browse the repository at this point in the history
Support send / receive of binary serial data
  • Loading branch information
arendst authored Jul 28, 2018
2 parents da7dada + 76c102c commit ade9535
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion sonoff/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions sonoff/settings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
27 changes: 20 additions & 7 deletions sonoff/sonoff.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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<data_len; i++)
{
Serial.write(dataBuf[i]);
}
}
else if (3 == index) {
uint16_t dat_len = data_len;
Expand Down Expand Up @@ -2176,7 +2180,7 @@ void SerialInput()
#endif // USE_ENERGY_SENSOR
/*-------------------------------------------------------------------------------------------*/

if (serial_in_byte > 127) { // binary data...
if (serial_in_byte > 127 && !Settings.flag.mqtt_serial_raw) { // binary data...
serial_in_byte_counter = 0;
Serial.flush();
return;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<serial_in_byte_counter; i++) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%02x"), mqtt_data, serial_in_buffer[i]);
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s\"}"), mqtt_data);
}
MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_SERIALRECEIVED));
// XdrvRulesProcess();
serial_in_byte_counter = 0;
Expand Down

0 comments on commit ade9535

Please sign in to comment.