forked from knolleary/pubsubclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
696 additions
and
511 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
67 changes: 67 additions & 0 deletions
67
PubSubClient/examples/mqtt_reconnect_nonblocking/mqtt_reconnect_nonblocking.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,67 @@ | ||
/* | ||
Reconnecting MQTT example - non-blocking | ||
This sketch demonstrates how to keep the client connected | ||
using a non-blocking reconnect function. If the client loses | ||
its connection, it attempts to reconnect every 5 seconds | ||
without blocking the main loop. | ||
*/ | ||
|
||
#include <SPI.h> | ||
#include <Ethernet.h> | ||
#include <PubSubClient.h> | ||
|
||
// Update these with values suitable for your hardware/network. | ||
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; | ||
IPAddress ip(172, 16, 0, 100); | ||
IPAddress server(172, 16, 0, 2); | ||
|
||
void callback(char* topic, byte* payload, unsigned int length) { | ||
// handle message arrived | ||
} | ||
|
||
EthernetClient ethClient; | ||
PubSubClient client(ethClient); | ||
|
||
long lastReconnectAttempt = 0; | ||
|
||
boolean reconnect() { | ||
if (client.connect("arduinoClient")) { | ||
// Once connected, publish an announcement... | ||
client.publish("outTopic","hello world"); | ||
// ... and resubscribe | ||
client.subscribe("inTopic"); | ||
} | ||
return client.connected(); | ||
} | ||
|
||
void setup() | ||
{ | ||
client.setServer(server, 1883); | ||
client.setCallback(callback); | ||
|
||
Ethernet.begin(mac, ip); | ||
delay(1500); | ||
lastReconnectAttempt = 0; | ||
} | ||
|
||
|
||
void loop() | ||
{ | ||
if (!client.connected()) { | ||
long now = millis(); | ||
if (now - lastReconnectAttempt > 5000) { | ||
lastReconnectAttempt = now; | ||
// Attempt to reconnect | ||
if (reconnect()) { | ||
lastReconnectAttempt = 0; | ||
} | ||
} | ||
} else { | ||
// Client connected | ||
|
||
client.loop(); | ||
} | ||
|
||
} |
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
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,9 @@ | ||
name=PubSubClient | ||
version=2.0 | ||
author=Nick O'Leary <nick.oleary@gmail.com> | ||
maintainer=Nick O'Leary <nick.oleary@gmail.com> | ||
sentence=A client library for MQTT messaging. | ||
paragraph=MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages from a remote server. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison and ESP8266. | ||
category=Communication | ||
url=http://knolleary.net/arduino-client-for-mqtt/ | ||
architectures=* |
Oops, something went wrong.