-
Notifications
You must be signed in to change notification settings - Fork 83
Added sender with reconnect example #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
valerionew
wants to merge
3
commits into
arduino-libraries:master
Choose a base branch
from
fablab-bergamo:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
examples/WiFiSimpleSenderReconnect/WiFiSimpleSenderReconnect.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
ArduinoMqttClient - WiFi Reconnect | ||
|
||
This example connects to a MQTT broker and publishes a message to | ||
a topic once a second. If the client is disconnected from the broker, | ||
it automatically tries to reconnect. | ||
|
||
|
||
The circuit: | ||
- Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board | ||
|
||
|
||
This example code is in the public domain. | ||
*/ | ||
|
||
#include <ArduinoMqttClient.h> | ||
#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h> | ||
|
||
#include "arduino_secrets.h" | ||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h | ||
char ssid[] = SECRET_SSID; // your network SSID (name) | ||
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) | ||
|
||
// To connect with SSL/TLS: | ||
// 1) Change WiFiClient to WiFiSSLClient. | ||
// 2) Change port value from 1883 to 8883. | ||
// 3) Change broker value to a server with a known SSL/TLS root certificate | ||
// flashed in the WiFi module. | ||
|
||
WiFiClient wifiClient; | ||
MqttClient mqttClient(wifiClient); | ||
|
||
const char broker[] = "test.mosquitto.org"; | ||
int port = 1883; | ||
const char topic[] = "arduino/simple"; | ||
|
||
const long interval = 1000; | ||
unsigned long previousMillis = 0; | ||
|
||
int count = 0; | ||
|
||
int attemptReconnect(){ | ||
if (!mqttClient.connect(broker, port)) { | ||
Serial.print("MQTT connection failed! Error code = "); | ||
Serial.println(mqttClient.connectError()); | ||
|
||
} | ||
return mqttClient.connectError(); // return status | ||
} | ||
|
||
void setup() { | ||
//Initialize serial and wait for port to open: | ||
Serial.begin(9600); | ||
|
||
while (!Serial) { | ||
; // wait for serial port to connect. Needed for native USB port only | ||
} | ||
|
||
// attempt to connect to Wifi network: | ||
Serial.print("Attempting to connect to WPA SSID: "); | ||
Serial.println(ssid); | ||
|
||
while (WiFi.begin(ssid, pass) != WL_CONNECTED) { | ||
// failed, retry | ||
Serial.print("."); | ||
|
||
delay(5000); | ||
} | ||
|
||
|
||
Serial.println("You're connected to the network"); | ||
Serial.println(); | ||
|
||
|
||
// You can provide a unique client ID, if not set the library uses Arduino-millis() | ||
// Each client must have a unique client ID | ||
// mqttClient.setId("14"); | ||
|
||
// You can provide a username and password for authentication | ||
// mqttClient.setUsernamePassword(SECRET_MQTT_USER, SECRET_MQTT_PW); | ||
|
||
|
||
Serial.print("Attempting to connect to the MQTT broker: "); | ||
Serial.println(broker); | ||
|
||
|
||
if (!mqttClient.connect(broker, port)) { | ||
Serial.print("MQTT connection failed! Error code = "); | ||
Serial.println(mqttClient.connectError()); | ||
|
||
while (1); | ||
} | ||
|
||
|
||
Serial.println("You're connected to the MQTT broker!"); | ||
Serial.println(); | ||
|
||
} | ||
|
||
void loop() { | ||
// avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay | ||
// see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info | ||
unsigned long currentMillis = millis(); | ||
|
||
if (currentMillis - previousMillis >= interval) { | ||
previousMillis = currentMillis; | ||
|
||
mqttClient.poll(); // poll to avoid beeing disconnected | ||
|
||
|
||
Serial.print("Sending message to topic: "); | ||
Serial.println(topic); | ||
Serial.print("hello "); | ||
Serial.println(count); | ||
|
||
// send message, the Print interface can be used to set the message contents | ||
mqttClient.beginMessage(topic); | ||
mqttClient.print("hello "); | ||
mqttClient.print(count); | ||
mqttClient.endMessage(); | ||
|
||
Serial.println(); | ||
|
||
count++; | ||
|
||
if(!mqttClient.connected()){ // if the client has been disconnected, | ||
Serial.println("Client disconnected, attempting reconnection"); | ||
Serial.println(); | ||
|
||
if(!attemptReconnect()){ // try reconnecting | ||
Serial.print("Client reconnected!"); | ||
Serial.println(); | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#define SECRET_SSID "" | ||
#define SECRET_PASS "" | ||
#define SECRET_MQTT_USER "" | ||
#define SECRET_MQTT_PW "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"cpu":{"name":"Arduino MKR WiFi 1010","com_name":"COM23","fqbn":"arduino:samd:mkrwifi1010","flavour":"default"},"secrets":[{"name":"SECRET_SSID","value":"","isOptional":false},{"name":"SECRET_PASS","value":"","isOptional":false},{"name":"SECRET_MQTT_USER","value":"","isOptional":true},{"name":"SECRET_MQTT_PW","value":"","isOptional":true}]} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please explain why this needs to be moved? What issue's where you seeing?
The idea is
.poll()
should be called very frequently and sending should happen on the intervalThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #14
Probably calling poll so frequently somehow gets my client disconnected after 2-3h. I didn't dig deeper, as moving the poll seemd to have fixed the issue, or at least reduce drastically the number of disconnections
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@5N44P thanks for the reply. At this time, I'm not keen to move forward merging this change until we further understand exactly why it fixes things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any suggestions on how or what to test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to uncomment this line to start and enable debugging of the library: https://github.com/arduino-libraries/ArduinoMqttClient/blob/master/src/MqttClient.cpp#L22
We need to track down if there is something wrong at the WiFi or MQTT level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we just merge this PR? It already contains the reconnect example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @5N44P 👋 Unfortunately not really ... even in your example code only you've moved the
poll
inside the timed function. As long as we don't understand from where exactly the problem originates hiding it is not a solution.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aentinger how can we debug this?
I'm aware that reconnecting doesn't fix the problem, but if it makes the library usable for everyone it should be at least proposed as an example.
What I'm thinking, by the way, is that random and erratic disconnections can be expected, and if handled with a reconnection are not an issue. The polling without delay just gives an higher number of disconnections per unit of time, keeping the frequency per request unaltered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I would be doing is this: Solder a SWD connector to the bottom of your MKR board and connect your favourite debugger + graphical debugging front-end. Trying to get to the bottom of this issue via
Serial.print(...)
-debugging is probably going to be quite hard.I'd do it myself but unfortunately there are a lot of high priority items on my list which is why I won't get to it anytime soon 😞 If you could help out here it would be greatly appreciated. Talking about debugging, the Arduino Pro IDE comes with experimental debugging support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or of course uncomment the line suggested in #15 (comment) .