From 76a15111457f58f0f78ab61326d2e694c526df43 Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Sat, 6 Jul 2024 03:01:55 -0600 Subject: [PATCH 1/7] Add renewAddress if connect fails - Update MQTT examples to renew their address if unable to connect to the MQTT server --- examples/MQTT/mqtt_basic/mqtt_basic.ino | 1 + examples/MQTT/mqtt_basic_2/mqtt_basic_2.ino | 1 + 2 files changed, 2 insertions(+) diff --git a/examples/MQTT/mqtt_basic/mqtt_basic.ino b/examples/MQTT/mqtt_basic/mqtt_basic.ino index d9a06e1..292d733 100644 --- a/examples/MQTT/mqtt_basic/mqtt_basic.ino +++ b/examples/MQTT/mqtt_basic/mqtt_basic.ino @@ -64,6 +64,7 @@ void connect() { Serial.print("."); if (millis() - clTimeout > 5001) { Serial.println(); + mesh.renewAddress(); return; } uint32_t timer = millis(); diff --git a/examples/MQTT/mqtt_basic_2/mqtt_basic_2.ino b/examples/MQTT/mqtt_basic_2/mqtt_basic_2.ino index b92bf38..14e78ba 100644 --- a/examples/MQTT/mqtt_basic_2/mqtt_basic_2.ino +++ b/examples/MQTT/mqtt_basic_2/mqtt_basic_2.ino @@ -64,6 +64,7 @@ void connect() { while (!client.connect(clientID)) { Serial.print("."); if (millis() - clTimeout > 5001) { + mesh.renewAddress(); Serial.println(); return; } From 9b78743d0f9eb907a806678d92a13aa267a2c375 Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Sat, 6 Jul 2024 13:41:26 -0600 Subject: [PATCH 2/7] Example adjustments, new example - Add non-blocking connect MQTT example - Remove unused variable `test` - Put timer calculations outside loops - Add new example file to docs --- RF24Ethernet.h | 6 + examples/MQTT/mqtt_basic/mqtt_basic.ino | 10 +- examples/MQTT/mqtt_basic_2/mqtt_basic_2.ino | 10 +- .../mqtt_basic_no_blk/mqtt_basic_no_blk.ino | 122 ++++++++++++++++++ 4 files changed, 136 insertions(+), 12 deletions(-) create mode 100644 examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino diff --git a/RF24Ethernet.h b/RF24Ethernet.h index 471fe7a..698332f 100644 --- a/RF24Ethernet.h +++ b/RF24Ethernet.h @@ -272,6 +272,12 @@ 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 * diff --git a/examples/MQTT/mqtt_basic/mqtt_basic.ino b/examples/MQTT/mqtt_basic/mqtt_basic.ino index 292d733..c926ed7 100644 --- a/examples/MQTT/mqtt_basic/mqtt_basic.ino +++ b/examples/MQTT/mqtt_basic/mqtt_basic.ino @@ -59,17 +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(); } } @@ -92,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); @@ -126,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)); diff --git a/examples/MQTT/mqtt_basic_2/mqtt_basic_2.ino b/examples/MQTT/mqtt_basic_2/mqtt_basic_2.ino index 14e78ba..c9062b0 100644 --- a/examples/MQTT/mqtt_basic_2/mqtt_basic_2.ino +++ b/examples/MQTT/mqtt_basic_2/mqtt_basic_2.ino @@ -60,17 +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(); } } @@ -93,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); @@ -127,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)); diff --git a/examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino b/examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino new file mode 100644 index 0000000..30c66ff --- /dev/null +++ b/examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino @@ -0,0 +1,122 @@ +/* + ************************************************************************* + 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 +#include +#include +#include +#include +#include + +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) { + 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); + } +} From 5b32020e4b7dd6647c13f1ed0ac9df87ff28857a Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Sat, 6 Jul 2024 13:42:57 -0600 Subject: [PATCH 3/7] Update RF24Ethernet.h Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- RF24Ethernet.h | 1 - 1 file changed, 1 deletion(-) diff --git a/RF24Ethernet.h b/RF24Ethernet.h index 698332f..cdde486 100644 --- a/RF24Ethernet.h +++ b/RF24Ethernet.h @@ -277,7 +277,6 @@ typedef RF24EthernetClass RF52EthernetClass; * * This is similar to the mqtt_basic example, but uses a non-blocking connect function. */ - /** * @example mqtt_basic_2.ino * From a57a390f9bd131086ac7637ea6509090ff40df8d Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Sun, 7 Jul 2024 05:16:05 -0700 Subject: [PATCH 4/7] [CI] add new example; rm PubSubClient dep --- .github/workflows/build_arduino.yml | 2 +- .github/workflows/build_platformIO.yml | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_arduino.yml b/.github/workflows/build_arduino.yml index 27ead50..f885bbb 100644 --- a/.github/workflows/build_arduino.yml +++ b/.github/workflows/build_arduino.yml @@ -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 @@ -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 }} diff --git a/.github/workflows/build_platformIO.yml b/.github/workflows/build_platformIO.yml index 656428e..4b17bf9 100644 --- a/.github/workflows/build_platformIO.yml +++ b/.github/workflows/build_platformIO.yml @@ -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: @@ -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 From fd4ab2396c63f8fb71414077063dd7d5acbb3611 Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Sun, 7 Jul 2024 05:32:18 -0700 Subject: [PATCH 5/7] [CI] rm duplicated job in arduino workflow --- .github/workflows/build_arduino.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build_arduino.yml b/.github/workflows/build_arduino.yml index f885bbb..4d80e18 100644 --- a/.github/workflows/build_arduino.yml +++ b/.github/workflows/build_arduino.yml @@ -58,7 +58,6 @@ jobs: matrix: fqbn: - "arduino:avr:yun" - - "arduino:avr:uno" - "arduino:avr:diecimila" - "arduino:avr:mega" - "arduino:avr:megaADK" @@ -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: From a341c8882569104e9f757c8c84f5e85d052e3106 Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Sun, 7 Jul 2024 05:54:41 -0700 Subject: [PATCH 6/7] ignore unused parameters in new example --- examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino b/examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino index 30c66ff..fd279b3 100644 --- a/examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino +++ b/examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino @@ -48,6 +48,8 @@ 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(" - "); From b0ae98a83481a241c8e880a93c43670744d0c936 Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Sun, 7 Jul 2024 06:03:09 -0700 Subject: [PATCH 7/7] cast the deref'd value of unused parameter --- examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino b/examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino index fd279b3..d2877fb 100644 --- a/examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino +++ b/examples/MQTT/mqtt_basic_no_blk/mqtt_basic_no_blk.ino @@ -48,7 +48,7 @@ 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)*client; (void)length; Serial.println("incoming: "); Serial.print(topic);