MQTT (community library)

Summary

Name Value
Name MQTT
Version 0.4.32
Installs
License MIT
Author hirotakaster
URL https://github.com/hirotakaster/MQTT/
Repository https://github.com/hirotakaster/MQTT.git
Download .tar.gz
All Versions 0.4.32, 0.4.31, 0.4.30, 0.4.29, 0.4.28, 0.4.27, 0.4.26, 0.4.25, 0.4.24, 0.4.23, 0.4.22, 0.4.21, 0.4.20, 0.4.19, 0.4.18, 0.4.17, 0.4.16, 0.4.14, 0.4.8, 0.4.6, 0.4.4, 0.4.3, 0.4.2, 0.4.1, 0.4.0, 0.3.9, 0.3.8, 0.3.7, 0.3.6, 0.3.5, 0.3.4, 0.3.3, 0.3.2, 0.3.1, 0.3.0, 0.2.9, 0.2.8, 0.2.7, 0.2.6, 0.2.5, 0.2.4, 0.2.3, 0.2.2, 0.2.1, 0.2.0, 0.1.0

Example Build Testing

Device OS Version:

This table is generated from an automated build. Success only indicates that the code compiled successfully.

Library Read Me

This content is provided by the library maintainer and has not been validated or approved.

MQTT for Photon, Spark Core

MQTT publish/subscribe library for Photon, Argon, Tracker One...etc version 0.4.32.

Source Code

This lightweight library source code is only 2 files. firmware -> MQTT.cpp, MQTT.h.

The application can use QoS 0, 1, 2 and the retain flag when publishing a message.

Example

Some sample sketches for Spark Core and Photon included (firmware/examples/).

  • mqtttest.ino : simple pub/sub sample.
  • mqttqostest.ino : QoS1, QoS2 publish and callback sample.
  • mqttSwitchBroker.ino : Example of how to switch to different brokers.
  • threadtest.ino : Example of SYSTEM_THREAD(ENABLED).

developer examples

some applications use MQTT with Photon. here are developer's reference examples.

sample source

#include "application.h"
#include "MQTT.h"

void callback(char* topic, byte* payload, unsigned int length);
MQTT client("iot.eclipse.org", 1883, callback);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;

if (!strcmp(p, "RED"))
RGB.color(255, 0, 0);
else if (!strcmp(p, "GREEN"))
RGB.color(0, 255, 0);
else if (!strcmp(p, "BLUE"))
RGB.color(0, 0, 255);
else
RGB.color(255, 255, 255);
delay(1000);
}


void setup() {
RGB.control(true);

// connect to the server(unique id by Time.now())
client.connect("sparkclient_" + String(Time.now()));

// publish/subscribe
if (client.isConnected()) {
client.publish("outTopic/message","hello world");
client.subscribe("inTopic/message");
}
}

void loop() {
if (client.isConnected())
client.loop();
}

FAQ

Can't connect/publish/subscribe to the MQTT server?
  • Test your MQTT server and port (default 1883) with the mosquitto_pub/sub command.
  • Check your network environments. Make sure your MQTT server can reach the Internet through your firewall.
  • Verify your subscribe/publish topic name is correct.
  • Perhaps device firmware network stack is failed. check your firmware version and bugs.
  • If you are using MQTT-TLS, make sure your RooT CA pem file, client key, certifications is valid.
  • Several MQTT servers will disconnect the first connection when you use the same user_id. When the application calls the connect method, use a different user_id on every device as connect method's second argument. Using the MAC address as a user_id is suggested.
    // device.1
    client.connect("spark-client", "user_1", "password1");
    // other devices...
    client.connect("spark-client", "user_others", "password1");
    
I want to change MQTT keepalive timeout.

MQTT keepalive timeout is defined "MQTT_DEFAULT_KEEPALIVE 15"(15 sec) in header file. You can change the keepalive timeout in constructor.

MQTT client("server_name", 1883, callback); // default: send keepalive packet to MQTT server every 15sec.
MQTT client("server_name", 1883, 256, 30, callback); // keepalive timeout is 30 seconds, default message size also added to be able to access correct constructor
Want to use over the 255 byte message size.

In this library, the maximum MQTT message size is defined as "MQTT_MAX_PACKET_SIZE 255" in the header file. If you want to use over 255 bytes, use the constructor's last argument.

MQTT client("server_name", 1883, callback);      // default 255 bytes
MQTT client("server_name", 1883, 512, callback); // max 512 bytes
Can I use on old firmware?

No, use the default latest firmware. I tested this library on the default latest firmware or the latest pre-release version. If you use an old firmware it may not work well and is not supported.

Bug or Problem?

First, check the Particle community site. But if your problem is not resolved, please create an issue with the problem details.

Pull Request

If you have a bug fix or feature, please send a pull request. Thanks for all developers' pull requests!

Browse Library Files