2020-12-14 20:58:27 +08:00
|
|
|
// Copyright (c) 2020 Cesanta Software Limited
|
|
|
|
// All rights reserved
|
|
|
|
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
2020-12-25 00:52:12 +08:00
|
|
|
#define WIFI_SSID "VMDF554B9" // SET THIS!
|
|
|
|
#define WIFI_PASS "Mp7wjmamPafa" // SET THIS!
|
2020-12-14 20:58:27 +08:00
|
|
|
|
2020-12-25 00:52:12 +08:00
|
|
|
#define SERVER_URL "http://0.0.0.0:80"
|
|
|
|
#define CLIENT_URL "http://info.cern.ch"
|
|
|
|
|
|
|
|
// Event handler for an server (accepted) connection
|
2020-12-14 20:58:27 +08:00
|
|
|
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
|
|
|
if (ev == MG_EV_HTTP_MSG) {
|
2020-12-25 00:52:12 +08:00
|
|
|
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;
|
2020-12-14 20:58:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called after we're connected to WiFi network
|
2020-12-25 00:52:12 +08:00
|
|
|
static void run_mongoose(void) {
|
2020-12-14 20:58:27 +08:00
|
|
|
struct mg_mgr mgr;
|
|
|
|
mg_log_set("3");
|
|
|
|
mg_mgr_init(&mgr);
|
2020-12-25 00:52:12 +08:00
|
|
|
mg_http_listen(&mgr, SERVER_URL, cb, &mgr); // Listening server
|
|
|
|
mg_http_connect(&mgr, CLIENT_URL, cb2, &mgr); // Example client
|
2020-12-14 20:58:27 +08:00
|
|
|
LOG(LL_INFO, ("Starting Mongoose web server v%s", MG_VERSION));
|
|
|
|
for (;;) mg_mgr_poll(&mgr, 1000);
|
|
|
|
mg_mgr_free(&mgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_main(void) {
|
|
|
|
// Setup wifi. This function is implemented in wifi.c
|
|
|
|
// It blocks until connected to the configured WiFi network
|
|
|
|
void wifi_init(const char *ssid, const char *pass);
|
|
|
|
wifi_init(WIFI_SSID, WIFI_PASS);
|
|
|
|
|
|
|
|
// Done connecting to WiFi, now start HTTP server
|
2020-12-25 00:52:12 +08:00
|
|
|
run_mongoose();
|
2020-12-14 20:58:27 +08:00
|
|
|
}
|