mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-28 23:49:44 +08:00
Merge pull request #2455 from cesanta/micropython
Add Micropython on ESP32 example
This commit is contained in:
commit
a619de0508
1
.github/workflows/test.yml
vendored
1
.github/workflows/test.yml
vendored
@ -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
|
||||
|
26
examples/esp32/micropython/Makefile
Normal file
26
examples/esp32/micropython/Makefile
Normal file
@ -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
|
23
examples/esp32/micropython/README.md
Normal file
23
examples/esp32/micropython/README.md
Normal file
@ -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("<SSID>", "<PASSWORD>")
|
||||
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
|
29
examples/esp32/micropython/mongoose/main.c
Normal file
29
examples/esp32/micropython/mongoose/main.c
Normal file
@ -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);
|
||||
}
|
1
examples/esp32/micropython/mongoose/micropython.cmake
Symbolic link
1
examples/esp32/micropython/mongoose/micropython.cmake
Symbolic link
@ -0,0 +1 @@
|
||||
../../../micropython/mongoose/micropython.cmake
|
1
examples/esp32/micropython/mongoose/micropython.mk
Symbolic link
1
examples/esp32/micropython/mongoose/micropython.mk
Symbolic link
@ -0,0 +1 @@
|
||||
../../../micropython/mongoose/micropython.mk
|
1
examples/esp32/micropython/mongoose/module.c
Symbolic link
1
examples/esp32/micropython/mongoose/module.c
Symbolic link
@ -0,0 +1 @@
|
||||
../../../micropython/mongoose/module.c
|
1
examples/esp32/micropython/mongoose/mongoose.c
Symbolic link
1
examples/esp32/micropython/mongoose/mongoose.c
Symbolic link
@ -0,0 +1 @@
|
||||
../../../../mongoose.c
|
1
examples/esp32/micropython/mongoose/mongoose.h
Symbolic link
1
examples/esp32/micropython/mongoose/mongoose.h
Symbolic link
@ -0,0 +1 @@
|
||||
../../../../mongoose.h
|
2
examples/esp32/micropython/mongoose/mongoose_custom.h
Normal file
2
examples/esp32/micropython/mongoose/mongoose_custom.h
Normal file
@ -0,0 +1,2 @@
|
||||
// For some reason, IDF detection at build time does not work here
|
||||
#define MG_ARCH MG_ARCH_ESP32
|
Loading…
x
Reference in New Issue
Block a user