Electron, IBM Bluemix, MQTT

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) {

 if (strcmp (updateTopic, topic) == 0) {

void handleUpdate(byte* payload) {
 StaticJsonBuffer<300> jsonBuffer;
 JsonObject& root = jsonBuffer.parseObject((char*)payload);
 if (!root.success()) {
 Serial.println("handleUpdate: payload parse FAILED");
 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();

void mqttConnect() {
 if (!client.isConnected()) {
 Serial.print("Reconnecting MQTT client to "); Serial.println(server);
 while (!client.connect(clientId,authMethod,token)) {

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) {
 lastPublishMillis = millis();
 if (!client.loop()) {

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");

Can you tell me what’s wrong with my fridge?
If you don’t tell us what it’s supposed to do, and what it currently does or doesn’t do, then how could we possibly help you? (Not to mention the fact that no-one likes digging through someone’s else’s code without knowing what to look for…)

Hi Sorry for not being specific. I am unable to establish mqtt connection with my cloud server. Neither i could publish nor subscribe.

  1. mqtt connect fails !
  2. at cloud side i can see that the connection toggles, it connects and disconnects.
  3. the same piece of code works good with WiFi

If not for WiFi, then what is it you’re using?

My title says Electron (cellular device !)

Right, my bad :speak_no_evil: Could it be that reception is poor in the area you’re in, considering it might not be the code (sseing as it does work on WiFi)?

I don’t believe the reception is poor, I get perfect cyan breathing. Further all spark based publish subscribe examples work perfect.
Only difference is
I used “knolleary/pubsubclient” last time in my wifi device and now I am using mqtt library available in web IDE. But I understand that both libraries behave the same.

Can someone help me to walk through my code?

Or is someone who is successful in integrating electron with IBM bluemix?

Hi ysashikumar,
I am also trying to connect Particle Electron with Bluemix and facing the same issue. On cloud it toggles between connected and disconnected. Could you solve this problem?

Hi, I upgraded the firmware and then everything worked fine! Try upgrading your device firmware.

Thanks a lot buddy. That solved my problem. I owe u a party :smile:


@ysashikumar and @iamsalman
We are also using Bluemix cloud with Electron. Our device is successfully sending data to cloud server, but at every 30 second its get disconnected. So as hack we are reconnecting it whenever we want to send data to our cloud. But can someone figured out why is it so, Why it gets disconnected after some time.

@sohil4932, are you using a 3rd part SIM?

Have you upgraded the firmware of Electron?