Files
OpenMQTTGateway/main/gatewayRFM69.cpp
Ryan Powell 585df9a420 [SYS] Update arduino core to 3.1.1 + refactor ino to cpp (#2177)
* Update arduino core to 3.1.1

* Fix Blufi build

* Update arduinojson, fix build errors with idf

* Fix narrowing

* fix RF builds

* WIP-Convert ino files to cpp

* Fix pilight build

* Fix Somfy actuator build.

* Update esp32dev-rf partition

* Fix Weatherstation build

* Fix GFSunInverter build

* Fix esp32dev-ir build

* Fix ble-aws build

* Fix eth builds

* Fix m5Stack missing pins_arduino.h

* Fix build errors for M5 stack/tough, others are upstream issues.

* Fix RTL 433 build - remaining errors are from radolib

* Fix nodemcu build

* fix 2g builds

* Fix serial build

* Fix actuator on off builds

* Fix SSD1306 build - remaining errors are from radiolib

* Fix multiple definition of OTAserver_cert

* Fix nodemcu rf2 build

* Fix ADC builds

* Fix sensor builds

* Fix LORA builds

* Fix multi-receiver builds - remaining errors are in radiolib

* Fix fastled builds

* Fix theegns board builds

* Fix broker builds

* Update fastled - old version failed all-test build

* Fix RN8209 builds

* Fix max temp actuator builds

* Fix PWM builds

* Fix C37 sensor builds

* Fix HTU21 builds

* Fix INA266 builds

* Fix undefined variables in mqtt discovery

* Fix webui build

* Fix fastled invalid pin error

* Fix wifi manual setup builds

* Fix onewire/all-test build - bin too big error remaining

* Fix theengs plug build

* Fix rfbridge builds

* Fix blufi builds

* Fix undefined functions in serial

* Fix preprocessor definition checks

* Set IDF log level to erre

* Add delay in loop to prevent watchdog timeout

* Use xTaskCreateUniveral to support single core processors

* Remove old HTTPUpdate files - upsteam fixed.

* Cleanup and move common declarations to header file

* Use custom partiton table to fix builds where bin is too large

* Update M5StickC - fixs esp32-m5stick-c-ble build

* Revert to Arduino core 2.x for builds with incompatible libs

* Remove "Z" from file names and rename omg_common to TheengsCommon

* Fix gateway name when using MAC with new Arduino core

* Update IDF config to reduce loop task stack use

* Update esp-nimble-cpp version, corrects BLE uppercase ID's

* Update wifi manager to fix watchdog timeout error
2025-05-06 19:35:50 -05:00

233 lines
7.4 KiB
C++

/*
Theengs OpenMQTTGateway - We Unite Sensors in One Open-Source Interface
Act as a wifi or ethernet gateway between your RF/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 signal corresponding to the received MQTT data with an RFM69 module
- publish MQTT data to a different topic related to received signal from an RFM69 module
Copyright: (c)Felix Rusu LowPowerLab.com
Library and code by Felix Rusu - felix@lowpowerlab.com
Modification of the code nanohab from bbx10 https://github.com/bbx10/nanohab
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 <http://www.gnu.org/licenses/>.
*/
#include "User_config.h"
#ifdef ZgatewayRFM69
# include <EEPROM.h>
# include <RFM69.h> //https://www.github.com/lowpowerlab/rfm69
# include "TheengsCommon.h"
# include "config_RFM69.h"
static char RadioConfig[128];
static const char* PROGMEM ENCRYPTKEY = RFM69_ENCRYPTKEY;
static const char* PROGMEM MDNS_NAME = RFM69_MDNS_NAME;
static const char* PROGMEM MQTT_BROKER = RFM69_MQTT_BROKER;
static const char* PROGMEM RFM69AP_NAME = RFM69_RFM69AP_NAME
// vvvvvvvvv Global Configuration vvvvvvvvvvv
struct _GLOBAL_CONFIG {
uint32_t checksum;
char rfmapname[32];
char encryptkey[16 + 1];
uint8_t networkid;
uint8_t nodeid;
uint8_t powerlevel; // bits 0..4 power leve, bit 7 RFM69HCW 1=true
uint8_t rfmfrequency;
};
# define GC_POWER_LEVEL (pGC->powerlevel & 0x1F)
# define GC_IS_RFM69HCW ((pGC->powerlevel & 0x80) != 0)
# define SELECTED_FREQ(f) ((pGC->rfmfrequency == f) ? "selected" : "")
struct _GLOBAL_CONFIG* pGC;
// vvvvvvvvv Global Configuration vvvvvvvvvvv
uint32_t gc_checksum() {
uint8_t* p = (uint8_t*)pGC;
uint32_t checksum = 0;
p += sizeof(pGC->checksum);
for (size_t i = 0; i < (sizeof(*pGC) - 4); i++) {
checksum += *p++;
}
return checksum;
}
void eeprom_setup() {
EEPROM.begin(4096);
pGC = (struct _GLOBAL_CONFIG*)EEPROM.getDataPtr();
// if checksum bad init GC else use GC values
if (gc_checksum() != pGC->checksum) {
Log.trace(F("Factory reset" CR));
memset(pGC, 0, sizeof(*pGC));
strcpy_P(pGC->encryptkey, ENCRYPTKEY);
strcpy_P(pGC->rfmapname, RFM69AP_NAME);
pGC->networkid = RFM69_NETWORKID;
pGC->nodeid = RFM69_NODEID;
pGC->powerlevel = ((IS_RFM69HCW) ? 0x80 : 0x00) | POWER_LEVEL;
pGC->rfmfrequency = FREQUENCY;
pGC->checksum = gc_checksum();
EEPROM.commit();
}
}
RFM69 radio;
void setupRFM69(void) {
eeprom_setup();
int freq;
static const char PROGMEM JSONtemplate[] =
R"({"msgType":"config","freq":%d,"rfm69hcw":%d,"netid":%d,"power":%d})";
char payload[128];
radio = RFM69(RFM69_CS, RFM69_IRQ, GC_IS_RFM69HCW, RFM69_IRQN);
// Initialize radio
if (!radio.initialize(pGC->rfmfrequency, pGC->nodeid, pGC->networkid)) {
Log.error(F("gatewayRFM69 initialization failed" CR));
}
if (GC_IS_RFM69HCW) {
radio.setHighPower(); // Only for RFM69HCW & HW!
}
radio.setPowerLevel(GC_POWER_LEVEL); // power output ranges from 0 (5dBm) to 31 (20dBm)
if (pGC->encryptkey[0] != '\0')
radio.encrypt(pGC->encryptkey);
switch (pGC->rfmfrequency) {
case RF69_433MHZ:
freq = 433;
break;
case RF69_868MHZ:
freq = 868;
break;
case RF69_915MHZ:
freq = 915;
break;
case RF69_315MHZ:
freq = 315;
break;
default:
freq = -1;
break;
}
Log.notice(F("gatewayRFM69 Listening and transmitting at: %d" CR), freq);
size_t len = snprintf_P(RadioConfig, sizeof(RadioConfig), JSONtemplate,
freq, GC_IS_RFM69HCW, pGC->networkid, GC_POWER_LEVEL);
if (len >= sizeof(RadioConfig)) {
Log.trace(F("\n\n*** RFM69 config truncated ***\n" CR));
}
}
bool RFM69toX(void) {
//check if something was received (could be an interrupt from the radio)
if (radio.receiveDone()) {
StaticJsonDocument<JSON_MSG_BUFFER> RFM69dataBuffer;
JsonObject RFM69data = RFM69dataBuffer.to<JsonObject>();
uint8_t data[RF69_MAX_DATA_LEN + 1]; // For the null character
uint8_t SENDERID = radio.SENDERID;
uint8_t DATALEN = radio.DATALEN;
uint16_t RSSI = radio.RSSI;
//save packet because it may be overwritten
memcpy(data, (void*)radio.DATA, DATALEN);
data[DATALEN] = '\0'; // Terminate the string
// Ack as soon as possible
//check if sender wanted an ACK
if (radio.ACKRequested()) {
radio.sendACK();
}
//updateClients(senderId, rssi, (const char *)data);
Log.trace(F("Data received: %s" CR), (const char*)data);
char buff[sizeof(subjectRFM69toMQTT) + 4];
sprintf(buff, "%s/%d", subjectRFM69toMQTT, SENDERID);
RFM69data["data"] = (char*)data;
RFM69data["rssi"] = (int)radio.RSSI;
RFM69data["senderid"] = (int)radio.SENDERID;
RFM69data["origin"] = buff;
enqueueJsonObject(RFM69data);
return true;
} else {
return false;
}
}
# if simpleReceiving
void XtoRFM69(const char* topicOri, const char* datacallback) {
if (cmpToMainTopic(topicOri, subjectMQTTtoRFM69)) {
Log.trace(F("MQTTtoRFM69 data analysis" CR));
char data[RF69_MAX_DATA_LEN + 1];
memcpy(data, (void*)datacallback, RF69_MAX_DATA_LEN);
data[RF69_MAX_DATA_LEN] = '\0';
//We look into the subject to see if a special RF protocol is defined
String topic = topicOri;
int valueRCV = defaultRFM69ReceiverId; //default receiver id value
int pos = topic.lastIndexOf(RFM69receiverKey);
if (pos != -1) {
pos = pos + +strlen(RFM69receiverKey);
valueRCV = (topic.substring(pos, pos + 3)).toInt();
Log.notice(F("RFM69 receiver ID: %d" CR), valueRCV);
}
if (radio.sendWithRetry(valueRCV, data, strlen(data), 10)) {
Log.notice(F(" OK " CR));
// Acknowledgement to the GTWRF topic
char buff[sizeof(subjectGTWRFM69toMQTT) + 4];
sprintf(buff, "%s/%d", subjectGTWRFM69toMQTT, radio.SENDERID);
pub(buff, data);
} else {
Log.error(F("RFM69 sending failed" CR));
}
}
}
# endif
# if jsonReceiving
void XtoRFM69(const char* topicOri, JsonObject& RFM69data) {
if (cmpToMainTopic(topicOri, subjectMQTTtoRFM69)) {
const char* data = RFM69data["data"];
Log.trace(F("MQTTtoRFM69 json data analysis" CR));
if (data) {
Log.trace(F("MQTTtoRFM69 data ok" CR));
int valueRCV = RFM69data["receiverid"] | defaultRFM69ReceiverId; //default receiver id value
Log.notice(F("RFM69 receiver ID: %d" CR), valueRCV);
if (radio.sendWithRetry(valueRCV, data, strlen(data), 10)) {
Log.notice(F(" OK " CR));
// Acknowledgement to the GTWRF topic
RFM69data["origin"] = subjectGTWRFM69toMQTT;
enqueueJsonObject(RFM69data);
} else {
Log.error(F("MQTTtoRFM69 sending failed" CR));
}
} else {
Log.error(F("MQTTtoRFM69 failed json read" CR));
}
}
}
# endif
#endif