Apply VSC auto format to the code and indent macros for readability
This commit is contained in:
1 parent
f7ec81ffaa
commit
7f9d8d18da
26 files changed
+4512
-3692
No files matched your search
+124
-98
@@ -32,7 +32,7 @@
|
||||
|
||||
#ifdef ZgatewayRFM69
|
||||
|
||||
#include <RFM69.h> //https://www.github.com/lowpowerlab/rfm69
|
||||
#include <RFM69.h> //https://www.github.com/lowpowerlab/rfm69
|
||||
#include <SPI.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
@@ -40,46 +40,51 @@ char RadioConfig[128];
|
||||
|
||||
// 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;
|
||||
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":"")
|
||||
#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() {
|
||||
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++) {
|
||||
for (size_t i = 0; i < (sizeof(*pGC) - 4); i++)
|
||||
{
|
||||
checksum += *p++;
|
||||
}
|
||||
return checksum;
|
||||
}
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
void eeprom_setup() {
|
||||
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) {
|
||||
if (gc_checksum() != pGC->checksum)
|
||||
{
|
||||
trc(F("Factory reset"));
|
||||
memset(pGC, 0, sizeof(*pGC));
|
||||
strcpy_P(pGC->encryptkey, ENCRYPTKEY);
|
||||
strcpy_P(pGC->rfmapname, RFM69AP_NAME);
|
||||
pGC->networkid = NETWORKID;
|
||||
pGC->nodeid = NODEID;
|
||||
pGC->powerlevel = ((IS_RFM69HCW)?0x80:0x00) | POWER_LEVEL;
|
||||
pGC->powerlevel = ((IS_RFM69HCW) ? 0x80 : 0x00) | POWER_LEVEL;
|
||||
pGC->rfmfrequency = FREQUENCY;
|
||||
pGC->checksum = gc_checksum();
|
||||
EEPROM.commit();
|
||||
@@ -89,13 +94,14 @@ void eeprom_setup() {
|
||||
|
||||
RFM69 radio;
|
||||
|
||||
void setupRFM69(void) {
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
eeprom_setup();
|
||||
#endif
|
||||
void setupRFM69(void)
|
||||
{
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
eeprom_setup();
|
||||
#endif
|
||||
int freq;
|
||||
static const char PROGMEM JSONtemplate[] =
|
||||
R"({"msgType":"config","freq":%d,"rfm69hcw":%d,"netid":%d,"power":%d})";
|
||||
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);
|
||||
@@ -104,57 +110,62 @@ void setupRFM69(void) {
|
||||
if (!radio.initialize(pGC->rfmfrequency, pGC->nodeid, pGC->networkid))
|
||||
{
|
||||
trc(F("ZgatewayRFM69 initialization failed"));
|
||||
}
|
||||
}
|
||||
|
||||
if (GC_IS_RFM69HCW) {
|
||||
radio.setHighPower(); // Only for RFM69HCW & HW!
|
||||
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);
|
||||
if (pGC->encryptkey[0] != '\0')
|
||||
radio.encrypt(pGC->encryptkey);
|
||||
|
||||
trc(F("ZgatewayRFM69 Listening and transmitting at"));
|
||||
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;
|
||||
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;
|
||||
}
|
||||
trc(freq);
|
||||
|
||||
size_t len = snprintf_P(RadioConfig, sizeof(RadioConfig), JSONtemplate,
|
||||
freq, GC_IS_RFM69HCW, pGC->networkid, GC_POWER_LEVEL);
|
||||
if (len >= sizeof(RadioConfig)) {
|
||||
freq, GC_IS_RFM69HCW, pGC->networkid, GC_POWER_LEVEL);
|
||||
if (len >= sizeof(RadioConfig))
|
||||
{
|
||||
trc(F("\n\n*** RFM69 config truncated ***\n"));
|
||||
}
|
||||
}
|
||||
|
||||
bool RFM69toMQTT(void) {
|
||||
bool RFM69toMQTT(void)
|
||||
{
|
||||
//check if something was received (could be an interrupt from the radio)
|
||||
if (radio.receiveDone())
|
||||
{
|
||||
trc(F("Creating RFM69 buffer"));
|
||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject& RFM69data = jsonBuffer.createObject();
|
||||
uint8_t data[RF69_MAX_DATA_LEN+1]; // For the null character
|
||||
JsonObject &RFM69data = jsonBuffer.createObject();
|
||||
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
|
||||
data[DATALEN] = '\0'; // Terminate the string
|
||||
|
||||
// Ack as soon as possible
|
||||
//check if sender wanted an ACK
|
||||
@@ -167,73 +178,88 @@ bool RFM69toMQTT(void) {
|
||||
trc(F("Data received"));
|
||||
trc((const char *)data);
|
||||
|
||||
char buff[sizeof(subjectRFM69toMQTT)+4];
|
||||
char buff[sizeof(subjectRFM69toMQTT) + 4];
|
||||
sprintf(buff, "%s/%d", subjectRFM69toMQTT, SENDERID);
|
||||
RFM69data.set("data", (char *)data);
|
||||
RFM69data.set("rssi", (int)radio.RSSI);
|
||||
RFM69data.set("rssi", (int)radio.RSSI);
|
||||
RFM69data.set("senderid", (int)radio.SENDERID);
|
||||
pub(buff,RFM69data);
|
||||
pub(buff, RFM69data);
|
||||
|
||||
return true;
|
||||
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef simpleReceiving
|
||||
void MQTTtoRFM69(char * topicOri, char * datacallback) {
|
||||
void MQTTtoRFM69(char *topicOri, char *datacallback)
|
||||
{
|
||||
|
||||
if(cmpToMainTopic(topicOri,subjectMQTTtoRFM69)){
|
||||
trc(F("MQTTtoRFM69 data analysis"));
|
||||
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();
|
||||
trc(F("RFM69 receiver ID:"));
|
||||
trc(valueRCV);
|
||||
}
|
||||
if(radio.sendWithRetry(valueRCV, data, strlen(data),10)) {
|
||||
trc(F(" OK "));
|
||||
// Acknowledgement to the GTWRF topic
|
||||
char buff[sizeof(subjectGTWRFM69toMQTT)+4];
|
||||
sprintf(buff, "%s/%d", subjectGTWRFM69toMQTT, radio.SENDERID);
|
||||
pub(buff, data);
|
||||
} else {
|
||||
trc(F("RFM69 sending failed"));
|
||||
}
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoRFM69))
|
||||
{
|
||||
trc(F("MQTTtoRFM69 data analysis"));
|
||||
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();
|
||||
trc(F("RFM69 receiver ID:"));
|
||||
trc(valueRCV);
|
||||
}
|
||||
if (radio.sendWithRetry(valueRCV, data, strlen(data), 10))
|
||||
{
|
||||
trc(F(" OK "));
|
||||
// Acknowledgement to the GTWRF topic
|
||||
char buff[sizeof(subjectGTWRFM69toMQTT) + 4];
|
||||
sprintf(buff, "%s/%d", subjectGTWRFM69toMQTT, radio.SENDERID);
|
||||
pub(buff, data);
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("RFM69 sending failed"));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef jsonReceiving
|
||||
void MQTTtoRFM69(char * topicOri, JsonObject& RFM69data) {
|
||||
|
||||
if (cmpToMainTopic(topicOri,subjectMQTTtoRFM69)){
|
||||
const char * data = RFM69data["data"];
|
||||
trc(F("MQTTtoRFM69 json data analysis"));
|
||||
if(data){
|
||||
trc(F("MQTTtoRFM69 data ok"));
|
||||
int valueRCV = RFM69data["receiverid"]| defaultRFM69ReceiverId; //default receiver id value
|
||||
trc(F("RFM69 receiver ID:"));
|
||||
trc(valueRCV);
|
||||
if(radio.sendWithRetry(valueRCV, data, strlen(data),10)) {
|
||||
trc(F(" OK "));
|
||||
// Acknowledgement to the GTWRF topic
|
||||
pub(subjectGTWRFM69toMQTT, RFM69data);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
} else {
|
||||
trc(F("MQTTtoRFM69 sending failed"));
|
||||
}
|
||||
}else{
|
||||
trc(F("MQTTtoRFM69 failed json read"));
|
||||
void MQTTtoRFM69(char *topicOri, JsonObject &RFM69data)
|
||||
{
|
||||
|
||||
if (cmpToMainTopic(topicOri, subjectMQTTtoRFM69))
|
||||
{
|
||||
const char *data = RFM69data["data"];
|
||||
trc(F("MQTTtoRFM69 json data analysis"));
|
||||
if (data)
|
||||
{
|
||||
trc(F("MQTTtoRFM69 data ok"));
|
||||
int valueRCV = RFM69data["receiverid"] | defaultRFM69ReceiverId; //default receiver id value
|
||||
trc(F("RFM69 receiver ID:"));
|
||||
trc(valueRCV);
|
||||
if (radio.sendWithRetry(valueRCV, data, strlen(data), 10))
|
||||
{
|
||||
trc(F(" OK "));
|
||||
// Acknowledgement to the GTWRF topic
|
||||
pub(subjectGTWRFM69toMQTT, RFM69data); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTtoRFM69 sending failed"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
trc(F("MQTTtoRFM69 failed json read"));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
Reference in new issue
Block a user