Can someone tell me what is wrong with my code,
// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"
// This #include statement was automatically added by the Particle IDE.
#include "SparkJson/SparkJson.h"
#define ORG "xxx"
#define DEVICE_TYPE "xxx"
#define DEVICE_ID "xxx"
#define TOKEN "xxxx"
//-------- Customise the above values --------
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID ;
const char publishTopic[] = "iot-2/evt/status/fmt/json";
const char responseTopic[] = "iotdm-1/response";
const char manageTopic[] = "iotdevice-1/mgmt/manage";
const char updateTopic[] = "iotdm-1/device/update";
const char rebootTopic[] = "iotdm-1/mgmt/initiate/device/reboot";
int publishInterval = 5000; // 30 seconds
long lastPublishMillis;
MQTT client(server, 1883, callback);
void callback(char* topic, byte* payload, unsigned int payloadLength) {
Serial.print("callback invoked for topic: "); Serial.println(topic);
if (strcmp (responseTopic, topic) == 0) {
return; // just print of response for now
}
if (strcmp (rebootTopic, topic) == 0) {
Serial.println("Rebooting...");
}
if (strcmp (updateTopic, topic) == 0) {
handleUpdate(payload);
}
}
void handleUpdate(byte* payload) {
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject((char*)payload);
if (!root.success()) {
Serial.println("handleUpdate: payload parse FAILED");
return;
}
Serial.println("handleUpdate payload:"); root.prettyPrintTo(Serial); Serial.println();
JsonObject& d = root["d"];
JsonArray& fields = d["fields"];
for(JsonArray::iterator it=fields.begin(); it!=fields.end(); ++it) {
JsonObject& field = *it;
const char* fieldName = field["field"];
if (strcmp (fieldName, "metadata") == 0) {
JsonObject& fieldValue = field["value"];
if (fieldValue.containsKey("publishInterval")) {
publishInterval = fieldValue["publishInterval"];
Serial.print("publishInterval:"); Serial.println(publishInterval);
}
}
}
}
void setup() {
Serial.begin(115200); Serial.println();
mqttConnect();
initManagedDevice();
}
void mqttConnect() {
if (!client.isConnected()) {
Serial.print("Reconnecting MQTT client to "); Serial.println(server);
while (!client.connect(clientId,authMethod,token)) {
Serial.print(".");
delay(500);
}
Serial.println();
}
}
void initManagedDevice() {
if (client.subscribe("iotdm-1/response")) {
Serial.println("subscribe to responses OK");
} else {
Serial.println("subscribe to responses FAILED");
}
if (client.subscribe(rebootTopic)) {
Serial.println("subscribe to reboot OK");
} else {
Serial.println("subscribe to reboot FAILED");
}
if (client.subscribe("iotdm-1/device/update")) {
Serial.println("subscribe to update OK");
} else {
Serial.println("subscribe to update FAILED");
}
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonObject& d = root.createNestedObject("d");
JsonObject& metadata = d.createNestedObject("metadata");
metadata["publishInterval"] = publishInterval;
JsonObject& supports = d.createNestedObject("supports");
supports["deviceActions"] = true;
char buff[300];
root.printTo(buff, sizeof(buff));
Serial.println("publishing device metadata:"); Serial.println(buff);
if (client.publish(manageTopic, buff)) {
Serial.println("device Publish ok");
} else {
Serial.print("device Publish failed:");
}
}
void loop() {
if (millis() - lastPublishMillis > publishInterval) {
publishData();
lastPublishMillis = millis();
}
if (!client.loop()) {
mqttConnect();
}
}
void publishData() {
String payload = "test";
Serial.print("Sending payload: "); Serial.println(payload);
if (client.publish(publishTopic, (char*) payload.c_str()))
{
Serial.println("Publish OK");
} else
{
Serial.println("Publish FAILED");
}
}