diff --git a/samples/net/mqtt_publisher/overlay-websocket.conf b/samples/net/mqtt_publisher/overlay-websocket.conf new file mode 100644 index 00000000000..a2611225da7 --- /dev/null +++ b/samples/net/mqtt_publisher/overlay-websocket.conf @@ -0,0 +1,3 @@ +CONFIG_WEBSOCKET_CLIENT=y +CONFIG_MQTT_LIB_WEBSOCKET=y +CONFIG_HEAP_MEM_POOL_SIZE=1500 diff --git a/samples/net/mqtt_publisher/src/config.h b/samples/net/mqtt_publisher/src/config.h index b8341a7f54e..29134a5b383 100644 --- a/samples/net/mqtt_publisher/src/config.h +++ b/samples/net/mqtt_publisher/src/config.h @@ -31,9 +31,17 @@ #endif #ifdef CONFIG_MQTT_LIB_TLS +#ifdef CONFIG_MQTT_LIB_WEBSOCKET +#define SERVER_PORT 9001 +#else #define SERVER_PORT 8883 +#endif /* CONFIG_MQTT_LIB_WEBSOCKET */ +#else +#ifdef CONFIG_MQTT_LIB_WEBSOCKET +#define SERVER_PORT 9001 #else #define SERVER_PORT 1883 +#endif /* CONFIG_MQTT_LIB_WEBSOCKET */ #endif #define APP_SLEEP_MSECS 500 diff --git a/samples/net/mqtt_publisher/src/main.c b/samples/net/mqtt_publisher/src/main.c index 59ab7f6334e..ba5046448f5 100644 --- a/samples/net/mqtt_publisher/src/main.c +++ b/samples/net/mqtt_publisher/src/main.c @@ -21,6 +21,14 @@ LOG_MODULE_REGISTER(net_mqtt_publisher_sample, LOG_LEVEL_DBG); static u8_t rx_buffer[APP_MQTT_BUFFER_SIZE]; static u8_t tx_buffer[APP_MQTT_BUFFER_SIZE]; +#if defined(CONFIG_MQTT_LIB_WEBSOCKET) +/* Making RX buffer large enough that the full IPv6 packet can fit into it */ +#define MQTT_LIB_WEBSOCKET_RECV_BUF_LEN 1280 + +/* Websocket needs temporary buffer to store partial packets */ +static u8_t temp_ws_rx_buf[MQTT_LIB_WEBSOCKET_RECV_BUF_LEN]; +#endif + /* The mqtt client struct */ static struct mqtt_client client_ctx; @@ -293,7 +301,11 @@ static void client_init(struct mqtt_client *client) /* MQTT transport configuration */ #if defined(CONFIG_MQTT_LIB_TLS) +#if defined(CONFIG_MQTT_LIB_WEBSOCKET) + client->transport.type = MQTT_TRANSPORT_SECURE_WEBSOCKET; +#else client->transport.type = MQTT_TRANSPORT_SECURE; +#endif struct mqtt_sec_config *tls_config = &client->transport.tls.config; @@ -307,9 +319,22 @@ static void client_init(struct mqtt_client *client) tls_config->hostname = NULL; #endif +#else +#if defined(CONFIG_MQTT_LIB_WEBSOCKET) + client->transport.type = MQTT_TRANSPORT_NON_SECURE_WEBSOCKET; #else client->transport.type = MQTT_TRANSPORT_NON_SECURE; #endif +#endif + +#if defined(CONFIG_MQTT_LIB_WEBSOCKET) + client->transport.websocket.config.host = SERVER_ADDR; + client->transport.websocket.config.url = "/mqtt"; + client->transport.websocket.config.tmp_buf = temp_ws_rx_buf; + client->transport.websocket.config.tmp_buf_len = + sizeof(temp_ws_rx_buf); + client->transport.websocket.timeout = K_SECONDS(5); +#endif #if defined(CONFIG_SOCKS) mqtt_client_set_proxy(client, &socks5_proxy,