Commit 511c9a6d authored by Alexander Alashkin's avatar Alexander Alashkin Committed by Cesanta Bot

Add simple mongoose/esp32 example (w/out mOS)

PUBLISHED_FROM=0e407a2fb328107540d753123fe027f43860d0ef
parent 8ec47111
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"
#
# 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
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
```
#
# 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 $@
#include <string.h>
#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);
}
}
docker.cesanta.com/esp32-build:1.0-r13
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# `wildcard ./*/` works in both linux and linux/wine, while `wildcard */` enumerates nothing under wine # `wildcard ./*/` works in both linux and linux/wine, while `wildcard */` enumerates nothing under wine
SUBDIRS = $(sort $(dir $(wildcard ./*/))) 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) ifeq ($(OS), Windows_NT)
SUBDIRS:=$(filter-out ./netcat/ ./raspberry_pi_mjpeg_led/ ./captive_dns_server/, $(SUBDIRS)) SUBDIRS:=$(filter-out ./netcat/ ./raspberry_pi_mjpeg_led/ ./captive_dns_server/, $(SUBDIRS))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment