This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

MQTT_simple sample with Websocket and Websocket Secure

Hi !

I'm doing some testing on mqtt with the nrf9160 DevKit.

So far, everything is working with TCP and TLS and I would like to use WSS. I started with WS but I can't make it work ...

Here's what I tried 

In prj.conf, I added (seen on zpehyr's websocket documentation):

  • CONFIG_WEBSOCKET_CLIENT=y
  • CONFIG_MQTT_LIB_WEBSOCKET=y

and I modified :

  • CONFIG_MQTT_BROKER_PORT=80 (as mentionned on mqtt.eclipseprojects.io)
  • CONFIG_NET_NATIVE=y

In the main.c I made some modification 

client_init : 

[...]
#elif defined(CONFIG_MQTT_LIB_WEBSOCKET)
        client->transport.type = MQTT_TRANSPORT_NON_SECURE_WEBSOCKET;
        struct websocket_request *ws_cfg = &(client->transport).websocket.config;
        ws_cfg->host = CONFIG_MQTT_BROKER_HOSTNAME;
        ws_cfg->tmp_buf = tmp_buffer;
	ws_cfg->tmp_buf_len = sizeof(tmp_buffer);
        client->transport.websocket.timeout = 2000;
#else
	client->transport.type = MQTT_TRANSPORT_NON_SECURE;
#endif

	return err;
}

fds_init :

static int fds_init(struct mqtt_client *c)
{
	if (c->transport.type == MQTT_TRANSPORT_NON_SECURE) {
		fds.fd = c->transport.tcp.sock;
	} else {
#if defined(CONFIG_MQTT_LIB_TLS)
		fds.fd = c->transport.tls.sock;
#elif defined(CONFIG_MQTT_LIB_WEBSOCKET)
                fds.fd = c->transport.websocket.sock;
#else
		return -ENOTSUP;
#endif
	}

	fds.events = POLLIN;

	return 0;
}

In the debug terminal here's what I get

<dbg> net_mqtt_sock_tcp.mqtt_client_tcp_connect: (0x200153a8): Created socket 1
<dbg> net_mqtt_sock_tcp.mqtt_client_tcp_connect: (0x200153a8): Connect completed
<dbg> net_mqtt_websocket.mqtt_client_websocket_connect: (0x200153a8): Connect completed

--- 9999 messages dropped ---
<dbg> net_mqtt_rx.mqtt_read_message_chunk: (0x200153a8): [CID 0x20015558]: Transport read error: -11
<dbg> net_mqtt.mqtt_input: (0x200153a8): state:0x00000006
.... 


<dbg> net_mqtt_rx.mqtt_read_message_chunk: (0x200153a8): [CID 0x20015558]: Connection closed.
<dbg> net_mqtt_websocket.mqtt_client_websocket_disconnect: (0x200153a8): Closing socket 2
<inf> mqtt_simple: MQTT client disconnected: -57
<err> mqtt_simple: mqtt_input: -57
<inf> mqtt_simple: Disconnecting MQTT client...
<err> mqtt_simple: Could not disconnect MQTT client: -57
<inf> mqtt_simple: Reconnecting in 60 seconds...
<dbg> net_mqtt_sock_tcp.mqtt_client_tcp_connect: (0x200153a8): Created socket 1
<dbg> net_mqtt_sock_tcp.mqtt_client_tcp_connect: (0x200153a8): Connect completed
<dbg> net_mqtt_websocket.mqtt_client_websocket_connect: (0x200153a8): Websocket connect failed (-53)
<err> mqtt_simple: mqtt_connect -53
<inf> mqtt_simple: Reconnecting in 60 seconds...

What am I doing wrong here ? Some helps would be much appreciated !

Best regards,

  • Hello,

    which NCS version are you using? It seems like we are having some issues with our websocket implementation.

    Could you try to change this

    #elif defined(CONFIG_MQTT_LIB_WEBSOCKET)
                    fds.fd = c->transport.websocket.sock;
    #else

    to

    #elif defined(CONFIG_MQTT_LIB_WEBSOCKET)
                    fds.fd = c->transport.tcp.sock;
    #else

    and check if that makes any difference?

    I will report this to our development team.

    Regards,

    Markus

  • Hello,

    Thank you for the reply.

    Currently, I am usine the NCS v1.5.1. I use the Serial LTE Modem sample as a basis in parallel and I had some trouble to build with the 1.6.0.

    I made the changes you suggested and it actually changed something ! Here's the debug terminal :

    *** Booting Zephyr OS build v2.4.99-ncs2  ***
    <inf> mqtt_simple: The MQTT simple sample started
    <inf> mqtt_simple: Disabling PSM and eDRX
    <inf> mqtt_simple: LTE Link Connecting...
    <inf> mqtt_simple: LTE Link Connected!
    <inf> mqtt_simple: IPv4 Address found 137.135.83.217
    <dbg> net_mqtt_sock_tcp.mqtt_client_tcp_connect: (0x200153a8): Created socket 1
    <dbg> net_mqtt_sock_tcp.mqtt_client_tcp_connect: (0x200153a8): Connect completed
    <dbg> net_mqtt_websocket.mqtt_client_websocket_connect: (0x200153a8): Connect completed
    <dbg> net_mqtt_enc.connect_request_encode: (0x200153a8): Encoding Protocol Description. Str:MQTT Size:00000004.
    <dbg> net_mqtt_enc.pack_utf8_str: (0x200153a8): >> str_size:00000006 cur:0x20016e3f, end:0x20016eba
    <dbg> net_mqtt_enc.pack_uint16: (0x200153a8): >> val:0004 cur:0x20016e3f, end:0x20016eba
    <dbg> net_mqtt_enc.connect_request_encode: (0x200153a8): Encoding Protocol Version 04.
    <dbg> net_mqtt_enc.pack_uint8: (0x200153a8): >> val:04 cur:0x20016e45, end:0x20016eba
    <dbg> net_mqtt_enc.pack_uint8: (0x200153a8): >> val:00 cur:0x20016e46, end:0x20016eba
    <dbg> net_mqtt_enc.connect_request_encode: (0x200153a8): Encoding Keep Alive Time 003c.
    <dbg> net_mqtt_enc.pack_uint16: (0x200153a8): >> val:003c cur:0x20016e47, end:0x20016eba
    <dbg> net_mqtt_enc.connect_request_encode: (0x200153a8): Encoding Client Id. Str:MQTT_NRF9160_CLIENT Size:00000013.
    <dbg> net_mqtt_enc.pack_utf8_str: (0x200153a8): >> str_size:00000015 cur:0x20016e49, end:0x20016eba
    <dbg> net_mqtt_enc.pack_uint16: (0x200153a8): >> val:0013 cur:0x20016e49, end:0x20016eba
    <dbg> net_mqtt_enc.mqtt_encode_fixed_header: (0x200153a8): << msg type:0x10 length:0x0000001f
    <dbg> net_mqtt_enc.packet_length_encode: (0x200153a8): >> length:0x0000001f cur:(nil), end:(nil)
    <dbg> net_mqtt_enc.mqtt_encode_fixed_header: (0x200153a8): Fixed header length = 02
    <dbg> net_mqtt_enc.pack_uint8: (0x200153a8): >> val:10 cur:0x20016e3d, end:0x20016eba
    <dbg> net_mqtt_enc.packet_length_encode: (0x200153a8): >> length:0x0000001f cur:0x20016e3e, end:0x20016eba
    <dbg> net_mqtt.client_connect: (0x200153a8): Connect completed
    <dbg> net_mqtt_dec.unpack_uint8: (0x200153a8): << val:00
    --- 7 messages dropped ---
    <dbg> net_mqtt_dec.unpack_uint8: (0x200153a8): << val:00
    <dbg> net_mqtt_dec.connect_ack_decode: (0x200153a8): [CID 0x20015558]: session_present_flag: 0
    <dbg> net_mqtt_rx.mqtt_handle_packet: (0x200153a8): [CID 0x20015558]: return_code: 0
    <inf> mqtt_simple: MQTT client connected
    <inf> mqtt_simple: Subscribing to: topic/simple/test2 len 18
    <dbg> net_mqtt.mqtt_subscribe: (0x200153a8): [CID 0x20015558]:[State 0x06]: >> message id 0x04d2 topic count 0x0001
    <dbg> net_mqtt_enc.pack_uint16: (0x200153a8): >> val:04d2 cur:0x20016e3f, end:0x20016eba
    <dbg> net_mqtt_enc.pack_utf8_str: (0x200153a8): >> str_size:00000014 cur:0x20016e41, end:0x20016eba
    <dbg> net_mqtt_enc.pack_uint16: (0x200153a8): >> val:0012 cur:0x20016e41, end:0x20016eba
    <dbg> net_mqtt_enc.pack_uint8: (0x200153a8): >> val:01 cur:0x20016e55, end:0x20016eba
    <dbg> net_mqtt_enc.mqtt_encode_fixed_header: (0x200153a8): << msg type:0x82 length:0x00000017
    <dbg> net_mqtt_enc.packet_length_encode: (0x200153a8): >> length:0x00000017 cur:(nil), end:(nil)
    <dbg> net_mqtt_enc.mqtt_encode_fixed_header: (0x200153a8): Fixed header length = 02
    <dbg> net_mqtt_enc.pack_uint8: (0x200153a8): >> val:82 cur:0x20016e3d, end:0x20016eba
    <dbg> net_mqtt_enc.packet_length_encode: (0x200153a8): >> length:0x00000017 cur:0x20016e3e, end:0x20016eba
    <dbg> net_mqtt.client_write: (0x200153a8): [0x20015558]: Transport writing 25 bytes.
    <dbg> net_mqtt.client_write: (0x200153a8): [0x20015558]: Transport write complete.
    <dbg> net_mqtt.mqtt_subscribe: (0x200153a8): [CID 0x20015558]:[State 0x06]: << result 0x00000000
    <dbg> net_mqtt.mqtt_input: (0x200153a8): state:0x00000006
    <dbg> net_mqtt_dec.unpack_uint8: (0x200153a8): >> cur:0x20016d3a, end:0x20016d3c
    <dbg> net_mqtt_dec.unpack_uint8: (0x200153a8): << val:90
    <dbg> net_mqtt_dec.packet_length_decode: (0x200153a8): length:0x00000003
    <dbg> net_mqtt_rx.mqtt_handle_packet: (0x200153a8): [CID 0x20015558]: Received MQTT_PKT_TYPE_SUBACK!
    <dbg> net_mqtt_dec.unpack_uint16: (0x200153a8): >> cur:0x20016d3c, end:0x20016d3f
    <dbg> net_mqtt_dec.unpack_uint16: (0x200153a8): << val:04d2
    <dbg> net_mqtt_dec.unpack_data: (0x200153a8): >> cur:0x20016d3e, end:0x20016d3f
    <dbg> net_mqtt_dec.unpack_data: (0x200153a8): << bin len:00000001
    <inf> mqtt_simple: SUBACK packet id: 1234

    After that, it seems to go to cpu_idle.S (l.107 cpsie i) and at some point I get the same error :

    <dbg> net_mqtt.client_write: (0x200153a8): [0x20015558]: Transport writing 2 bytes.
    <dbg> net_mqtt.client_write: (0x200153a8): [0x20015558]: Transport write complete.
    <dbg> net_mqtt.mqtt_input: (0x200153a8): state:0x00000006
    <dbg> net_mqtt_rx.mqtt_read_message_chunk: (0x200153a8): [CID 0x20015558]: Connection closed.
    <dbg> net_mqtt_websocket.mqtt_client_websocket_disconnect: (0x200153a8): Closing socket 2
    <inf> mqtt_simple: MQTT client disconnected: -57
    <err> mqtt_simple: mqtt_input: -57
    <inf> mqtt_simple: Disconnecting MQTT client...
    <err> mqtt_simple: Could not disconnect MQTT client: -57
    <inf> mqtt_simple: Reconnecting in 60 seconds...

    Quick question on the websocket buffer (websocket.config.tmp_buf), it should be a different buffer from the mqtt buffers, right ?

    Best regards,

  • Hello,

    Kindleh said:
    After that, it seems to go to cpu_idle.S (l.107 cpsie i) and at some point I get the same error :

    <dbg> net_mqtt.mqtt_input: (0x200153a8): state:0x00000006
    <dbg> net_mqtt_rx.mqtt_read_message_chunk: (0x200153a8): [CID 0x20015558]: Connection closed.

    This doesn't seem like an error, as the log will be printed when peer (MQTT broker) closes the underlying connection websocket/TCP. Why does this happen though is hard to tell based on the application logs only. One idea might be that the MQTT keepalive PING was sent too late? Theoretically the server should wait for 1,5 * keep-alive timeout, but if the server does not respect this requirement, it could close the connection. When the MQTT ping will be sent is controlled purely by the application, so it can be easily set to be sent earlier (see `mqtt_live()` function).

    Kindleh said:
    Quick question on the websocket buffer (websocket.config.tmp_buf), it should be a different buffer from the mqtt buffers, right ?

    Yes, these should be separate buffers.

    Regards,

    Markus

Related