From 511c9a6d4a47447eb493707c6cc5e58b1759ae49 Mon Sep 17 00:00:00 2001 From: Alexander Alashkin Date: Wed, 29 Mar 2017 14:40:52 +0100 Subject: [PATCH] Add simple mongoose/esp32 example (w/out mOS) PUBLISHED_FROM=0e407a2fb328107540d753123fe027f43860d0ef --- examples/ESP32_IDF/Makefile | 16 +++++ examples/ESP32_IDF/Makefile.build | 9 +++ examples/ESP32_IDF/README.md | 18 ++++++ examples/ESP32_IDF/main/component.mk | 17 +++++ examples/ESP32_IDF/main/main.c | 93 ++++++++++++++++++++++++++++ examples/ESP32_IDF/sdk.version | 1 + examples/Makefile | 2 +- 7 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 examples/ESP32_IDF/Makefile create mode 100644 examples/ESP32_IDF/Makefile.build create mode 100644 examples/ESP32_IDF/README.md create mode 100644 examples/ESP32_IDF/main/component.mk create mode 100644 examples/ESP32_IDF/main/main.c create mode 100644 examples/ESP32_IDF/sdk.version diff --git a/examples/ESP32_IDF/Makefile b/examples/ESP32_IDF/Makefile new file mode 100644 index 00000000..e40fd4c0 --- /dev/null +++ b/examples/ESP32_IDF/Makefile @@ -0,0 +1,16 @@ +SDK_VER=$(shell cat sdk.version) + +.PHONY: all + +MG_PATH = $(realpath $(PWD)/../../) + +all: + docker run --rm -it -v $(MG_PATH):/esp32_idf \ + $(SDK_VER) \ + bash -c "cd esp32_idf/examples/ESP32_IDF && \ + make -f Makefile.build defconfig && make -f Makefile.build" + +clean: + docker run --rm -it -v $(MG_PATH):/esp32_idf \ + $(SDK_VER) \ + bash -c "rm -rf esp32_idf/examples/ESP32_IDF/build" diff --git a/examples/ESP32_IDF/Makefile.build b/examples/ESP32_IDF/Makefile.build new file mode 100644 index 00000000..5a6de248 --- /dev/null +++ b/examples/ESP32_IDF/Makefile.build @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := esp32_idf + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/ESP32_IDF/README.md b/examples/ESP32_IDF/README.md new file mode 100644 index 00000000..4af679a0 --- /dev/null +++ b/examples/ESP32_IDF/README.md @@ -0,0 +1,18 @@ +This is a Mongoose "Hello, world" that can be compiled under Espressif IoT Development Framework for ESP32 + +It connects to WiFi network and serves a "hello world" page. + +Most of the the boilerplate comes from [project_template](https://github.com/espressif/esp-idf-template) with minimal changes. + +For building the example, you need to have [Docker](https://www.docker.com/products/docker) and use our pre-built SDK container. +To build just run in the example directory +``` +$ make +``` + +Note: before building, change `WIFI_SSID` and `WIFI_PASS` macros in main/main.c file + +Once built, use [esptool](https://github.com/espressif/esptool) for flashing +``` +$ python esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 115200 --before default_reset --after hard_reset write_flash -u --flash_mode dio --flash_freq 40m --flash_size detect 0x1000 build/bootloader/bootloader.bin 0x10000 build/esp32_idf.bin 0x8000 build/partitions_singleapp.bin +``` diff --git a/examples/ESP32_IDF/main/component.mk b/examples/ESP32_IDF/main/component.mk new file mode 100644 index 00000000..f8659a72 --- /dev/null +++ b/examples/ESP32_IDF/main/component.mk @@ -0,0 +1,17 @@ +# +# Main component makefile. +# +# This Makefile can be left empty. By default, it will take the sources in the +# src/ directory, compile them and link them into lib(subdirectory_name).a +# in the build directory. This behaviour is entirely configurable, +# please read the ESP-IDF documents if you need to do this. +# + +COMPONENT_OBJS = main.o mongoose.o + +mongoose.o: ../../../../mongoose.c + $(summary) "CC $@" + $(CC) $(CFLAGS) $(CPPFLAGS) \ + $(addprefix -I ,$(COMPONENT_INCLUDES)) \ + $(addprefix -I ,$(COMPONENT_EXTRA_INCLUDES)) \ + -c $< -o $@ diff --git a/examples/ESP32_IDF/main/main.c b/examples/ESP32_IDF/main/main.c new file mode 100644 index 00000000..3b9a0ee7 --- /dev/null +++ b/examples/ESP32_IDF/main/main.c @@ -0,0 +1,93 @@ +#include + +#include "freertos/FreeRTOS.h" + +#include "esp_event.h" +#include "esp_event_loop.h" +#include "esp_system.h" +#include "esp_wifi.h" +#include "nvs_flash.h" + +#include "./../../../mongoose.h" + +#define WIFI_SSID "ssid" +#define WIFI_PASS "pass" + +#define MG_LISTEN_ADDR "80" + +static esp_err_t event_handler(void *ctx, system_event_t *event) { + (void) ctx; + (void) event; + return ESP_OK; +} + +static void mg_ev_handler(struct mg_connection *nc, int ev, void *p) { + static const char *reply_fmt = + "HTTP/1.0 200 OK\r\n" + "Connection: close\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "Hello %s\n"; + + switch (ev) { + case MG_EV_ACCEPT: { + char addr[32]; + mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), + MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); + printf("Connection %p from %s\n", nc, addr); + break; + } + case MG_EV_HTTP_REQUEST: { + char addr[32]; + struct http_message *hm = (struct http_message *) p; + mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), + MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); + printf("HTTP request from %s: %.*s %.*s\n", addr, (int) hm->method.len, + hm->method.p, (int) hm->uri.len, hm->uri.p); + mg_printf(nc, reply_fmt, addr); + nc->flags |= MG_F_SEND_AND_CLOSE; + break; + } + case MG_EV_CLOSE: { + printf("Connection %p closed\n", nc); + break; + } + } +} + +void app_main(void) { + nvs_flash_init(); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + + /* Initializing WiFi */ + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + wifi_config_t sta_config = { + .sta = {.ssid = WIFI_SSID, .password = WIFI_PASS, .bssid_set = false}}; + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_config)); + ESP_ERROR_CHECK(esp_wifi_start()); + ESP_ERROR_CHECK(esp_wifi_connect()); + + /* Starting Mongoose */ + struct mg_mgr mgr; + struct mg_connection *nc; + + printf("Starting web-server on port %s\n", MG_LISTEN_ADDR); + + mg_mgr_init(&mgr, NULL); + + nc = mg_bind(&mgr, MG_LISTEN_ADDR, mg_ev_handler); + if (nc == NULL) { + printf("Error setting up listener!\n"); + return; + } + mg_set_protocol_http_websocket(nc); + + /* Processing events */ + while (1) { + mg_mgr_poll(&mgr, 1000); + } +} diff --git a/examples/ESP32_IDF/sdk.version b/examples/ESP32_IDF/sdk.version new file mode 100644 index 00000000..174eac89 --- /dev/null +++ b/examples/ESP32_IDF/sdk.version @@ -0,0 +1 @@ +docker.cesanta.com/esp32-build:1.0-r13 diff --git a/examples/Makefile b/examples/Makefile index a6c96ddc..9f364d2e 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -3,7 +3,7 @@ # `wildcard ./*/` works in both linux and linux/wine, while `wildcard */` enumerates nothing under wine SUBDIRS = $(sort $(dir $(wildcard ./*/))) -SUBDIRS:=$(filter-out ./ ./CC3200/ ./ESP8266_RTOS/ ./mbed/ ./MSP432/ ./nRF51/ ./nRF52/ ./NXP_K64/ ./NXP_LPC4088/ ./PIC32/ ./STM32F4_CC3100/ ./TM4C129/ ./WinCE/, $(SUBDIRS)) +SUBDIRS:=$(filter-out ./ ./CC3200/ ./ESP32_IDF/ ./ESP8266_RTOS/ ./mbed/ ./MSP432/ ./nRF51/ ./nRF52/ ./NXP_K64/ ./NXP_LPC4088/ ./PIC32/ ./STM32F4_CC3100/ ./TM4C129/ ./WinCE/, $(SUBDIRS)) ifeq ($(OS), Windows_NT) SUBDIRS:=$(filter-out ./netcat/ ./raspberry_pi_mjpeg_led/ ./captive_dns_server/, $(SUBDIRS))