From 1716af6c72d5d86d6e2a8c37d315a8219f7126b9 Mon Sep 17 00:00:00 2001 From: cpq Date: Thu, 24 Dec 2020 16:52:12 +0000 Subject: [PATCH] Add HTTP client code --- examples/esp32/Makefile | 7 ++++++- examples/esp32/README.md | 9 +++++++++ examples/esp32/main/main.c | 34 ++++++++++++++++++++++++++-------- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/examples/esp32/Makefile b/examples/esp32/Makefile index 85019156..bdf03ca6 100644 --- a/examples/esp32/Makefile +++ b/examples/esp32/Makefile @@ -3,8 +3,13 @@ ROOTDIR = $(realpath $(CURDIR)/../..) all: example -example: +example: main/main.c Makefile docker run --rm -v $(ROOTDIR):$(ROOTDIR) -w $(THISDIR) espressif/idf idf.py build +COMPORT ?= /dev/cu.SLAB_USBtoUART +ESPTOOL ?= esptool.py +flash: + cd build && $(ESPTOOL) --chip esp32 -p $(COMPORT) -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 2MB 0x8000 partition_table/partition-table.bin 0x1000 bootloader/bootloader.bin 0x10000 mongoose-esp32-example.bin + clean: rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb mongoose mongoose_* mongoose.* build sdkconfig diff --git a/examples/esp32/README.md b/examples/esp32/README.md index 0daba123..b44dfc43 100644 --- a/examples/esp32/README.md +++ b/examples/esp32/README.md @@ -17,3 +17,12 @@ To build this application, follow these steps: make ``` +# Flashing built firmware + +If you have a real board to flash to, you can flash a built firmware. +First, make sure to have `esptool.py` script installed on your workstation. +Then, connect the board to your workstation and run the following command: + +```sh +make flash COMPORT=YOUR_USB_PORT +``` diff --git a/examples/esp32/main/main.c b/examples/esp32/main/main.c index 277841c1..5b9e4dc3 100644 --- a/examples/esp32/main/main.c +++ b/examples/esp32/main/main.c @@ -3,23 +3,41 @@ #include "mongoose.h" -#define WIFI_SSID "MY_WIFI_NETWORK" -#define WIFI_PASS "MY_WIFI_PASSWORD" -#define LISTENING_ADDR "http://0.0.0.0:80" +#define WIFI_SSID "VMDF554B9" // SET THIS! +#define WIFI_PASS "Mp7wjmamPafa" // SET THIS! -// Event handler for an accepted connection +#define SERVER_URL "http://0.0.0.0:80" +#define CLIENT_URL "http://info.cern.ch" + +// Event handler for an server (accepted) connection static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (ev == MG_EV_HTTP_MSG) { - mg_http_reply(c, 200, "", "Hello from ESP32!"); + mg_http_reply(c, 200, "", "Hello from ESP32!\n"); + } +} + +// Event handler for a client connection - fetch the first web page in history +// To enable TLS for HTTP, +// 1. Copy "ca.pem" file to the ESP32 flash FS +// 2. Add TLS init snippet for the connection, see examples/http-client +static void cb2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + if (ev == MG_EV_CONNECT) { + struct mg_str s = mg_url_host(CLIENT_URL); + mg_printf(c, "GET / HTTP/1.0\r\nHost: %.*s\r\n\r\n", (int) s.len, s.ptr); + } else if (ev == MG_EV_HTTP_MSG) { + struct mg_http_message *hm = ev_data; // Print HTTP response + LOG(LL_INFO, ("Fetched:\n%.*s", (int) hm->message.len, hm->message.ptr)); + c->is_closing = 1; } } // Called after we're connected to WiFi network -static void start_server(void) { +static void run_mongoose(void) { struct mg_mgr mgr; mg_log_set("3"); mg_mgr_init(&mgr); - mg_http_listen(&mgr, LISTENING_ADDR, cb, &mgr); + mg_http_listen(&mgr, SERVER_URL, cb, &mgr); // Listening server + mg_http_connect(&mgr, CLIENT_URL, cb2, &mgr); // Example client LOG(LL_INFO, ("Starting Mongoose web server v%s", MG_VERSION)); for (;;) mg_mgr_poll(&mgr, 1000); mg_mgr_free(&mgr); @@ -32,5 +50,5 @@ void app_main(void) { wifi_init(WIFI_SSID, WIFI_PASS); // Done connecting to WiFi, now start HTTP server - start_server(); + run_mongoose(); }