TCP socket fails to read after a short while

Hi everyone, I’m having some troubles with MQTT over a TCP socket. I am essentially using the same code as the one provided in the library, socket creation, connection, mqtt subscribe/publish/ping are working fine.

After connecting to a MQTT broker and subscribing to a topic, I poll “AT+USORD=0,0” to watch for new data coming. If data is published (using mosquitto_pub) to the topic every 2 o 3 seconds or so, then the socket reads everything and no data is lost. On the contrary if data publication (still using mosqutito_pub) is done every 5+ seconds, then the socket read seems to silently go idle (I keep on reading “+USORD: 0,0”). In this case sending data to the broker (for example through a ping) seems to re establish the communication and I can again receive data for a few seconds.

I tried this with a private broker and and I got the same result.

I tried to enable the “keep alive” TCP socket option but with no result.

I run out of ideas, any input would be greatly appreciated!



Hi Fred,

The R4 MQTT builtin is indeed not stable.
I recommend you use the extra libraries to build an MQTT on top of the TCP connection with our MQTT libraries.

Best regards,

Hi Jan thanks for your reply, maybe my message was not very clear, I am not using the built in mqtt, I implemented the protocol using a tcp socket (at+usocr), everything works perfectly except that after ~5 of inactivity (meaning 5s without getting any data from the broker) the socket seems to “lock” and cannot get any new data from the broker. Outgoing data keeps on working and sending data actually fully “awakes” the socket for 5 new seconds… I investigated the power management stuff and used:
but no luck so far.
I studied your library closely and cannot find any meaningful difference, did you experience any similar issue?

I ran some tests using your library and arduino test sketch, I have the same issue, if the broker ( issues a message every 2 seconds or so, the device reads every single one of them, if the broker stops for 10 seconds and starts again then the device stops receiving anything.
I updated the ublox firmware. No change.

Can you make sure that the +UPSV setting is disabled. This is the ‘idle’ mode which disables the UART after 6s of inactivity, new activity over the UART wakes the modem. This puts the modem in a lower power state, but interferes with the commands sent, as usually the first command is discarded as it triggers the wake.

Hi thanks for your answer, I tried “AT+UPSV?” and I got “’+UPSV: 0”, so according to the manual, power saving should be disabled if I am not mistaken.