Mqtt Connection suddenly error -128 after a few seconds

Hi,

I try to merge 2 examples on my custom board (based on nrf9160), which is mqtt_simple and locations. The goal is just publish location, which is longitude and lattitude, to an open source mqtt broker.

So there are several steps for firmware, those are : 

  1. Init modem
  2. Connect to mqtt broker
  3. Send dummy message to mqtt broker
  4. Obtaining a location
  5. Sending location to mqtt broker

In the last part, there is a tiime that when I try to publish location again, I got error -128. After double-check the error number code, -128 means socket is disconnect. Anybody have idea about this? Thank you so much.

Parents
  • Hello,

    Please allow me to do some follow up questions which might help in finding out what's wrong:
    Are you connected to the broker the whole time? How long time is it between the two messages? And how long is the keepalive? Are you using TLS?
    I'm guessing your using a MQTT event handler. Are there any log outputs from this between the two messages? Do you get the error every time, or just sometimes?

    Kind regards,

    Torje 

Reply
  • Hello,

    Please allow me to do some follow up questions which might help in finding out what's wrong:
    Are you connected to the broker the whole time? How long time is it between the two messages? And how long is the keepalive? Are you using TLS?
    I'm guessing your using a MQTT event handler. Are there any log outputs from this between the two messages? Do you get the error every time, or just sometimes?

    Kind regards,

    Torje 

Children
  • Hi, thanks for responding.

    Yes, my plan is the board always connected with a broker for a whole time. Let me know if there is an issue with this method.

    It is around one or two seconds.

    I will check for keepalive. How do I check keepalive settings on this, can you inform me?

    For TLS, I didn't use TLS for this yet.

    Below is the log information

    * Booting Zephyr OS build v3.2.99-ncs1 *
    Retrieving Location Started.
    
    Connecting to LTE...
    Connected to LTE
    Connecting.. Connected.
    Init client IPv4 Address found 3.228.54.173client_id = mqtt-client -- success.
    Connecting to mqtt  -- success.
    FDS Initialize  -- success.
    Poll initialize  -- success.
     -- success.
    MQTT client connectedSubscribing to: palmlab/pettracker/subscribe len 28Send ping message  -- success.
    Waiting for current time
    Requesting location with short GNSS timeout to trigger fallback to cellular...
    [00:00:09.705,596] <inf> location: Method specific timeout expired
    [00:00:22.745,697] <inf> location: Location retrieval failed using 'GNSS', trying with 'Cellular' next
    Got location:
      method: Cellular
      latitude: 49.555555
      longitude: 69.555555
      accuracy: 12500.0 m
      date: 2023-01-12
      time: 12:09:25.588 UTC
      Google maps URL: https://maps.google.com/?q=249.555555,69.555555
    
    Publishing Location using cellular service.
    Publishing longitude  -- success.
    Publishing latitude  -- success.
    Publishing both information  -- success.
    Requesting location with the default configuration...
    [00:02:26.023,315] <inf> location: Method specific timeout expired
    [00:02:26.030,548] <inf> location: Location retrieval failed using 'GNSS', trying with 'Cellular' next
    Got location:
      method: Cellular
      latitude: 49.555555
      longitude: 69.555555
      accuracy: 12500.0 m
      date: 2023-01-12
      time: 12:09:25.588 UTC
      Google maps URL: https://maps.google.com/?q=249.555555,69.555555
    
    Publishing Location using cellular service.
    Publishing longitude MQTT client disconnected: -128ERR data_publish (longitude) : -128

    As you can see, at the second time, it is trying to publish but looks like it is disconnected. 

    Thanks for your participate again, really appreciate it.

  • Hello again,

    Based on this, its hard for me to see any immediate source of the errors. I've had the same error before, but I can't remember what I did to solve it. I've merged different samples that you, so it might not even be the same thing anyway.

    What I can say, is that you might get disconnected if either side doesn't follow the protocol. You could implement some tests before trying to send the data. Verify the MQTT state, and if its not connected; try to reconnect before sending. But this would only cure the symptom, not the source. And it would be a power drain. My best tip is to keep debugging to see if you find the issue. 

    The default keepalive time is 60 seconds. If the keepalive ping is not responded to properly, it will cause a disconnect. You could try to expand this time using something like CONFIG_MQTT_KEEPALIVE = 1200 (this would set it to 20 minutes)

    I would also try other MQTT brokers, just to make sure that's not the problem.

    Uploading the code might be helpful if none of these tips are. Or maybe a Nordic Employee has some better tips or info than me.

    Kind regards,

    Torje

Related