samples: net: mqtt_publisher: Add support for Websocket
Add a possibility to run MQTT over Websocket. Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
parent
b3b1ca7077
commit
cc76c75272
3
samples/net/mqtt_publisher/overlay-websocket.conf
Normal file
3
samples/net/mqtt_publisher/overlay-websocket.conf
Normal file
|
@ -0,0 +1,3 @@
|
|||
CONFIG_WEBSOCKET_CLIENT=y
|
||||
CONFIG_MQTT_LIB_WEBSOCKET=y
|
||||
CONFIG_HEAP_MEM_POOL_SIZE=1500
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue