I have now reduced my code to something generic. In my Setup this works for approximately 23h20min without failure. This piece of code does not try to reconnect or restore Connection at the Moment.
Perhaps someone could try this on his core and a mqtt-server.
Thank You
// for compiling in the cloud you need to change this probably to "MQTT\MQTT.h"
#include "mqtt.h"
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
}
// you need to change this to Your MQTT-Server
byte server[] = { 192,168,2,2 };
MQTT client(server, 1883, callback);
int once = 0;
unsigned long last_time;
unsigned long now_time;
time_t starttime;
time_t nowtime;
unsigned int publish_success = 0;
unsigned int publish_fail = 0;
unsigned long fail1 = 0;
unsigned long fail2 = 0;
unsigned long fail3 = 0;
void setup() {
last_time = 0;
now_time = 0;
Serial1.begin(115200);
Serial1.println( "CrashTest start" );
}
void loop() {
boolean result;
if( ! once )
{
once = 1;
starttime = Time.now();
client.connect("CrashTest");
if( client.isConnected() )
{
client.publish("CrashTest","startup");
}
}
now_time = millis();
if( (now_time - last_time) > 50 )
{
last_time = now_time;
//Serial1.println( "every 5 seconds" );
//Serial1.println(Time.timeStr());
result=client.publish("CrashTest:","publish");
if( !result )
{
publish_fail++;
if( publish_fail == 1)
fail1 = millis();
if( publish_fail == 2)
fail2 = millis();
if( publish_fail == 3)
fail3 = millis();
}
else
{
publish_success++;
}
Serial1.print("publish_success ");
Serial1.print(publish_success);
Serial1.print(" -- publish_fail ");
Serial1.println(publish_fail);
if( publish_fail )
{
Serial1.print("fail1 ");
Serial1.println(fail1);
Serial1.print("fail2 ");
Serial1.println(fail2);
Serial1.print("fail3 ");
Serial1.println(fail3);
}
}
}
Please use this syntax to paste code in the forum: ~ kennethlimcp
``` <--- insert this
paste code here
``` <--- insert this