// Copyright (c) 2023 Cesanta Software Limited // All rights reserved #include "hal.h" #include "mongoose.h" static const char *s_url = "mqtt://broker.hivemq.com:1883"; static const char *s_sub_topic = "mg/+/test"; // Publish topic static const char *s_pub_topic = "mg/clnt/test"; // Subscribe topic static const uint8_t s_qos = 1; // MQTT QoS static struct mg_connection *s_conn; // Client connection void SystemInit(void) { // Called automatically by startup code clock_init(); rng_init(); } static volatile uint64_t s_ticks; // Milliseconds since boot void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms s_ticks++; } uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } return true; } static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { MG_INFO(("%lu CREATED", c->id)); // c->is_hexdumping = 1; } else if (ev == MG_EV_ERROR) { // On error, log error message MG_ERROR(("%lu ERROR %s", c->id, (char *) ev_data)); } else if (ev == MG_EV_MQTT_OPEN) { // MQTT connect is successful struct mg_str subt = mg_str(s_sub_topic); struct mg_str pubt = mg_str(s_pub_topic), data = mg_str("hello"); MG_INFO(("%lu CONNECTED to %s", c->id, s_url)); struct mg_mqtt_opts sub_opts; memset(&sub_opts, 0, sizeof(sub_opts)); sub_opts.topic = subt; sub_opts.qos = s_qos; mg_mqtt_sub(c, &sub_opts); MG_INFO(("%lu SUBSCRIBED to %.*s", c->id, (int) subt.len, subt.buf)); struct mg_mqtt_opts pub_opts; memset(&pub_opts, 0, sizeof(pub_opts)); pub_opts.topic = pubt; pub_opts.message = data; pub_opts.qos = s_qos, pub_opts.retain = false; mg_mqtt_pub(c, &pub_opts); MG_INFO(("%lu PUBLISHED %.*s -> %.*s", c->id, (int) data.len, data.buf, (int) pubt.len, pubt.buf)); } 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(("%lu RECEIVED %.*s <- %.*s", c->id, (int) mm->data.len, mm->data.buf, (int) mm->topic.len, mm->topic.buf)); } else if (ev == MG_EV_CLOSE) { MG_INFO(("%lu CLOSED", c->id)); s_conn = NULL; // Mark that we're closed } } // 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, .qos = s_qos, .topic = mg_str(s_pub_topic), .version = 4, .message = mg_str("bye")}; if (s_conn == NULL) s_conn = mg_mqtt_connect(mgr, s_url, &opts, fn, NULL); } int main(void) { gpio_input(BUTTON_PIN); gpio_output(LED_PIN); uart_init(UART_DEBUG, 115200); ethernet_init(); MG_INFO(("Starting, CPU freq %g MHz", (double) clock_sys_freq() / 1000000)); struct mg_mgr mgr; // Initialise mg_mgr_init(&mgr); // Mongoose event manager mg_log_set(MG_LL_DEBUG); // Set log level // Initialise Mongoose network stack struct mg_tcpip_driver_same54_data driver_data = {.mdc_cr = 5}; struct mg_tcpip_if mif = { .mac = GENERATE_LOCALLY_ADMINISTERED_MAC() /*{2, 3, 4, 5, 6, 7}*/, // Uncomment below for static configuration: // .ip = mg_htonl(MG_U32(192, 168, 0, 223)), // .mask = mg_htonl(MG_U32(255, 255, 255, 0)), // .gw = mg_htonl(MG_U32(192, 168, 0, 1)), .driver = &mg_tcpip_driver_same54, .driver_data = &driver_data}; mg_tcpip_init(&mgr, &mif); MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac)); while (mif.state != MG_TCPIP_STATE_READY) { mg_mgr_poll(&mgr, 0); } MG_INFO(("Initialising application...")); mg_timer_add(&mgr, 3000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr); MG_INFO(("Starting event loop")); for (;;) { mg_mgr_poll(&mgr, 0); } return 0; }