From d44c86438796a5e6be9606a389c21ef7119719e6 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 10 Dec 2024 15:27:09 +0000 Subject: [PATCH] Add arduino/esp32-http example --- examples/arduino/esp32-http/esp32-http.ino | 63 +++++++++++++++++++ examples/arduino/esp32-http/mongoose.c | 1 + examples/arduino/esp32-http/mongoose.h | 1 + examples/arduino/esp32-http/mongoose_config.h | 3 + 4 files changed, 68 insertions(+) create mode 100644 examples/arduino/esp32-http/esp32-http.ino create mode 120000 examples/arduino/esp32-http/mongoose.c create mode 120000 examples/arduino/esp32-http/mongoose.h create mode 100644 examples/arduino/esp32-http/mongoose_config.h diff --git a/examples/arduino/esp32-http/esp32-http.ino b/examples/arduino/esp32-http/esp32-http.ino new file mode 100644 index 00000000..9949b23a --- /dev/null +++ b/examples/arduino/esp32-http/esp32-http.ino @@ -0,0 +1,63 @@ +#include "mongoose.h" +#include "WiFi.h" + +#define LED_PIN LED_BUILTIN +#define WIFI_SSID "wifi_network_name" // Change this +#define WIFI_PASS "wifi_password" // And this + +struct mg_mgr mgr; + +// Crude function to get available RAM, for quick profiling +size_t getFreeRAM(void) { + size_t size = 0, increment = 1024; + void *p; + while ((p = malloc(size)) != NULL) free(p), size += increment; + return size; +} + +void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) { + if (ev == MG_EV_HTTP_MSG) { + struct mg_http_message *hm = (struct mg_http_message *)ev_data; + if (mg_match(hm->uri, mg_str("/api/led/on"), NULL)) { + digitalWrite(LED_PIN, HIGH); + mg_http_reply(c, 200, "", "{%m: %d}\n", MG_ESC("led"), digitalRead(LED_PIN)); + } else if (mg_match(hm->uri, mg_str("/api/led/off"), NULL)) { + digitalWrite(LED_PIN, LOW); + mg_http_reply(c, 200, "", "{%m: %d}\n", MG_ESC("led"), digitalRead(LED_PIN)); + } else { + mg_http_reply(c, 200, "", "ok, free RAM: %u\n", xPortGetFreeHeapSize()); + } + } +} + +void setup() { + pinMode(LED_PIN, OUTPUT); + Serial.begin(115200); + while (!Serial) delay(50); + + WiFi.mode(WIFI_STA); + WiFi.begin(WIFI_SSID, WIFI_PASS); + while(WiFi.status() != WL_CONNECTED) Serial.print("."), delay(100); + + Serial.println("\nConnected to the WiFi network"); + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); + + mg_mgr_init(&mgr); + mg_log_set(MG_LL_DEBUG); + mg_log_set_fn([](char ch, void *) { Serial.print(ch); }, NULL); + mg_http_listen(&mgr, "http://0.0.0.0", http_ev_handler, NULL); +} + +void loop() { + mg_mgr_poll(&mgr, 0); +} + +extern "C" int lwip_hook_ip6_input(struct pbuf *p, struct netif *inp) __attribute__((weak)); +extern "C" int lwip_hook_ip6_input(struct pbuf *p, struct netif *inp) { + if (ip6_addr_isany_val(inp->ip6_addr[0].u_addr.ip6)) { + pbuf_free(p); + return 1; + } + return 0; +} diff --git a/examples/arduino/esp32-http/mongoose.c b/examples/arduino/esp32-http/mongoose.c new file mode 120000 index 00000000..5e522bbc --- /dev/null +++ b/examples/arduino/esp32-http/mongoose.c @@ -0,0 +1 @@ +../../../mongoose.c \ No newline at end of file diff --git a/examples/arduino/esp32-http/mongoose.h b/examples/arduino/esp32-http/mongoose.h new file mode 120000 index 00000000..ee4ac823 --- /dev/null +++ b/examples/arduino/esp32-http/mongoose.h @@ -0,0 +1 @@ +../../../mongoose.h \ No newline at end of file diff --git a/examples/arduino/esp32-http/mongoose_config.h b/examples/arduino/esp32-http/mongoose_config.h new file mode 100644 index 00000000..1149a330 --- /dev/null +++ b/examples/arduino/esp32-http/mongoose_config.h @@ -0,0 +1,3 @@ +#pragma once + +#define MG_ARCH MG_ARCH_ESP32