diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 286dd4da..45f3a2fb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -321,6 +321,7 @@ jobs: example: - path: esp32/device-dashboard - path: esp32/uart-bridge + - path: esp32/micropython - path: esp8266/http-client-server - path: microchip/same54-xpro/device-dashboard - path: microchip/same54-xpro/mqtt-client diff --git a/examples/esp32/micropython/Makefile b/examples/esp32/micropython/Makefile new file mode 100644 index 00000000..c6e61b3c --- /dev/null +++ b/examples/esp32/micropython/Makefile @@ -0,0 +1,26 @@ + +THISDIR = $(realpath $(CURDIR)) +ROOTDIR = $(realpath $(CURDIR)/../../..) +PORT ?= /dev/ttyUSB0 +DOCKER ?= docker run --rm $(DA) -v $(ROOTDIR):$(ROOTDIR) -w $(THISDIR) espressif/idf:v5.0.2 +# Note that the esp32 port needs the extra .. for relative paths due to the location of its main CMakeLists.txt file +CMD ?= bash -c '$(MAKE) -C micropython/ports/esp32 submodules && $(MAKE) -C micropython/ports/esp32 USER_C_MODULES=../../../../mongoose/micropython.cmake' + +all: example + +example: + true + +build: micropython + $(DOCKER) $(CMD) + +micropython: + $(DOCKER) git clone https://github.com/micropython/micropython.git + +clean: + $(DOCKER) rm -rf micropython + +flash: +flash: DA = --device $(PORT) +flash: CMD = bash -c '$(MAKE) -C micropython/ports/esp32 erase deploy' +flash: build diff --git a/examples/esp32/micropython/README.md b/examples/esp32/micropython/README.md new file mode 100644 index 00000000..683fa338 --- /dev/null +++ b/examples/esp32/micropython/README.md @@ -0,0 +1,23 @@ +# Mongoose as a MicroPython user module on the ESP32 + +The most basic usage is: + +```python +import network +import mongoose + +sta_if = network.WLAN(network.STA_IF) +sta_if.active(True) +sta_if.connect("", "") +while not sta_if.isconnected(): + pass + +mongoose.init() +while True: + mongoose.poll(1000) + +``` + +Check [_thread](https://docs.micropython.org/en/latest/library/_thread.html) support for more advanced usage + +https://docs.micropython.org/en/latest/develop/cmodules.html diff --git a/examples/esp32/micropython/mongoose/main.c b/examples/esp32/micropython/mongoose/main.c new file mode 100644 index 00000000..6a72d005 --- /dev/null +++ b/examples/esp32/micropython/mongoose/main.c @@ -0,0 +1,29 @@ +// Copyright (c) 2022 Cesanta Software Limited +// All rights reserved + +#include "mongoose.h" + +static void t_fn(void *arg) { // Tick periodically + MG_INFO(("tick")); + (void) arg; +} + +static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + if (ev == MG_EV_HTTP_MSG) mg_http_reply(c, 200, "", "hi\n"); + (void) fn_data; + (void) ev_data; +} + +static struct mg_mgr mgr; + +void mgmp_init(void) { + mg_mgr_init(&mgr); // Mongoose event manager + mg_log_set(MG_LL_DEBUG); // Set log level + + mg_timer_add(&mgr, 1000, MG_TIMER_REPEAT, t_fn, &mgr); + mg_http_listen(&mgr, "http://0.0.0.0:80", fn, &mgr); +} + +void mgmp_poll(int t) { + mg_mgr_poll(&mgr, t); +} diff --git a/examples/esp32/micropython/mongoose/micropython.cmake b/examples/esp32/micropython/mongoose/micropython.cmake new file mode 120000 index 00000000..34f70b20 --- /dev/null +++ b/examples/esp32/micropython/mongoose/micropython.cmake @@ -0,0 +1 @@ +../../../micropython/mongoose/micropython.cmake \ No newline at end of file diff --git a/examples/esp32/micropython/mongoose/micropython.mk b/examples/esp32/micropython/mongoose/micropython.mk new file mode 120000 index 00000000..0e35394e --- /dev/null +++ b/examples/esp32/micropython/mongoose/micropython.mk @@ -0,0 +1 @@ +../../../micropython/mongoose/micropython.mk \ No newline at end of file diff --git a/examples/esp32/micropython/mongoose/module.c b/examples/esp32/micropython/mongoose/module.c new file mode 120000 index 00000000..6b153452 --- /dev/null +++ b/examples/esp32/micropython/mongoose/module.c @@ -0,0 +1 @@ +../../../micropython/mongoose/module.c \ No newline at end of file diff --git a/examples/esp32/micropython/mongoose/mongoose.c b/examples/esp32/micropython/mongoose/mongoose.c new file mode 120000 index 00000000..7a2752cb --- /dev/null +++ b/examples/esp32/micropython/mongoose/mongoose.c @@ -0,0 +1 @@ +../../../../mongoose.c \ No newline at end of file diff --git a/examples/esp32/micropython/mongoose/mongoose.h b/examples/esp32/micropython/mongoose/mongoose.h new file mode 120000 index 00000000..daff1633 --- /dev/null +++ b/examples/esp32/micropython/mongoose/mongoose.h @@ -0,0 +1 @@ +../../../../mongoose.h \ No newline at end of file diff --git a/examples/esp32/micropython/mongoose/mongoose_custom.h b/examples/esp32/micropython/mongoose/mongoose_custom.h new file mode 100644 index 00000000..d5b597a6 --- /dev/null +++ b/examples/esp32/micropython/mongoose/mongoose_custom.h @@ -0,0 +1,2 @@ +// For some reason, IDF detection at build time does not work here +#define MG_ARCH MG_ARCH_ESP32