This library provides a client for doing simple publish/subscribe messaging with MQTT PlugPlay.
This library is derived almost entirely from the great work done here: https://github.com/knolleary/pubsubclient
The library comes with a number of example sketches. See File > Examples > PlugPlayMQTT within the Arduino application.
- It can only publish QoS 0 messages. It can subscribe at QoS 0 or QoS 1.
- The maximum message size, including header, is 128 bytes by default. This
is configurable via
MQTT_MAX_PACKET_SIZE
inPubSubClient.h
. - The keepalive interval is set to 15 seconds by default. This is configurable
via
MQTT_KEEPALIVE
inPubSubClient.h
. - The client uses MQTT 3.1.1 by default. It can be changed to use MQTT 3.1 by
changing value of
MQTT_VERSION
inPubSubClient.h
.
The library uses the Arduino Ethernet Client api for interacting with the underlying network hardware. This means it Just Works with a growing number of boards and shields, including:
- Arduino Ethernet
- Arduino Ethernet Shield
- Arduino YUN – use the included
YunClient
in place ofEthernetClient
, and be sure to do aBridge.begin()
first - Arduino WiFi Shield - if you want to send packets > 90 bytes with this shield,
enable the
MQTT_MAX_TRANSFER_SIZE
define inPubSubClient.h
. - Sparkfun WiFly Shield – library
- TI CC3000 WiFi - library
- Intel Galileo/Edison
- ESP8266
The library cannot currently be used with hardware based on the ENC28J60 chip – such as the Nanode or the Nuelectronics Ethernet Shield. For those, there is an alternative library available.
Clone (or download and unzip) the repository to ~/Documents/Arduino/libraries
where ~/Documents/Arduino
is your sketchbook directory.
> cd ~/Documents/Arduino
> mkdir libraries
> cd libraries
> git clone https://github.com/plugplayco/plugplay-arduino-mqtt PlugPlayMQTT
Set authentication to connect to PlugPlay
Arguments:
- userKey - user Key of your user account.
- boardId - board Id of board you want to publish/subcribe to.
Connect to PlugPlay with a MQTT client Id
Arguments:
- id - client Id (should be a random Id)
// Create a random client ID
String clientId = "ArduinoClient-";
clientId += String(random(0xffff), HEX);
client.connectPlugPlay(clientId.c_str());
Create a message to publish
Arguments:
- dvName - device name
- data0 - first data
- data1 - second data
- data2 - third data
Subcribe a topic
Publish a message to a topic. The message is created by createMsg(String dvName, float data0, float data1, float data2)
Get device name from a received message
Get data from a received message
// Get the first data
client.getData((char*)payload,"data0");
// Get the second data
client.getData((char*)payload,"data1");
// Get the third data
client.getData((char*)payload,"data2");
You can refer here: http://pubsubclient.knolleary.net
This code is released under the MIT License.