MQTT is lightweight pub/sub messaging protocol for IoT/M2M. MQTT target is large scale pub/sub system, used on Facebook Messenger and others(not for the message queue like a RabbitMQ/AMQP…etc). MQTT for Arduino is already developed as pubsubclient, but Arduino like open source system “Spark.io” don’t have MQTT library. So I made MQTT for Spark.io based on pubsubclient. Thanks for knolleary.
#include "MQTT.h"
void callback(char* topic, byte* payload, unsigned int length);
MQTT client("server_name", 1883, callback);
// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
if (message.equals("RED"))
RGB.color(255, 0, 0);
else if (message.equals("GREEN"))
RGB.color(0, 255, 0);
else if (message.equals("BLUE"))
RGB.color(0, 0, 255);
else
RGB.color(255, 255, 255);
delay(1000);
}
void setup() {
RGB.control(true);
// connect to the MQTT server
client.connect("sparkclient");
// publish/subscribe
if (client.isConnected()) {
client.publish("outTopic","hello world");
client.subscribe("inTopic");
}
}
void loop() {
if (client.isConnected())
client.loop();
}