mirror of
https://github.com/cesanta/mongoose.git
synced 2024-11-27 20:59:00 +08:00
Add HTTP client code
This commit is contained in:
parent
749326c83a
commit
1716af6c72
@ -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
|
||||
|
@ -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
|
||||
```
|
||||
|
@ -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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user