Skip to content

Commit

Permalink
Merge pull request #49 from nRF24/mqtt_examples_renew
Browse files Browse the repository at this point in the history
- Add renewAddress to MQTT examples if connect fails
- Add new non-blocking example
  • Loading branch information
TMRh20 authored Jul 7, 2024
2 parents 4b9271a + b0ae98a commit f5980c0
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 17 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/build_arduino.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- examples/InteractiveServer_Mesh
- examples/MQTT/mqtt_basic
- examples/MQTT/mqtt_basic_2
- examples/MQTT/mqtt_basic_no_blk
- examples/SimpleClient_Mesh
# these need RF24_TAP defined
Expand All @@ -42,7 +43,6 @@ jobs:
- source-url: https://github.com/nRF24/RF24.git
- source-url: https://github.com/nRF24/RF24Network.git
- source-url: https://github.com/nRF24/RF24Mesh.git
- name: PubSubClient
- name: MQTT
- source-path: ./
fqbn: ${{ matrix.fqbn }}
Expand All @@ -58,7 +58,6 @@ jobs:
matrix:
fqbn:
- "arduino:avr:yun"
- "arduino:avr:uno"
- "arduino:avr:diecimila"
- "arduino:avr:mega"
- "arduino:avr:megaADK"
Expand Down Expand Up @@ -102,9 +101,9 @@ jobs:
# Generate size deltas data for this board
include:
- fqbn: arduino:avr:nano
enable-deltas-report: true
enable-deltas-report: ${{ github.event_name == 'pull_request' }}
- fqbn: arduino:samd:mkrzero
enable-deltas-report: true
enable-deltas-report: ${{ github.event_name == 'pull_request' }}

# When using a matrix to compile for multiple boards, it's necessary to use a separate job for the deltas report
report:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/build_platformIO.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
with:
example-path: ${{ matrix.example }}
board-id: ${{ matrix.board }}
lib-deps: -l knolleary/PubSubClient -l 256dpi/MQTT
lib-deps: -l 256dpi/MQTT
strategy:
fail-fast: false
matrix:
Expand All @@ -48,6 +48,7 @@ jobs:
- "examples/InteractiveServer_Mesh/*"
- "examples/MQTT/mqtt_basic/mqtt_basic.ino"
- "examples/MQTT/mqtt_basic_2/mqtt_basic_2.ino"
- "examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino"
- "examples/SimpleClient_Mesh/SimpleClient_Mesh.ino"

# these need RF24_TAP defined
Expand Down
5 changes: 5 additions & 0 deletions RF24Ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ typedef RF24EthernetClass RF52EthernetClass;
* This is the example taken from the PubSub library (https://github.com/knolleary/pubsubclient) & slightly modified to include RF24Ethernet/RF24Mesh.
*/

/**
* @example mqtt_basic_no_blk.ino
*
* This is similar to the mqtt_basic example, but uses a non-blocking connect function.
*/
/**
* @example mqtt_basic_2.ino
*
Expand Down
11 changes: 5 additions & 6 deletions examples/MQTT/mqtt_basic/mqtt_basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ MQTTClient client;

void connect() {
Serial.print("connecting...");
uint32_t clTimeout = millis();
uint32_t clTimeout = millis() + 5001;
while (!client.connect(clientID)) {
Serial.print(".");
if (millis() - clTimeout > 5001) {
if (millis() > clTimeout) {
Serial.println();
mesh.renewAddress();
return;
}
uint32_t timer = millis();
uint32_t timer = millis() + 1000;
//Instead of delay, keep the RF24 stack updating
while (millis() - timer < 1000) {
while (millis() < timer) {
Ethernet.update();
}
}
Expand All @@ -91,7 +92,6 @@ void setup() {

//Convert the last octet of the IP address to an identifier used
char str[4];
int test = ip[3];
itoa(ip[3], str, 10);
memcpy(&clientID[13], &str, strlen(str));
Serial.println(clientID);
Expand Down Expand Up @@ -125,7 +125,6 @@ void 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));
Expand Down
11 changes: 5 additions & 6 deletions examples/MQTT/mqtt_basic_2/mqtt_basic_2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ MQTTClient client;

void connect() {
Serial.print("connecting...");
uint32_t clTimeout = millis();
uint32_t clTimeout = millis() + 5001;
while (!client.connect(clientID)) {
Serial.print(".");
if (millis() - clTimeout > 5001) {
if (millis() > clTimeout) {
mesh.renewAddress();
Serial.println();
return;
}
uint32_t timer = millis();
uint32_t timer = millis() + 1000;
//Instead of delay, keep the RF24 stack updating
while (millis() - timer < 1000) {
while (millis() < timer) {
Ethernet.update();
}
}
Expand All @@ -92,7 +93,6 @@ void setup() {

//Convert the last octet of the IP address to an identifier used
char str[4];
int test = ip[3];
itoa(ip[3], str, 10);
memcpy(&clientID[13], &str, strlen(str));
Serial.println(clientID);
Expand Down Expand Up @@ -126,7 +126,6 @@ void 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));
Expand Down
124 changes: 124 additions & 0 deletions examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
*************************************************************************
RF24Ethernet Arduino library by TMRh20 - 2014-2021
Automated (mesh) wireless networking and TCP/IP communication stack for RF24 radio modules
RF24 -> RF24Network -> UIP(TCP/IP) -> RF24Ethernet
-> RF24Mesh
Documentation: http://nRF24.github.io/RF24Ethernet/
*************************************************************************
**** EXAMPLE REQUIRES: Arduino MQTT library: https://github.com/256dpi/arduino-mqtt/ ***
Shown in Arduino Library Manager as 'MQTT' by Joel Gaehwiler
*************************************************************************
Basic MQTT example
This sketch demonstrates the basic capabilities of the library.
It connects to an MQTT server then:
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic", printing out any messages
it receives.
- it assumes the received payloads are strings not binary
- Continually publishes its nodeID to the outTopic
It will reconnect to the server if the connection is lost using a non-blocking
reconnect function.
*/

#include <SPI.h>
#include <RF24.h>
#include <RF24Network.h>
#include <RF24Mesh.h>
#include <RF24Ethernet.h>
#include <MQTT.h>

RF24 radio(7, 8);
RF24Network network(radio);
RF24Mesh mesh(radio, network);
RF24EthernetClass RF24Ethernet(radio, network, mesh);

IPAddress ip(10, 10, 2, 4);
IPAddress gateway(10, 10, 2, 2); //Specify the gateway in case different from the server
IPAddress server(10, 10, 2, 2); //The ip of the MQTT server
char clientID[] = { "arduinoClient " };

void messageReceived(MQTTClient* client, char topic[], char payload[], int length) {
(void)*client;
(void)length;
Serial.println("incoming: ");
Serial.print(topic);
Serial.print(" - ");
Serial.println(payload);
}

EthernetClient ethClient;
MQTTClient client;

void connect() {
Serial.println("connecting...");
if (!client.connect(clientID)) {
mesh.renewAddress();
return;
}
Serial.println("\nconnected!");
client.publish("outTopic", "hello world");
client.subscribe("inTopic", 2);
}

void setup() {

Serial.begin(115200);

Ethernet.begin(ip, gateway);

if (mesh.begin()) {
Serial.println(" OK");
} else {
Serial.println(" Failed");
}

//Convert the last octet of the IP address to an identifier used
char str[4];
itoa(ip[3], str, 10);
memcpy(&clientID[13], &str, strlen(str));
Serial.println(clientID);

client.begin(server, ethClient);
client.onMessageAdvanced(messageReceived);
}


uint32_t pub_timer = 0;

void loop() {

Ethernet.update();

if (!client.connected()) {
if (!mesh.checkConnection()) {
if (mesh.renewAddress() == MESH_DEFAULT_ADDRESS) {
mesh.begin();
}
}
connect();
Serial.println();
}

client.loop();

// Every so often, report to the MQTT server the Node ID of this node
if (client.connected() && millis() - pub_timer > 3000) {
pub_timer = millis();
char str[4];
itoa(ip[3], str, 10);
char str1[] = "Node \r\n";
memcpy(&str1[5], &str, strlen(str));

client.publish("outTopic", str1);
}
}

0 comments on commit f5980c0

Please sign in to comment.