Skip to content

Commit

Permalink
Tidy up more api and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
knolleary committed Aug 28, 2015
1 parent 8e7e99c commit fa5b7f7
Show file tree
Hide file tree
Showing 8 changed files with 696 additions and 511 deletions.
495 changes: 0 additions & 495 deletions PubSubClient/PubSubClient.cpp

This file was deleted.

5 changes: 5 additions & 0 deletions PubSubClient/examples/mqtt_auth/mqtt_auth.ino
Expand Up @@ -26,6 +26,11 @@ PubSubClient client(server, 1883, callback, ethClient);
void setup()
{
Ethernet.begin(mac, ip);
// Note - the default maximum packet size is 128 bytes. If the
// combined length of clientId, username and password exceed this,
// you will need to increase the value of MQTT_MAX_PACKET_SIZE in
// PubSubClient.h

if (client.connect("arduinoClient", "testuser", "testpass")) {
client.publish("outTopic","hello world");
client.subscribe("inTopic");
Expand Down
56 changes: 48 additions & 8 deletions PubSubClient/examples/mqtt_basic/mqtt_basic.ino
@@ -1,9 +1,16 @@
/*
Basic MQTT example
- connects to an MQTT server
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"
- subscribes to the topic "inTopic", printing out any messages
it receives. NB - it assumes the received payloads are strings not binary
It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
achieve the same result without blocking the main loop.
*/

#include <SPI.h>
Expand All @@ -16,22 +23,55 @@ IPAddress ip(172, 16, 0, 100);
IPAddress server(172, 16, 0, 2);

void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
PubSubClient client(ethClient);

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic","hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup()
{
Serial.begin(57600);

client.setServer(server, 1883);
client.setCallback(callback);

Ethernet.begin(mac, ip);
if (client.connect("arduinoClient")) {
client.publish("outTopic","hello world");
client.subscribe("inTopic");
}
// Allow the hardware to sort itself out
delay(1500);
}

void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
}
@@ -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();
}

}
9 changes: 7 additions & 2 deletions PubSubClient/keywords.txt
@@ -1,5 +1,5 @@
#######################################
# Syntax Coloring Map For Ultrasound
# Syntax Coloring Map For PubSubClient
#######################################

#######################################
Expand All @@ -15,11 +15,16 @@ PubSubClient KEYWORD1
connect KEYWORD2
disconnect KEYWORD2
publish KEYWORD2
publish_P KEYWORD2
subscribe KEYWORD2
unsubscribe KEYWORD2
loop KEYWORD2
connected KEYWORD2
setServer KEYWORD2
setCallback KEYWORD2
setClient KEYWORD2
setStream KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################

9 changes: 9 additions & 0 deletions PubSubClient/library.properties
@@ -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=*

0 comments on commit fa5b7f7

Please sign in to comment.