diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 735894ce..b79c6523 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -1409,8 +1409,13 @@ #define RF_PIN 14 #endif +#ifndef RF_DEBOUNCE #define RF_DEBOUNCE 500 +#endif + +#ifndef RF_LEARN_TIMEOUT #define RF_LEARN_TIMEOUT 60000 +#endif //-------------------------------------------------------------------------------- // Custom RFM69 to MQTT bridge diff --git a/code/espurna/config/progmem.h b/code/espurna/config/progmem.h index 8974151c..d4cbf880 100644 --- a/code/espurna/config/progmem.h +++ b/code/espurna/config/progmem.h @@ -97,7 +97,7 @@ PROGMEM const char espurna_modules[] = #if RFM69_SUPPORT "RFM69 " #endif - #if RF_SUPPORT + #if RF_SUPPORT || defined(ITEAD_SONOFF_RFBRIDGE) "RF " #endif #if SCHEDULER_SUPPORT diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 0f545d75..82575539 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -148,7 +148,7 @@ void setup() { #if I2C_SUPPORT i2cSetup(); #endif - #ifdef ITEAD_SONOFF_RFBRIDGE + #if defined(ITEAD_SONOFF_RFBRIDGE) || RF_SUPPORT rfbSetup(); #endif #if ALEXA_SUPPORT @@ -166,9 +166,6 @@ void setup() { #if RFM69_SUPPORT rfm69Setup(); #endif - #if RF_SUPPORT - rfSetup(); - #endif #if IR_SUPPORT irSetup(); #endif diff --git a/code/espurna/rf.ino b/code/espurna/rf.ino deleted file mode 100644 index 65e50dc9..00000000 --- a/code/espurna/rf.ino +++ /dev/null @@ -1,253 +0,0 @@ -/* - -RF MODULE - -Copyright (C) 2016-2018 by Xose Pérez - -*/ - -#if RF_SUPPORT - -#include - -RCSwitch * _rfModem; - -unsigned long _rf_learn_start = 0; -unsigned char _rf_learn_id = 0; -bool _rf_learn_status = true; -bool _rf_learn_active = false; - -#if WEB_SUPPORT -#include -Ticker _rfb_sendcodes; -#endif - -// ----------------------------------------------------------------------------- -// RF -// ----------------------------------------------------------------------------- - -unsigned long _rfRetrieve(unsigned char id, bool status) { - String code = getSetting(status ? "rfbON" : "rfbOFF", id, "0"); - return strtoul(code.c_str(), 0, 16); -} - -void _rfStore(unsigned char id, bool status, unsigned long code) { - DEBUG_MSG_P(PSTR("[RF] Storing %d-%s => %X\n"), id, status ? "ON" : "OFF", code); - char buffer[20]; - snprintf_P(buffer, sizeof(buffer), PSTR("%X"), code); - setSetting(status ? "rfbON" : "rfbOFF", id, buffer); -} - -void _rfLearn(unsigned char id, bool status) { - _rf_learn_start = millis(); - _rf_learn_id = id; - _rf_learn_status = status; - _rf_learn_active = true; -} - -void _rfForget(unsigned char id, bool status) { - - delSetting(status ? "rfbON" : "rfbOFF", id); - - // Websocket update - #if WEB_SUPPORT - char wsb[100]; - snprintf_P(wsb, sizeof(wsb), PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"\"}]}"), id, status ? 1 : 0); - wsSend(wsb); - #endif - -} - -bool _rfMatch(unsigned long code, unsigned char& relayID, unsigned char& value) { - - bool found = false; - DEBUG_MSG_P(PSTR("[RF] Trying to match code %X\n"), code); - - for (unsigned char i=0; iargc < 3) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); - return; - } - - int id = String(e->argv[1]).toInt(); - if (id >= relayCount()) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong relayID (%d)\n"), id); - return; - } - - int status = String(e->argv[2]).toInt(); - - _rfLearn(id, status == 1); - - DEBUG_MSG_P(PSTR("+OK\n")); - - }); - - settingsRegisterCommand(F("FORGET"), [](Embedis* e) { - - if (e->argc < 3) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n")); - return; - } - - int id = String(e->argv[1]).toInt(); - if (id >= relayCount()) { - DEBUG_MSG_P(PSTR("-ERROR: Wrong relayID (%d)\n"), id); - return; - } - - int status = String(e->argv[2]).toInt(); - - _rfForget(id, status == 1); - - DEBUG_MSG_P(PSTR("+OK\n")); - - }); - -} - -#endif // TERMINAL_SUPPORT - -// ----------------------------------------------------------------------------- -// WEB -// ----------------------------------------------------------------------------- - -#if WEB_SUPPORT - -void _rfWebSocketSendCode(unsigned char id, bool status, unsigned long code) { - char wsb[100]; - snprintf_P(wsb, sizeof(wsb), PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"%X\"}]}"), id, status ? 1 : 0, code); - wsSend(wsb); -} - -void _rfWebSocketSendCodes() { - for (unsigned char id=0; idavailable()) { - - static unsigned long last = 0; - if (millis() - last > RF_DEBOUNCE) { - last = millis(); - - if (_rfModem->getReceivedValue() > 0) { - - unsigned long rf_code = _rfModem->getReceivedValue(); - - DEBUG_MSG_P(PSTR("[RF] Received code: %X\n"), rf_code); - - if (_rf_learn_active) { - - _rf_learn_active = false; - - _rfStore(_rf_learn_id, _rf_learn_status, rf_code); - - // Websocket update - #if WEB_SUPPORT - _rfWebSocketSendCode(_rf_learn_id, _rf_learn_status, rf_code); - #endif - - } else { - - unsigned char id; - unsigned char value; - if (_rfMatch(rf_code, id, value)) { - if (2 == value) { - relayToggle(id); - } else { - relayStatus(id, 1 == value); - } - } - - } - - } - - } - - _rfModem->resetAvailable(); - - } - - if (_rf_learn_active && (millis() - _rf_learn_start > RF_LEARN_TIMEOUT)) { - _rf_learn_active = false; - } - -} - -void rfSetup() { - - _rfModem = new RCSwitch(); - _rfModem->enableReceive(RF_PIN); - DEBUG_MSG_P(PSTR("[RF] RF receiver on GPIO %u\n"), RF_PIN); - - #if WEB_SUPPORT - wsOnSendRegister(_rfWebSocketOnSend); - wsOnActionRegister(_rfWebSocketOnAction); - #endif - - #if TERMINAL_SUPPORT - _rfInitCommands(); - #endif - - // Register loop - espurnaRegisterLoop(rfLoop); - -} - -#endif diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index f85babbd..7fab0bec 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -1,17 +1,17 @@ /* -ITEAD RF BRIDGE MODULE +RF MODULE -Copyright (C) 2017-2018 by Xose Pérez +Copyright (C) 2016-2018 by Xose Pérez */ -#ifdef ITEAD_SONOFF_RFBRIDGE +#if defined(ITEAD_SONOFF_RFBRIDGE) || RF_SUPPORT #include #include -#if RFB_DIRECT +#if RFB_DIRECT || RF_SUPPORT #include #endif @@ -53,6 +53,7 @@ unsigned char _learnId = 0; bool _learnStatus = true; bool _rfbin = false; +#if not RF_SUPPORT typedef struct { byte code[RF_MESSAGE_SIZE]; byte times; @@ -60,8 +61,9 @@ typedef struct { static std::queue _rfb_message_queue; Ticker _rfb_ticker; bool _rfb_ticker_active = false; +#endif -#if RFB_DIRECT +#if RFB_DIRECT || RF_SUPPORT RCSwitch * _rfModem; bool _learning = false; #endif @@ -74,21 +76,6 @@ bool _rfb_ticker_active = false; // PRIVATES // ----------------------------------------------------------------------------- -/* - From an hexa char array ("A220EE...") to a byte array (half the size) - */ -static int _rfbToArray(const char * in, byte * out, int length = RF_MESSAGE_SIZE * 2) { - int n = strlen(in); - if (n > RF_MAX_MESSAGE_SIZE*2 || (length > 0 && n != length)) return 0; - char tmp[3] = {0,0,0}; - n /= 2; - for (unsigned char p = 0; p RF_MAX_MESSAGE_SIZE*2 || (length > 0 && n != length)) return 0; + char tmp[3] = {0,0,0}; + n /= 2; + for (unsigned char p = 0; p 0 && millis() - learn_start > RF_LEARN_TIMEOUT) { - DEBUG_MSG_P(PSTR("[RFBRIDGE] learn timeout triggered\n")); + DEBUG_MSG_P(PSTR("[RF] Learn timeout triggered\n")); memset(_uartbuf, 0, sizeof(_uartbuf)); _uartbuf[0] = RF_CODE_LEARN_KO; _rfbDecode(); @@ -390,7 +397,7 @@ void _rfbReceive() { last = millis(); unsigned long rf_code = _rfModem->getReceivedValue(); if ( rf_code > 0) { - DEBUG_MSG_P(PSTR("[RFBRIDGE] Received code: %08X\n"), rf_code); + DEBUG_MSG_P(PSTR("[RF] Received code: %08X\n"), rf_code); unsigned int timing = _rfModem->getReceivedDelay(); memset(_uartbuf, 0, sizeof(_uartbuf)); unsigned char *msgbuf = _uartbuf + 1; @@ -417,7 +424,7 @@ void _rfbReceive() { yield(); byte c = Serial.read(); - //DEBUG_MSG_P(PSTR("[RFBRIDGE] Received 0x%02X\n"), c); + //DEBUG_MSG_P(PSTR("[RF] Received 0x%02X\n"), c); if (receiving) { if (c == RF_CODE_STOP && (_uartpos == 1 || _uartpos == RF_MESSAGE_SIZE + 1)) { @@ -453,7 +460,9 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa char buffer[strlen(MQTT_TOPIC_RFLEARN) + 3]; snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_RFLEARN); mqttSubscribe(buffer); + #if not RF_SUPPORT mqttSubscribe(MQTT_TOPIC_RFOUT); + #endif #if RF_RAW_SUPPORT mqttSubscribe(MQTT_TOPIC_RFRAW); #endif @@ -469,7 +478,7 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa _learnId = t.substring(strlen(MQTT_TOPIC_RFLEARN)+1).toInt(); if (_learnId >= relayCount()) { - DEBUG_MSG_P(PSTR("[RFBRIDGE] Wrong learnID (%d)\n"), _learnId); + DEBUG_MSG_P(PSTR("[RF] Wrong learnID (%d)\n"), _learnId); return; } _learnStatus = (char)payload[0] != '0'; @@ -478,13 +487,17 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa } - bool isRFOut = t.equals(MQTT_TOPIC_RFOUT); + #if not RF_SUPPORT + bool isRFOut = t.equals(MQTT_TOPIC_RFOUT); + #endif + #if RF_RAW_SUPPORT bool isRFRaw = !isRFOut && t.equals(MQTT_TOPIC_RFRAW); - #else + #elif not RF_SUPPORT bool isRFRaw = false; #endif + #if not RF_SUPPORT if (isRFOut || isRFRaw) { // The payload may be a code in HEX format ([0-9A-Z]{18}) or @@ -528,6 +541,8 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa } + #endif // not RF_SUPPORT + } } @@ -588,7 +603,7 @@ void _rfbInitCommands() { // ----------------------------------------------------------------------------- void rfbStore(unsigned char id, bool status, const char * code) { - DEBUG_MSG_P(PSTR("[RFBRIDGE] Storing %d-%s => '%s'\n"), id, status ? "ON" : "OFF", code); + DEBUG_MSG_P(PSTR("[RF] Storing %d-%s => '%s'\n"), id, status ? "ON" : "OFF", code); char key[RF_MAX_KEY_LENGTH] = {0}; snprintf_P(key, sizeof(key), PSTR("rfb%s%d"), status ? "ON" : "OFF", id); setSetting(key, code); @@ -600,6 +615,8 @@ String rfbRetrieve(unsigned char id, bool status) { return getSetting(key); } +#if not RF_SUPPORT + void rfbStatus(unsigned char id, bool status) { String value = rfbRetrieve(id, status); @@ -643,6 +660,7 @@ void rfbStatus(unsigned char id, bool status) { _rfbin = false; } +#endif // not RF_SUPPORT void rfbLearn(unsigned char id, bool status) { _learnId = id; @@ -684,13 +702,18 @@ void rfbSetup() { _rfbInitCommands(); #endif - #if RFB_DIRECT + #if RFB_DIRECT || RF_SUPPORT _rfModem = new RCSwitch(); + #if RF_SUPPORT + _rfModem->enableReceive(RF_PIN); + DEBUG_MSG_P(PSTR("[RF] RF receiver on GPIO %u\n"), RF_PIN); + #else _rfModem->enableReceive(RFB_RX_PIN); _rfModem->enableTransmit(RFB_TX_PIN); _rfModem->setRepeatTransmit(6); - DEBUG_MSG_P(PSTR("[RFBRIDGE] RF receiver on GPIO %u\n"), RFB_RX_PIN); - DEBUG_MSG_P(PSTR("[RFBRIDGE] RF transmitter on GPIO %u\n"), RFB_TX_PIN); + DEBUG_MSG_P(PSTR("[RF] RF receiver on GPIO %u\n"), RFB_RX_PIN); + DEBUG_MSG_P(PSTR("[RF] RF transmitter on GPIO %u\n"), RFB_TX_PIN); + #endif #endif // Register loop