8 years, 10 months ago.

Session dropping

I've written a program that takes in sensor values via an NRF24 radio module converts them into an appropriate output and sends them off to an MQTT server (where OpenHAB picks them up)

The code works for a short period of time (between 20 minutes and a couple of hours) and then stops (mqtt messages stop turning up at the server). It looks to me like it's the session between the mBed and the MQTT server that is dropping/timing out.

I think this is the case because I can still ping the IP address of the mBed (so it's not crashed) and when the code receives a radio message it flashes an LED. This is still happening.

I've tried to trigger the connection routine again by checking the isConnected function with the code if(!client.isConnected()) but this doesn't seem to work.

My code is here: https://developer.mbed.org/users/Mephi/code/IoT_NRF24/file/cec410958705/main.cpp One thing to note is that I've moved the MQTT publishing into it's own function 'sendMessage'.

Any idea what could be going wrong?

Thanks,

Matt

Question relating to:

An API for using MQTT over multiple transports MQTT, MQTTClient

1 Answer

8 years, 10 months ago.

Hi Matt,

I think you need to reinitialize the variables ipstack and client before triggering the connection routine again.

Basically insert the following lines in the if(!isConnected) condition

ipstack = MQTTEthernet();
MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);

I was facing a similar problem and this worked for me. However, after reconnecting and resubscribing, my messageArrived() function is triggered twice for each received message, and the second one only contains the first character of the actual message. I'm still trying to resolve this. Any idea why this would be happening?

The problem is my if(!isConnected) is never being triggered. I put the LED line in there to see if it's called.

For your messageArrived function, could it be making a second MQTTClient rather than overwriting the first one?

posted by Matthew Green 25 Jun 2015

I see. Try inserting printf statements at different points in the MQTT client header file for the functions involving publish. That may help you find the point of failure

posted by Farzan Hasani 26 Jun 2015