MQTT for Photon, Spark Core
MQTT publish/subscribe library for Photon, Spark Core version 0.4.29.
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.
developer examples
some applications use MQTT with Photon. here are developer's reference examples.
- Spark Core / Photon and CloudMQTT
- MQTT Publish-Subscribe Using Rpi, ESP and Photon
- Particle Photon on Watson IoT
- Connecting IoT devices to the Watson Conversation Car-Dashboard app
- ThingSpeak MQTT API
- How to Connect a Particle Photon to the Losant IoT Platform
- How I Hacked my Humidor with Losant and a Particle Photon
- ARTIK as MQTT Message Broker
- Particle and Ubidots using MQTT
- Using Twilio Sync with MQTT on a Particle Photon
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, 30, callback); // keepalive timeout is 30 sec.
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, MQTT_DEFAULT_KEEPALIVE, callback, 512); // 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!