/* Theengs OpenMQTTGateway - We Unite Sensors in One Open-Source Interface Act as a gateway between your 433mhz, infrared IR, BLE, LoRa signal and one interface like an 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 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 ZgatewayRF_RHASK #include #ifdef RH_HAVE_HARDWARE_SPI #include // Not actually used but needed to compile #endif //int RollingCounter = 0; // Initialize rolling counter to avoid duplicate messages (messages a repeated multiple times int RC_old = 99, adressIn_old = 99; // initialize values to an unused value to ensure that the first value is not missed RH_ASK driver(500,4,0); // Initialize RH_ASK driver void setupRF(){ //RF init parameters Log.notice("Setup RF"); if (!driver.init()) { Log.notice("Success"); } else { Log.notice("RF failed"); } } # if defined(ZmqttDiscovery) && !defined(RF_DISABLE_TRANSMIT) && defined(RFmqttDiscovery) void RFtoMQTTdiscovery(int deviceID, String sensorName, String sensorUnit) { // Function creates autodiscovery message depending on the sensor ID Log.trace(F("RF Entity Discovered, create HA Discovery CFG" CR)); String discovery_topic = String(subjectRFtoMQTT); String theUniqueId = getUniqueId(String(deviceID), "-RH_ASK"); String devID = String(deviceID); // Device ID (adress of RF sender) announceDeviceTrigger( false, (char*)discovery_topic.c_str(), "received", "", (char*)theUniqueId.c_str(), (char*)sensorName.c_str(), "", "", "", (char*)sensorUnit.c_str(), (char*)devID.c_str()); } # endif void RFtoX() { uint8_t buf[RH_ASK_MAX_MESSAGE_LEN]; uint8_t buflen = sizeof(buf); if (driver.recv(buf, &buflen)) { Log.trace(F("Rcv. RF" CR)); # ifdef ESP32 Log.trace(F("RF Task running on core :%d" CR), xPortGetCoreID()); # endif // Decrypt RF message int adressIn, parameterIn, RCin; float payloadIn; MessageDecrypt(buf, &adressIn, ¶meterIn, &payloadIn, &RCin); // if (!isAduplicateSignal(MQTTvalue) && MQTTvalue != 0) { // conditions to avoid duplications of RF -->MQTT if (((RCin != RC_old) || (adressIn != adressIn_old)) && (unsigned long)buf != 0) { // new message (no duplicate) if rolling counter is changed RC_old = RCin; adressIn_old = adressIn; // Detect device in the device list int SensorNo; bool FoundSensor = false; String sensorName, sensorUnit; for(SensorNo = 0;SensorNo RFdataBuffer; JsonObject RFdata = RFdataBuffer.to(); //String theUniqueId = getUniqueId(String(adressIn), "-RH_ASK"); // get unique ID for this sensor String template_Name = String(sensorName) + "_" + String(adressIn); RFdata[template_Name] = (float)payloadIn; # if defined(ZmqttDiscovery) && !defined(RF_DISABLE_TRANSMIT) && defined(RFmqttDiscovery) //component creation for HA if (SYSConfig.discovery) RFtoMQTTdiscovery(adressIn, sensorName, sensorUnit); # endif RFdata["origin"] = subjectRFtoMQTT; enqueueJsonObject(RFdata); // Casting "receivedSignal[o].value" to (unsigned long) because ArduinoLog doesn't support uint64_t for ESP's // Log.trace(F("Store val: %u" CR), (unsigned long)MQTTvalue); // storeSignalValue(MQTTvalue); } else { Log.notice("duplicate, no publishing\n"); } } } bool MessageDecrypt(byte ByteArray[], int *adress, int *parameter, float *payload, int *RCin) { *adress = (int)ByteArray[0]; // To be replaced by memcopy // todo byte arrayFloat[4]; arrayFloat[0] = ByteArray[1]; arrayFloat[1] = ByteArray[2]; arrayFloat[2] = ByteArray[3]; arrayFloat[3] = ByteArray[4]; float testfloat; testfloat = *((float*) (arrayFloat)); *payload = testfloat; *RCin = (int)ByteArray[5]; return true; } #endif