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,

Parents
  • 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

Reply
  • 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

Children
No Data
Related