#include <RF24Network.h>
#include <RF24Mesh.h>
#include <RF24Ethernet.h>
#include <MQTT.h>
RF52Network network(radio);
RF52Mesh mesh(radio, network);
RF52EthernetClass RF24Ethernet(radio, network, mesh);
IPAddress ip(10, 1, 3, 244);
IPAddress gateway(10, 1, 3, 33);
IPAddress server(10, 1, 3, 33);
char clientID[] = { "arduinoClient " };
void messageReceived(MQTTClient* client, char topic[], char payload[], int length) {
Serial.println("incoming: ");
Serial.print(topic);
Serial.print(" - ");
Serial.println(payload);
}
EthernetClient ethClient;
MQTTClient client;
bool connectFail = false;
void connect() {
Serial.print("connecting...");
uint32_t clTimeout = millis();
while (!client.connect(clientID)) {
Serial.print(".");
if (millis() - clTimeout > 5001) {
Serial.println();
connectFail = true;
return;
}
uint32_t timer = millis();
while (millis() - timer < 1000) {
Ethernet.update();
}
}
Serial.println("\nconnected!");
client.publish("outTopic", "hello world");
client.subscribe("inTopic", 2);
}
void setup() {
Serial.begin(115200);
delay(5000);
Ethernet.begin(ip, gateway);
if (mesh.begin()) {
Serial.println(" OK");
} else {
Serial.println(" Failed");
}
char str[4];
int test = ip[3];
itoa(ip[3], str, 10);
memcpy(&clientID[13], &str, strlen(str));
Serial.println(clientID);
client.begin(server, ethClient);
client.onMessageAdvanced(messageReceived);
}
uint32_t mesh_timer = 0;
uint32_t pub_timer = 0;
void loop() {
Ethernet.update();
if (millis() - mesh_timer > 10000 || connectFail) {
connectFail = false;
mesh_timer = millis();
if (!mesh.checkConnection()) {
if (mesh.renewAddress() == MESH_DEFAULT_ADDRESS) {
mesh.begin();
}
}
Serial.println();
}
if (!client.connected()) {
connect();
}
client.loop();
if (client.connected() && millis() - pub_timer > 3000) {
pub_timer = millis();
char str[4];
int test = ip[3];
itoa(ip[3], str, 10);
char str1[] = "Node \r\n";
memcpy(&str1[5], &str, strlen(str));
client.publish("outTopic", str1);
}
}
Driver class for nRF52840 2.4GHz Wireless Transceiver.