Hi
This is a programm to receive a message over the RFM69HW and push it to the mqtt server.
My code crash at some point. I do not figure out where and why. Can somebody help me?
It works 2,3,3 times and then nothing happened.
#include <RFM69.h>
#include <SPI.h>
#include <Ethernet2.h>
#include <PubSubClient.h>
//#define DEBUG // uncomment for debugging
#define VERSION "GW V2.0"
// Ethernet settings
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x22, 0xE6 }; // MAC address for ethernet
IPAddress ip(192, 168, 1, 60); // GW address
byte mqtt_server[] = { 192, 168, 1 , 14}; // MQTT broker address
// Wireless settings
#define NODEID 1 // unique node ID in the closed radio network; gateway is 1
#define RFM_SS 8 // Slave Select RFM69 is connected to pin 8
#define NETWORKID 100 // closed radio network ID
//Match frequency to the hardware version of the radio (uncomment one):
#define FREQUENCY RF69_433MHZ
//#define FREQUENCY RF69_868MHZ
//#define FREQUENCY RF69_915MHZ
#define ENCRYPTKEY "aSimpleTestKrypt" // shared 16-char encryption key is equal on Gateway and nodes
#define IS_RFM69HW // uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ACK_TIME 50 // max # of ms to wait for an ack
// PIN settings
#define SERIAL_BAUD 9600
typedef struct { // Radio packet structure max 66 bytes
int nodeID; // node identifier
int sensorID; // device identifier 0 is node; 31 is temperature, 32 is humidity
char cmd[1]; // read [r], write [w]
long intVal; // integer payload
float fltVal; // floating payload
char payLoad[32]; // char array payload
} Message;
Message mes;
int dest; // destination node for radio packet
bool mqttCon = false; // MQTT broker connection flag
bool respNeeded = false; // MQTT message flag in case of radio connection failure
bool mqttToSend = false; // message request issued by MQTT request
bool promiscuousMode = false; // only receive closed network nodes
char nodeTopic[32] = "home/rfm_gw/status"; // MQTT GW Topic transmitt
char subTopic[100] = "home/rfm_gw/rx/#"; // MQTT subscription topic ; receive
char clientName[32] = "RFM_gateway"; // MQTT system name of gateway
char pubTopic[100]; // MQTT publish topic string
char pubMsg[100]; // MQTT publish message string
// callback function header
void mqtt_callback(char* topic, byte* payload, unsigned int length);
// Init radio
RFM69 radio(RFM_SS);
// init mqtt client
EthernetClient ethClient;
PubSubClient mqttClient(ethClient );
void setup() {
// SERIAL CONNECTION FOR DEBUG MODE -------------------------------
#ifdef DEBUG
// start monitoring
Serial.begin(SERIAL_BAUD);
delay(1000);
Serial.print("Gateway Software Version ");
Serial.println(VERSION);
#endif
// RADIO MODULE -------------------------------
//change default Slave Select pin for RFM
radio.setCS(RFM_SS);
// initialise
radio.initialize(FREQUENCY, NODEID, NETWORKID);
#ifdef IS_RFM69HW
// only for RFM69HW!
//radio.setHighPower();
radio.setPowerLevel(2);
#endif
radio.encrypt(ENCRYPTKEY); // encrypt with shared key
radio.promiscuous(promiscuousMode); // listen only to nodes in closed network
// notify frequency
#ifdef DEBUG
Serial.print("\nListening at ");
Serial.print(FREQUENCY == RF69_433MHZ ? 433 : FREQUENCY == RF69_868MHZ ? 868 : 915);
Serial.println(" Mhz...");
#endif
// ETHERNET CONNECTION -------------------------------
// connect to router
Ethernet.begin(mac,ip);
delay(1500);
#ifdef DEBUG
// show ip
Serial.print("Gateway IP: ");
Serial.println(Ethernet.localIP());
#endif
// CONNECT TO MQTT BROKER -------------------------------
mqttClient.setServer(mqtt_server, 1883);
mqttClient.setCallback(mqtt_callback);
} // end setup
void loop() {
// check for received radio packets and construct MQTT message
if (radio.receiveDone()) {
processPacket();
}
// RECEIVE AND SEND MESSAGES -------------------------------
// send MQTT instruction packets over the radio network
if (mqttToSend) {
//sendMsg(dest);
}
/* MQTT loop*/
if (!mqttClient.loop()) { // check connection MQTT server and process MQTT subscription input
int mqttCon = 0;
while(mqttCon != 1){ // try to reconnect every 2 seconds
mqttCon = mqttClient.connect(clientName);
delay(2000);
}
if(mqttCon){ // Yes, we have a link so,
mqttClient.subscribe(subTopic); // re-subscribe to mqtt topic
mqttClient.publish(nodeTopic,"RFM Ethernet GW connected");
}
}
}
/* -------------------------------
PROCESSPACKET
receives data from the radio network, parses the contents and constructs MQTT topic and value
------------------------------- */
void processPacket() {
if (radio.DATALEN != sizeof(mes)){ // wrong message size means trouble
#ifdef DEBUG
Serial.println("invalid message structure..");
#endif
} else { // message size OK...
mes = *(Message*)radio.DATA; // copy radio packet
// and construct MQTT tx topic
sprintf(pubTopic, "home/rfm_gw/tx/node%02d/dev%02d", radio.SENDERID, mes.sensorID);
#ifdef DEBUG
Serial.print(radio.SENDERID); Serial.print(", ");
Serial.print(mes.sensorID); Serial.print(", ");
Serial.print(mes.cmd[0]); Serial.print(", ");
Serial.print(mes.intVal); Serial.print(", ");
Serial.print(mes.fltVal); Serial.print(", RSSI= ");
Serial.println(radio.RSSI); Serial.print("Node: ");
Serial.print(mes.nodeID); Serial.print(" Version: ");
for (int i = 0; i < sizeof(mes.payLoad); i++) Serial.print(mes.payLoad[i]);
Serial.println();
#endif
}
// sensorID ==1 -> alway battery power
if (mes.sensorID == 1) {
mes.fltVal /= 1000;
dtostrf(mes.fltVal, 2, 2, pubMsg);
}
#ifdef DEBUG
Serial.print("MQTT message: ");
Serial.print(pubTopic);
Serial.print(": ");
Serial.println(pubMsg);
#endif
mqttClient.publish(pubTopic, pubMsg); // publish MQTT message in northbound topic
if (radio.ACKRequested()) radio.sendACK(); // reply to any radio ACK requests
}
void mqtt_callback(char* topic, byte* payload, unsigned int length){
;
}