mongoose/examples/mqtt-client/main.c

94 lines
3.3 KiB
C
Raw Normal View History

2020-12-12 18:03:19 +08: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
2021-11-10 20:57:02 +08:00
// 2. When connected, subscribes to the topic `s_sub_topic`
// 3. Publishes message `hello` to the `s_pub_topic`
// 4. Receives that message back from the subscribed topic and closes
// 5. Timer-based reconnection logic revives the connection when it is down
2020-12-12 18:03:19 +08:00
//
// To enable SSL/TLS, see https://mongoose.ws/tutorials/tls/#how-to-build
2020-12-12 18:03:19 +08:00
#include "mongoose.h"
2023-02-23 03:38:21 +08:00
static const char *s_url =
#if MG_ENABLE_MBEDTLS || MG_ENABLE_OPENSSL
"mqtts://broker.hivemq.com:8883";
#else
"mqtt://broker.hivemq.com:1883";
#endif
2021-11-10 20:57:02 +08:00
static const char *s_sub_topic = "mg/+/test";
static const char *s_pub_topic = "mg/clnt/test";
2021-11-01 21:29:44 +08:00
static int s_qos = 1;
2021-11-10 20:57:02 +08:00
static struct mg_connection *s_conn;
2020-12-12 18:03:19 +08:00
2021-11-11 15:36:25 +08:00
// Handle interrupts, like Ctrl-C
static int s_signo;
static void signal_handler(int signo) {
s_signo = signo;
}
2020-12-12 18:03:19 +08:00
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_OPEN) {
MG_INFO(("CREATED"));
// c->is_hexdumping = 1;
} else if (ev == MG_EV_ERROR) {
2020-12-12 18:03:19 +08:00
// On error, log error message
MG_ERROR(("%p %s", c->fd, (char *) ev_data));
2020-12-12 18:03:19 +08: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_MQTT_OPEN) {
// MQTT connect is successful
2021-11-10 20:57:02 +08:00
struct mg_str subt = mg_str(s_sub_topic);
struct mg_str pubt = mg_str(s_pub_topic), data = mg_str("hello");
MG_INFO(("CONNECTED to %s", s_url));
mg_mqtt_sub(c, subt, s_qos);
MG_INFO(("SUBSCRIBED to %.*s", (int) subt.len, subt.ptr));
2021-11-10 20:57:02 +08:00
mg_mqtt_pub(c, pubt, data, s_qos, false);
2022-06-11 03:25:56 +08:00
MG_INFO(("PUBLISHED %.*s -> %.*s", (int) data.len, data.ptr,
(int) pubt.len, pubt.ptr));
2020-12-12 18:03:19 +08:00
} else if (ev == MG_EV_MQTT_MSG) {
// When we get echo response, print it
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
MG_INFO(("RECEIVED %.*s <- %.*s", (int) mm->data.len, mm->data.ptr,
(int) mm->topic.len, mm->topic.ptr));
2021-11-10 20:57:02 +08:00
c->is_closing = 1;
} else if (ev == MG_EV_CLOSE) {
MG_INFO(("CLOSED"));
2021-11-10 20:57:02 +08:00
s_conn = NULL; // Mark that we're closed
2020-12-12 18:03:19 +08:00
}
2021-11-10 20:57:02 +08:00
(void) fn_data;
}
2020-12-12 18:03:19 +08:00
2021-11-10 20:57:02 +08:00
// Timer function - recreate client connection if it is closed
static void timer_fn(void *arg) {
struct mg_mgr *mgr = (struct mg_mgr *) arg;
struct mg_mqtt_opts opts = {.clean = true,
.will_qos = s_qos,
2021-11-10 20:57:02 +08:00
.will_topic = mg_str(s_pub_topic),
.will_message = mg_str("goodbye")};
if (s_conn == NULL) s_conn = mg_mqtt_connect(mgr, s_url, &opts, fn, NULL);
2020-12-12 18:03:19 +08:00
}
int main(void) {
2021-11-01 21:29:44 +08:00
struct mg_mgr mgr;
2021-11-10 20:57:02 +08:00
int topts = MG_TIMER_REPEAT | MG_TIMER_RUN_NOW;
2021-11-01 21:29:44 +08:00
2021-11-11 15:36:25 +08:00
signal(SIGINT, signal_handler); // Setup signal handlers - exist event
signal(SIGTERM, signal_handler); // manager loop on SIGINT and SIGTERM
mg_mgr_init(&mgr); // Init event manager
mg_timer_add(&mgr, 3000, topts, timer_fn, &mgr); // Init timer
while (s_signo == 0) mg_mgr_poll(&mgr, 1000); // Event loop, 1s timeout
mg_mgr_free(&mgr); // Finished, cleanup
2021-11-01 21:29:44 +08:00
2020-12-12 18:03:19 +08:00
return 0;
}