2017-07-30 16:55:37 +01:00
|
|
|
/*
|
|
|
|
xdrv_snfbridge.ino - sonoff RF bridge 433 support for Sonoff-Tasmota
|
|
|
|
|
|
|
|
Copyright (C) 2017 Theo Arends
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
Sonoff RF Bridge 433
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2017-08-29 15:35:45 +01:00
|
|
|
#define SFB_TIME_AVOID_DUPLICATE 2000 // Milliseconds
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
uint8_t sonoff_bridge_receive_flag = 0;
|
|
|
|
uint8_t sonoff_bridge_learn_key = 1;
|
|
|
|
uint8_t sonoff_bridge_learn_active = 0;
|
|
|
|
uint32_t sonoff_bridge_last_received_id = 0;
|
|
|
|
unsigned long sonoff_bridge_last_time = 0;
|
2017-07-30 16:55:37 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void SonoffBridgeReceived()
|
2017-07-30 16:55:37 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
uint16_t sync_time = 0;
|
|
|
|
uint16_t low_time = 0;
|
|
|
|
uint16_t high_time = 0;
|
|
|
|
uint32_t received_id = 0;
|
2017-08-05 14:11:50 +01:00
|
|
|
char svalue[90];
|
|
|
|
char rfkey[8];
|
2017-07-30 16:55:37 +01:00
|
|
|
|
|
|
|
svalue[0] = '\0';
|
2017-10-18 17:22:34 +01:00
|
|
|
for (byte i = 0; i < serial_in_byte_counter; i++) {
|
|
|
|
snprintf_P(svalue, sizeof(svalue), PSTR("%s%02X "), svalue, serial_in_buffer[i]);
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_BRIDGE D_RECEIVED " %s"), svalue);
|
2017-10-18 17:22:34 +01:00
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
2017-07-30 16:55:37 +01:00
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
if (0xA2 == serial_in_buffer[0]) { // Learn timeout
|
|
|
|
sonoff_bridge_learn_active = 0;
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFKEY "%d\":\"" D_LEARN_FAILED "\"}"), sonoff_bridge_learn_key);
|
|
|
|
MqttPublishPrefixTopic_P(5, PSTR(D_CMND_RFKEY));
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
else if (0xA3 == serial_in_buffer[0]) { // Learned A3 20 F8 01 18 03 3E 2E 1A 22 55
|
|
|
|
sonoff_bridge_learn_active = 0;
|
|
|
|
low_time = serial_in_buffer[3] << 8 | serial_in_buffer[4]; // Low time in uSec
|
|
|
|
high_time = serial_in_buffer[5] << 8 | serial_in_buffer[6]; // High time in uSec
|
|
|
|
if (low_time && high_time) {
|
|
|
|
for (byte i = 0; i < 9; i++) {
|
|
|
|
Settings.rf_code[sonoff_bridge_learn_key][i] = serial_in_buffer[i +1];
|
2017-08-05 14:11:50 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFKEY "%d\":\"" D_LEARNED "\"}"), sonoff_bridge_learn_key);
|
2017-08-05 14:11:50 +01:00
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFKEY "%d\":\"" D_LEARN_FAILED "\"}"), sonoff_bridge_learn_key);
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
MqttPublishPrefixTopic_P(5, PSTR(D_CMND_RFKEY));
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
else if (0xA4 == serial_in_buffer[0]) { // Received RF data A4 20 EE 01 18 03 3E 2E 1A 22 55
|
|
|
|
sync_time = serial_in_buffer[1] << 8 | serial_in_buffer[2]; // Sync time in uSec
|
|
|
|
low_time = serial_in_buffer[3] << 8 | serial_in_buffer[4]; // Low time in uSec
|
|
|
|
high_time = serial_in_buffer[5] << 8 | serial_in_buffer[6]; // High time in uSec
|
|
|
|
received_id = serial_in_buffer[7] << 16 | serial_in_buffer[8] << 8 | serial_in_buffer[9];
|
2017-08-29 15:35:45 +01:00
|
|
|
|
|
|
|
unsigned long now = millis();
|
2017-10-18 17:22:34 +01:00
|
|
|
if (!((received_id == sonoff_bridge_last_received_id) && (now - sonoff_bridge_last_time < SFB_TIME_AVOID_DUPLICATE))) {
|
|
|
|
sonoff_bridge_last_received_id = received_id;
|
|
|
|
sonoff_bridge_last_time = now;
|
2017-09-06 16:37:41 +01:00
|
|
|
strncpy_P(rfkey, PSTR("\"" D_NONE "\""), sizeof(rfkey));
|
2017-10-18 17:22:34 +01:00
|
|
|
for (byte i = 1; i <= 16; i++) {
|
|
|
|
if (Settings.rf_code[i][0]) {
|
|
|
|
uint32_t send_id = Settings.rf_code[i][6] << 16 | Settings.rf_code[i][7] << 8 | Settings.rf_code[i][8];
|
|
|
|
if (send_id == received_id) {
|
2017-08-29 15:35:45 +01:00
|
|
|
snprintf_P(rfkey, sizeof(rfkey), PSTR("%d"), i);
|
|
|
|
break;
|
|
|
|
}
|
2017-08-05 14:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_RFRECEIVED "\":{\"" D_SYNC "\":%d, \"" D_LOW "\":%d, \"" D_HIGH "\":%d, \"" D_DATA "\":\"%06X\", \"" D_CMND_RFKEY "\":%s}}"),
|
2017-10-18 17:22:34 +01:00
|
|
|
sync_time, low_time, high_time, received_id, rfkey);
|
|
|
|
MqttPublishPrefixTopic_P(6, PSTR(D_RFRECEIVED));
|
2017-10-02 15:45:03 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
2017-10-18 17:22:34 +01:00
|
|
|
DomoticzSensor(DZ_COUNT, received_id); // Send rid as Domoticz Counter value
|
2017-10-08 15:51:05 +01:00
|
|
|
#endif // USE_DOMOTICZ
|
2017-08-05 14:11:50 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
}
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
boolean SonoffBridgeSerialInput()
|
2017-07-30 16:55:37 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
if (sonoff_bridge_receive_flag) {
|
|
|
|
if (!((serial_in_byte_counter == 0) && (serial_in_byte == 0))) { // Skip leading 0
|
|
|
|
serial_in_buffer[serial_in_byte_counter++] = serial_in_byte;
|
|
|
|
if (0x55 == serial_in_byte) { // 0x55 - End of text
|
|
|
|
SonoffBridgeReceived();
|
|
|
|
sonoff_bridge_receive_flag = 0;
|
2017-07-30 16:55:37 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
serial_in_byte = 0;
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
if (0xAA == serial_in_byte) { // 0xAA - Start of text
|
|
|
|
serial_in_byte_counter = 0;
|
|
|
|
serial_in_byte = 0;
|
|
|
|
sonoff_bridge_receive_flag = 1;
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void SonoffBridgeSendAck()
|
2017-07-30 16:55:37 +01:00
|
|
|
{
|
|
|
|
Serial.write(0xAA); // Start of Text
|
|
|
|
Serial.write(0xA0); // Acknowledge
|
|
|
|
Serial.write(0x55); // End of Text
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void SonoffBridgeSend(uint8_t idx, uint8_t key)
|
2017-07-30 16:55:37 +01:00
|
|
|
{
|
|
|
|
uint8_t code;
|
2017-09-02 13:37:02 +01:00
|
|
|
|
2017-07-30 16:55:37 +01:00
|
|
|
key--; // Support 1 to 16
|
|
|
|
Serial.write(0xAA); // Start of Text
|
|
|
|
Serial.write(0xA5); // Send following code
|
|
|
|
for (uint8_t i = 0; i < 8; i++) {
|
2017-10-18 17:22:34 +01:00
|
|
|
Serial.write(Settings.rf_code[idx][i]);
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
|
|
|
if (0 == idx) {
|
2017-10-10 14:40:02 +01:00
|
|
|
code = (0x10 << (key >> 2)) | (1 << (key & 3)); // 11,12,14,18,21,22,24,28,41,42,44,48,81,82,84,88
|
2017-07-30 16:55:37 +01:00
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
code = Settings.rf_code[idx][8];
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
|
|
|
Serial.write(code);
|
|
|
|
Serial.write(0x55); // End of Text
|
|
|
|
Serial.flush();
|
2017-10-02 15:45:03 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
2017-10-18 17:22:34 +01:00
|
|
|
// uint32_t rid = Settings.rf_code[idx][6] << 16 | Settings.rf_code[idx][7] << 8 | code;
|
|
|
|
// DomoticzSensor(DZ_COUNT, rid); // Send rid as Domoticz Counter value
|
2017-10-02 15:45:03 +01:00
|
|
|
#endif // USE_DOMOTICZ
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
void SonoffBridgeLearn(uint8_t key)
|
2017-07-30 16:55:37 +01:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
sonoff_bridge_learn_key = key;
|
|
|
|
sonoff_bridge_learn_active = 1;
|
2017-07-30 16:55:37 +01:00
|
|
|
Serial.write(0xAA); // Start of Text
|
|
|
|
Serial.write(0xA1); // Start learning
|
|
|
|
Serial.write(0x55); // End of Text
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Commands
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2017-10-18 17:22:34 +01:00
|
|
|
boolean SonoffBridgeCommand(char *type, uint16_t index, char *dataBuf, uint16_t data_len, int16_t payload)
|
2017-07-30 16:55:37 +01:00
|
|
|
{
|
|
|
|
boolean serviced = true;
|
|
|
|
char *p;
|
|
|
|
|
2017-09-02 13:37:02 +01:00
|
|
|
if (!strcasecmp_P(type, PSTR(D_CMND_RFDEFAULT))) {
|
2017-07-30 16:55:37 +01:00
|
|
|
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)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.rf_code[0][6] = msb;
|
|
|
|
Settings.rf_code[0][7] = lsb;
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFDEFAULT "\":\"%0X%0X\"}"), Settings.rf_code[0][6], Settings.rf_code[0][7]);
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
2017-09-02 13:37:02 +01:00
|
|
|
else if (!strcasecmp_P(type, PSTR(D_CMND_RFKEY)) && (index > 0) && (index <= 16)) {
|
2017-10-18 17:22:34 +01:00
|
|
|
if (!sonoff_bridge_learn_active) {
|
2017-07-30 16:55:37 +01:00
|
|
|
if (2 == payload) {
|
2017-10-18 17:22:34 +01:00
|
|
|
SonoffBridgeLearn(index);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFKEY "%d\":\"" D_START_LEARNING "\"}"), index);
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
|
|
|
else if (3 == payload) {
|
2017-10-18 17:22:34 +01:00
|
|
|
Settings.rf_code[index][0] = 0;
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFKEY "%d\":\"" D_SET_TO_DEFAULT "\"}"), index);
|
2017-07-30 16:55:37 +01:00
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
if ((1 == payload) || (0 == Settings.rf_code[index][0])) {
|
|
|
|
SonoffBridgeSend(0, index);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFKEY "%d\":\"" D_DEFAULT_SENT "\"}"), index);
|
2017-07-30 16:55:37 +01:00
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
SonoffBridgeSend(index, 0);
|
2017-09-13 13:19:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFKEY "%d\":\"" D_LEARNED_SENT "\"}"), index);
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-10-18 17:22:34 +01:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFKEY "%d\":\"" D_LEARNING_ACTIVE "\"}"), sonoff_bridge_learn_key);
|
2017-07-30 16:55:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
serviced = false; // Unknown command
|
|
|
|
}
|
|
|
|
return serviced;
|
|
|
|
}
|