Hy,
#include "MQTT.h" void callback(char* topic, byte* payload, unsigned int length); /** * if want to use IP address, * byte server[] = { XXX,XXX,XXX,XXX }; * MQTT client(server, 1883, callback); * want to use domain name, * exp) iot.eclipse.org is Eclipse Open MQTT Broker: https://iot.eclipse.org/getting-started * MQTT client("iot.eclipse.org", 1883, callback); **/ byte server[] = { XXX.XXX.XXX.XXXX };//the IP of broker void callback(char* topic, byte* payload, unsigned int length); MQTT client(server, 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 client.connect("sparkclient"); // publish/subscribe if (client.isConnected()) { client.publish("outTopic/message","hello world"); client.subscribe("inTopic/message"); } } void loop() { if (client.isConnected()) client.loop(); }
i have used this Sample code test.
it works for 5 or 6 Publishing and stops responding afterwards and i have to hard reset.
Please help me out.