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()
{
dallas.begin();
Serial.begin(9600);
clientVefgatt.connect(vefgattID, "xxx", "xxx");
}
void loop()
{
if(threshold < millis())
{
threshold = millis() + (MEAS_INTERVAL * 60000);
measureAndSend();
}
}
void measureAndSend()
{
dallas.requestTemperatures();
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"));
if(!clientVefgatt.isConnected())
{
Serial.print("Not connected to the broker");
clientVefgatt.connect(vefgattID, "xxx", "xxx");
}
if(clientVefgatt.isConnected())
{
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()));
}
else
{
Serial.println("Couldn't send to MQTT broker: ");
}
}
void callback(char* topic, byte* payload, unsigned int length) {
}