2021-08-01 14:10:21 +01:00
|
|
|
// Copyright (c) 2020 Cesanta Software Limited
|
|
|
|
// All rights reserved
|
|
|
|
//
|
|
|
|
// Example MQTT client. It performs the following steps:
|
|
|
|
// 1. Connects to the MQTT server specified by `s_url` variable
|
|
|
|
// 2. When connected, subscribes to the topic `s_topic`
|
|
|
|
// 3. Publishes message `hello` to the `s_topic`
|
|
|
|
// 4. Receives that message back from the subscribed topic and exits
|
|
|
|
//
|
2022-07-04 17:47:17 +01:00
|
|
|
// To enable SSL/TLS, make SSL=OPENSSL or make SSL=MBEDTLS
|
2021-08-01 14:10:21 +01:00
|
|
|
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
|
|
|
static const char *s_url = "ws://broker.hivemq.com:8000/mqtt";
|
|
|
|
static const char *s_topic = "mg/test";
|
|
|
|
|
|
|
|
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
|
|
|
if (ev == MG_EV_ERROR) {
|
|
|
|
// On error, log error message
|
2022-02-12 18:17:25 +00:00
|
|
|
MG_ERROR(("%p %s", c->fd, (char *) ev_data));
|
2021-08-01 14:10:21 +01:00
|
|
|
} else if (ev == MG_EV_CONNECT) {
|
|
|
|
// If target URL is SSL/TLS, command client connection to use TLS
|
|
|
|
if (mg_url_is_ssl(s_url)) {
|
|
|
|
struct mg_tls_opts opts = {.ca = "ca.pem"};
|
|
|
|
mg_tls_init(c, &opts);
|
|
|
|
}
|
|
|
|
} else if (ev == MG_EV_WS_OPEN) {
|
|
|
|
// WS connection established. Perform MQTT login
|
2022-02-12 18:17:25 +00:00
|
|
|
MG_INFO(("Connected to WS. Logging in to MQTT..."));
|
2021-12-23 11:00:18 +00:00
|
|
|
struct mg_mqtt_opts opts = {.will_qos = 1,
|
2021-08-01 14:10:21 +01:00
|
|
|
.will_topic = mg_str(s_topic),
|
|
|
|
.will_message = mg_str("goodbye")};
|
|
|
|
size_t len = c->send.len;
|
2021-11-02 16:40:25 +00:00
|
|
|
mg_mqtt_login(c, &opts);
|
2021-08-01 14:10:21 +01:00
|
|
|
mg_ws_wrap(c, c->send.len - len, WEBSOCKET_OP_BINARY);
|
2022-07-04 17:47:17 +01:00
|
|
|
c->is_hexdumping = 1;
|
2021-08-01 14:10:21 +01:00
|
|
|
} else if (ev == MG_EV_WS_MSG) {
|
|
|
|
struct mg_mqtt_message mm;
|
|
|
|
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
|
2022-07-04 17:47:17 +01:00
|
|
|
uint8_t version = c->is_mqtt5 ? 5 : 4;
|
2022-02-12 18:17:25 +00:00
|
|
|
MG_INFO(("GOT %d bytes WS msg", (int) wm->data.len));
|
2022-06-28 11:31:13 +01:00
|
|
|
while ((mg_mqtt_parse((uint8_t *) wm->data.ptr, wm->data.len, version,
|
2022-07-04 14:32:12 -03:00
|
|
|
&mm)) == MQTT_OK) {
|
2021-08-01 14:10:21 +01:00
|
|
|
switch (mm.cmd) {
|
|
|
|
case MQTT_CMD_CONNACK:
|
|
|
|
mg_call(c, MG_EV_MQTT_OPEN, &mm.ack);
|
|
|
|
if (mm.ack == 0) {
|
|
|
|
struct mg_str topic = mg_str(s_topic), data = mg_str("hello");
|
|
|
|
size_t len = c->send.len;
|
2022-02-12 18:17:25 +00:00
|
|
|
MG_INFO(("CONNECTED to %s", s_url));
|
2022-01-19 09:57:27 +00:00
|
|
|
mg_mqtt_sub(c, topic, 1);
|
2022-06-28 16:27:49 -03:00
|
|
|
len = mg_ws_wrap(c, c->send.len - len, WEBSOCKET_OP_BINARY);
|
2022-02-12 18:17:25 +00:00
|
|
|
MG_INFO(("SUBSCRIBED to %.*s", (int) topic.len, topic.ptr));
|
2022-01-19 09:57:27 +00:00
|
|
|
mg_mqtt_pub(c, topic, data, 1, false);
|
2022-06-28 16:27:49 -03:00
|
|
|
MG_INFO(("PUBLISHED %.*s -> %.*s", (int) data.len, data.ptr,
|
2022-02-12 18:17:25 +00:00
|
|
|
(int) topic.len, topic.ptr));
|
2021-08-01 14:10:21 +01:00
|
|
|
len = mg_ws_wrap(c, c->send.len - len, WEBSOCKET_OP_BINARY);
|
|
|
|
} else {
|
2022-02-12 18:17:25 +00:00
|
|
|
MG_ERROR(("%lu MQTT auth failed, code %d", c->id, mm.ack));
|
2021-08-01 14:10:21 +01:00
|
|
|
c->is_closing = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MQTT_CMD_PUBLISH: {
|
2022-02-12 18:17:25 +00:00
|
|
|
MG_DEBUG(("%lu [%.*s] -> [%.*s]", c->id, (int) mm.topic.len,
|
|
|
|
mm.topic.ptr, (int) mm.data.len, mm.data.ptr));
|
|
|
|
MG_INFO(("RECEIVED %.*s <- %.*s", (int) mm.data.len, mm.data.ptr,
|
|
|
|
(int) mm.topic.len, mm.topic.ptr));
|
2021-08-01 14:10:21 +01:00
|
|
|
c->is_closing = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wm->data.ptr += mm.dgram.len;
|
|
|
|
wm->data.len -= mm.dgram.len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) {
|
|
|
|
*(bool *) fn_data = true; // Signal that we're done
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2022-08-01 11:19:32 +01:00
|
|
|
struct mg_mgr mgr; // Event manager
|
|
|
|
bool done = false; // Event handler flips it to true when done
|
|
|
|
mg_mgr_init(&mgr); // Initialise event manager
|
|
|
|
mg_log_set(MG_LL_DEBUG); // Set log level
|
2021-08-01 14:10:21 +01:00
|
|
|
mg_ws_connect(&mgr, s_url, fn, &done, NULL); // Create client connection
|
|
|
|
while (done == false) mg_mgr_poll(&mgr, 1000); // Event loop
|
|
|
|
mg_mgr_free(&mgr); // Finished, cleanup
|
|
|
|
return 0;
|
|
|
|
}
|