From 0bb017c1d52f4feb25aace7547550ef5b8b7d186 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Wed, 3 Nov 2021 20:58:57 +0000 Subject: [PATCH] Add examples/mqtt-client-aws-iot --- examples/mqtt-client-aws-iot/Makefile | 11 ++++ examples/mqtt-client-aws-iot/ca.pem | 1 + examples/mqtt-client-aws-iot/main.c | 91 +++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 examples/mqtt-client-aws-iot/Makefile create mode 120000 examples/mqtt-client-aws-iot/ca.pem create mode 100644 examples/mqtt-client-aws-iot/main.c diff --git a/examples/mqtt-client-aws-iot/Makefile b/examples/mqtt-client-aws-iot/Makefile new file mode 100644 index 00000000..d89293ab --- /dev/null +++ b/examples/mqtt-client-aws-iot/Makefile @@ -0,0 +1,11 @@ +PROG ?= example +CFLAGS ?= -W -Wall -O2 -DMG_ENABLE_MBEDTLS=1 -lmbedtls -lmbedcrypto -lmbedx509 + +all: $(PROG) + $(RUN) ./$(PROG) $(ARGS) + +$(PROG): main.c + $(CC) main.c ../../mongoose.c -I../.. $(CFLAGS) -o $(PROG) + +clean: + rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb diff --git a/examples/mqtt-client-aws-iot/ca.pem b/examples/mqtt-client-aws-iot/ca.pem new file mode 120000 index 00000000..8addd9e2 --- /dev/null +++ b/examples/mqtt-client-aws-iot/ca.pem @@ -0,0 +1 @@ +../../test/data/ca.pem \ No newline at end of file diff --git a/examples/mqtt-client-aws-iot/main.c b/examples/mqtt-client-aws-iot/main.c new file mode 100644 index 00000000..9310d569 --- /dev/null +++ b/examples/mqtt-client-aws-iot/main.c @@ -0,0 +1,91 @@ +// Copyright (c) 2020 Cesanta Software Limited +// All rights reserved +// +// Example MQTT client. It performs the following steps: +// 1. Connects to the AWS IoT MQTT server +// 2. When connected, subscribes to the topic `s_rx_topic` +// 3. Publishes message `hello` to the `s_tx_topic` periodically +// +// This example requires TLS support. By default, it is built with mbedTLS, +// therefore make sure mbedTLS is installed. To build with OpenSSL, execute: +// make clean all CFLAGS="-W -Wall -DMG_ENABLE_OPENSSL=1 -lssl" + +// In order to get MQTT URL, login to AWS IoT, click on "Settings" on the left +// bar, copy the "Endpoint" URL. +static const char *s_url = + "mqtts://a1pjwh2bop1ojt-ats.iot.eu-west-1.amazonaws.com"; + +// To create certificates: +// 1. Click Policies -> Create, fill fields: +// Name : Policy1 +// Action : iot:* +// Resource ARN: * +// Effect : allow +// then, click "Create" +// 2. Click Manage -> Things -> Create things -> Create single thing -> Next +// Thing name: t1, no shadow, Next +// Auto-generate new certificate, Next +// Select policy Policy1, Create thing +// 3. From the dialog box that appears, download: +// xxx-certificate.pem.crt as cert.pem to the example directory +// xxx-private.pem.key as key.pem to the example directory +static const char *s_cert = "cert.pem"; +static const char *s_key = "key.pem"; + +static const char *s_rx_topic = "d/rx"; +static const char *s_tx_topic = "d/tx"; +static int s_qos = 1; + +#include "mongoose.h" + +static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + if (ev == MG_EV_OPEN) { + // c->is_hexdumping = 1; + } else if (ev == MG_EV_ERROR) { + // On error, log error message + LOG(LL_ERROR, ("%p %s", c->fd, (char *) ev_data)); + } else if (ev == MG_EV_CONNECT) { + // Set up 2-way TLS that is required by AWS IoT + struct mg_tls_opts opts = { + .ca = "ca.pem", .cert = s_cert, .certkey = s_key}; + mg_tls_init(c, &opts); + } else if (ev == MG_EV_MQTT_OPEN) { + // MQTT connect is successful + struct mg_str topic = mg_str(s_rx_topic); + LOG(LL_INFO, ("Connected to %s", s_url)); + LOG(LL_INFO, ("Subscribing to %s", s_rx_topic)); + mg_mqtt_sub(c, &topic, s_qos); + c->label[0] = 'X'; // Set a label that we're logged in + } else if (ev == MG_EV_MQTT_MSG) { + // When we receive MQTT message, print it + struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data; + LOG(LL_INFO, ("Received on %.*s : %.*s", (int) mm->topic.len, mm->topic.ptr, + (int) mm->data.len, mm->data.ptr)); + } else if (ev == MG_EV_POLL && c->label[0] == 'X') { + static unsigned long prev_second; + unsigned long now_second = (*(unsigned long *) ev_data) / 1000; + if (now_second != prev_second) { + struct mg_str topic = mg_str(s_tx_topic), data = mg_str("{\"a\":123}"); + LOG(LL_INFO, ("Publishing to %s", s_tx_topic)); + mg_mqtt_pub(c, &topic, &data, s_qos, false); + prev_second = now_second; + } + } + + if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) { + LOG(LL_INFO, ("Got event %d, stopping...", ev)); + *(bool *) fn_data = true; // Signal that we're done + } +} + +int main(void) { + struct mg_mgr mgr; + struct mg_mqtt_opts opts = {.qos = s_qos}; + bool done = false; + mg_mgr_init(&mgr); // Initialise event manager + LOG(LL_INFO, ("Connecting to %s", s_url)); // Inform that we're starting + mg_mqtt_connect(&mgr, s_url, &opts, fn, &done); // Create client connection + while (!done) mg_mgr_poll(&mgr, 1000); // Loop until done + mg_mgr_free(&mgr); // Finished, cleanup + return 0; +}