/* OpenMQTTGateway - ESP8266 or Arduino program for home automation Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker Send and receiving command by MQTT This gateway enables to: - receive MQTT data from a topic and send RF 433Mhz signal corresponding to the received MQTT data - publish MQTT data to a different topic related to received 433Mhz signal - leverage the rtl_433 device decoders on a ESP32 device Copyright: (c)Florian ROBERT This file is part of OpenMQTTGateway. OpenMQTTGateway 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. OpenMQTTGateway 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 . */ #include "User_config.h" #ifdef ZgatewayRTL_433 # include char messageBuffer[JSON_MSG_BUFFER]; rtl_433_ESP rtl_433(-1); void rtl_433_Callback(char* message) { DynamicJsonDocument jsonBuffer2(JSON_MSG_BUFFER); JsonObject RFrtl_433_ESPdata = jsonBuffer2.to(); auto error = deserializeJson(jsonBuffer2, message); if (error) { Log.error(F("deserializeJson() failed: %s" CR), error.c_str()); return; } unsigned long MQTTvalue = (int)RFrtl_433_ESPdata["id"] + round((float)RFrtl_433_ESPdata["temperature_C"]); String topic = String(subjectRTL_433toMQTT); # if valueAsATopic String model = RFrtl_433_ESPdata["model"]; String id = RFrtl_433_ESPdata["id"]; if (model != 0) { topic = topic + "/" + model; if (id != 0) { topic = topic + "/" + id; } } # endif if (!isAduplicateSignal(MQTTvalue)) { pub((char*)topic.c_str(), RFrtl_433_ESPdata); storeSignalValue(MQTTvalue); } # ifdef MEMORY_DEBUG Log.trace(F("Post rtl_433_Callback: %d" CR), ESP.getFreeHeap()); # endif } void setupRTL_433() { rtl_433.setCallback(rtl_433_Callback, messageBuffer, JSON_MSG_BUFFER); Log.trace(F("ZgatewayRTL_433 command topic: %s%s%s" CR), mqtt_topic, gateway_name, subjectMQTTtoRTL_433); Log.notice(F("ZgatewayRTL_433 setup done " CR)); } void RTL_433Loop() { rtl_433.loop(); } extern void MQTTtoRTL_433(char* topicOri, JsonObject& RTLdata) { if (cmpToMainTopic(topicOri, subjectMQTTtoRTL_433)) { float tempMhz = RTLdata["mhz"]; bool success = false; if (RTLdata.containsKey("mhz") && validFrequency(tempMhz)) { receiveMhz = tempMhz; Log.notice(F("RTL_433 Receive mhz: %F" CR), receiveMhz); success = true; } if (RTLdata.containsKey("active")) { Log.trace(F("RTL_433 active:" CR)); activeReceiver = ACTIVE_RTL; // Enable RTL_433 Gateway success = true; } if (RTLdata.containsKey("rssi")) { int rssiThreshold = RTLdata["rssi"] | 0; Log.notice(F("RTL_433 RSSI Threshold Delta: %d " CR), rssiThreshold); rtl_433.setRSSIThreshold(rssiThreshold); success = true; } # if defined(RF_SX1276) || defined(RF_SX1278) if (RTLdata.containsKey("ookThreshold")) { int newOokThreshold = RTLdata["ookThreshold"] | 0; Log.notice(F("RTL_433 ookThreshold %d" CR), newOokThreshold); rtl_433.setOOKThreshold(newOokThreshold); success = true; } # endif if (RTLdata.containsKey("debug")) { int debug = RTLdata["debug"] | -1; Log.notice(F("RTL_433 set debug: %d" CR), debug); // rtl_433.setDebug(debug); rtl_433.initReceiver(RF_MODULE_RECEIVER_GPIO, receiveMhz); success = true; } if (RTLdata.containsKey("status")) { Log.notice(F("RTL_433 get status:" CR)); rtl_433.getStatus(1); success = true; } if (success) { pub(subjectRTL_433toMQTT, RTLdata); } else { pub(subjectRTL_433toMQTT, "{\"Status\": \"Error\"}"); // Fail feedback Log.error(F("MQTTtoRTL_433 Fail json" CR)); } enableActiveReceiver(false); } } extern void enableRTLreceive() { Log.notice(F("Switching to RTL_433 Receiver: %FMhz" CR), receiveMhz); # ifdef ZgatewayRF disableRFReceive(); # endif # ifdef ZgatewayRF2 disableRF2Receive(); # endif # ifdef ZgatewayPilight disablePilightReceive(); # endif rtl_433.initReceiver(RF_MODULE_RECEIVER_GPIO, receiveMhz); rtl_433.enableReceiver(RF_MODULE_RECEIVER_GPIO); } extern void disableRTLreceive() { Log.trace(F("disableRTLreceive" CR)); rtl_433.enableReceiver(-1); rtl_433.disableReceiver(); } extern int getRTLrssiThreshold() { return rtl_433.rssiThreshold; } extern int getRTLAverageRSSI() { return rtl_433.averageRssi; } extern int getRTLCurrentRSSI() { return rtl_433.currentRssi; } extern int getRTLMessageCount() { return rtl_433.messageCount; } # if defined(RF_SX1276) || defined(RF_SX1278) extern int getOOKThresh() { return rtl_433.OokFixedThreshold; } # endif #endif