Electron - MQTT stops working 18 hours after reboot

I have an Electron that I’m using to send measurements via MQTT to my broker. It is working for about 18 hours after rebooting each time. Then I stop getting messages to my broker.

When the messages stop arriving I’m getting 1 as a result from the function .isConnected() and 0 from the function .publish(vefgattTopic, msg.c_str()).

I’m also running code on the same device that is sending the measurements to the EasyIoT cloud using REST API which never fails.

Do you guys have any suggestion on what might be causing the MQTT to stop working ?

Here is my code regarding the mqtt:

#include "spark-dallas-temperature/spark-dallas-temperature.h"
#include "ThingSpeak/ThingSpeak.h"
#include "OneWire/OneWire.h"
#include "MQTT/MQTT.h"
#include "cellular_hal.h"

#define MEAS_INTERVAL        10

STARTUP(cellular_credentials_set("vmc.gprs.is", "", "", NULL));

void callback(char* topic, byte* payload, unsigned int length);

MQTT clientVefgatt("xxx", 1883, callback);
const char vefgattID[] = "xxx";
const char vefgattTopic[] = "m/xxx";

long int threshold = 0; 

OneWire oneWire(D2);
DallasTemperature dallas(&oneWire);

void setup()
    clientVefgatt.connect(vefgattID, "xxx", "xxx");

void loop() 
    if(threshold < millis())
        threshold = millis() + (MEAS_INTERVAL * 60000);

void measureAndSend()
    float value0 = dallas.getTempCByIndex(0);
    float value1 = dallas.getTempCByIndex(1);
    float value2 = dallas.getTempCByIndex(2);
    float value3 = analogRead(A0)*(3.0/4095);
    float value4 = analogRead(A1)*(3.0/4095);

    ////////////////////////////// MQTT ////////////////////////////
    Serial.print("Connecting to broker: ");
    Serial.println(clientVefgatt.connect(vefgattID, "xxx", "xxx"));

        Serial.print("Not connected to the broker");
		clientVefgatt.connect(vefgattID, "xxx", "xxx");

        Serial.print("Connected  ");

        String msg = "{\"A\":[";
        msg += String(value0, 2);
        msg += ",";
        msg += String(value1, 2);
        msg += ",";
        msg += String(value2, 2);
        msg += ",";
        msg += String(value3, 2);
        msg += ",";
        msg += String(value4, 2);
        msg += ",0],\"D\":[0,0,0,0,0,0]}";       
        Serial.println(clientVefgatt.publish(vefgattTopic, msg.c_str()));       
        Serial.println("Couldn't send to MQTT broker: ");

void callback(char* topic, byte* payload, unsigned int length) {


does it make a difference when you change the above to this?

    char msg[128];
    snprintf(msg, sizeof(msg), "{\"A\":[%.2f,%.2f,%.2f,%.2f,%.2f,0],\"D\":[0,0,0,0,0,0]}", value0, value1, value2, value3, value4);
    Serial.println(clientVefgatt.publish(vefgattTopic, msg)); 
1 Like

No. It does not make any difference.

Did you fix it? Im having a similar issue, after some time, the electron won’t connect to the broker