When using GitHub - arduino-libraries/ArduinoMqttClient: ArduinoMqttClient Library for Arduino Arduino MQTT Client on my MKR NB1500, I find it frequently stuck on .poll() . I am wondering if there is a way I could write my code in a way that while .poll() is running, there is a timer on the side and if it times out kills the process or just reset the Arduino?
I found that the mqtt.loop() getting whacked out can be a function of the network connection. I've not used client.poll().
Yes, I am suspecting there maybe an network issue. I already check if GPRS is attached before running the .poll(). However, it still freeze sometimes.
This is how I connect to WiFi and the MQTT Broker,
void MQTTkeepalive( void *pvParameters )
{
sema_MQTT_KeepAlive = xSemaphoreCreateBinary();
xSemaphoreGive( sema_MQTT_KeepAlive ); // found keep alive can mess with a publish, stop keep alive during publish
// setting must be set before a mqtt connection is made
MQTTclient.setKeepAlive( 90 ); // setting keep alive to 90 seconds makes for a very reliable connection, must be set before the 1st connection is made.
for (;;)
{
//check for a is-connected and if the WiFi 'thinks' its connected, found checking on both is more realible than just a single check
if ( (wifiClient.connected()) && (WiFi.status() == WL_CONNECTED) )
{
xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY ); // whiles MQTTlient.loop() is running no other mqtt operations should be in process
MQTTclient.loop();
xSemaphoreGive( sema_MQTT_KeepAlive );
}
else {
log_i( "MQTT keep alive found MQTT status %s WiFi status %s", String(wifiClient.connected()), String(WiFi.status()) );
if ( !(wifiClient.connected()) || !(WiFi.status() == WL_CONNECTED) )
{
connectToWiFi();
}
connectToMQTT();
}
vTaskDelay( 250 ); //task runs approx every 250 mS
}
vTaskDelete ( NULL );
}
Using WiFi? Issue a disconnect before connecting so that the WiFi stack gets set to default values before a connection is made.
Which MQTT library are you using. There are many different one out there.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.