mirror of
https://github.com/cesanta/mongoose.git
synced 2025-06-25 13:50:38 +08:00
Add ESP8266 example
This commit is contained in:
parent
7d1ef2ffe8
commit
71a22adc06
@ -3,8 +3,8 @@
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
#define WIFI_SSID "VMDF554B9" // SET THIS!
|
||||
#define WIFI_PASS "Mp7wjmamPafa" // SET THIS!
|
||||
#define WIFI_SSID "WIFI_NETWORK" // SET THIS!
|
||||
#define WIFI_PASS "WIFI_PASSWORD" // SET THIS!
|
||||
|
||||
#define SERVER_URL "http://0.0.0.0:80"
|
||||
#define CLIENT_URL "http://info.cern.ch"
|
||||
@ -12,7 +12,7 @@
|
||||
// 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!\n");
|
||||
mg_http_reply(c, 200, "", "Hello from ESP!\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
// Code taken from the ESP32 IDF WiFi station Example
|
||||
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/task.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
@ -17,11 +17,12 @@
|
||||
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
/* The event group allows multiple bits for each event, but we only care about two events:
|
||||
/* The event group allows multiple bits for each event, but we only care about
|
||||
* two events:
|
||||
* - we are connected to the AP with an IP
|
||||
* - we failed to connect after the maximum amount of retries */
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
static int s_retry_num = 0;
|
||||
|
||||
@ -47,7 +48,7 @@ static void event_handler(void *arg, esp_event_base_t event_base,
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_init(const char *ssid, const char *pass, void (*fn)(void)) {
|
||||
void wifi_init(const char *ssid, const char *pass) {
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
||||
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
|
10
examples/esp8266/Makefile
Normal file
10
examples/esp8266/Makefile
Normal file
@ -0,0 +1,10 @@
|
||||
THISDIR = $(realpath $(CURDIR))
|
||||
ROOTDIR = $(realpath $(CURDIR)/../..)
|
||||
|
||||
all: example
|
||||
|
||||
example: src/main/main.c src/main/wifi.c Makefile
|
||||
docker run --rm -v $(ROOTDIR):$(ROOTDIR) -w $(THISDIR) docker.io/mdashnet/8266 make -C src defconfig app
|
||||
|
||||
clean:
|
||||
rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb mongoose mongoose_* mongoose.* src/build src/sdkconfig
|
9
examples/esp8266/src/Makefile
Normal file
9
examples/esp8266/src/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := mongoose-example
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
10
examples/esp8266/src/main/component.mk
Normal file
10
examples/esp8266/src/main/component.mk
Normal file
@ -0,0 +1,10 @@
|
||||
#
|
||||
# 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_INCLUDES += ../../../../..
|
54
examples/esp8266/src/main/main.c
Normal file
54
examples/esp8266/src/main/main.c
Normal file
@ -0,0 +1,54 @@
|
||||
// Copyright (c) 2020 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
#define WIFI_SSID "WIFI_NETWORK" // SET THIS!
|
||||
#define WIFI_PASS "WIFI_PASSWORD" // SET THIS!
|
||||
|
||||
#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 ESP!\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 run_mongoose(void) {
|
||||
struct mg_mgr mgr;
|
||||
mg_log_set("3");
|
||||
mg_mgr_init(&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);
|
||||
}
|
||||
|
||||
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
|
||||
run_mongoose();
|
||||
}
|
1
examples/esp8266/src/main/mongoose.c
Symbolic link
1
examples/esp8266/src/main/mongoose.c
Symbolic link
@ -0,0 +1 @@
|
||||
../../../../mongoose.c
|
86
examples/esp8266/src/main/wifi.c
Normal file
86
examples/esp8266/src/main/wifi.c
Normal file
@ -0,0 +1,86 @@
|
||||
#include <string.h>
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/task.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sys.h"
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
/* The event group allows multiple bits for each event, but we only care about
|
||||
* two events:
|
||||
* - we are connected to the AP with an IP
|
||||
* - we failed to connect after the maximum amount of retries */
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
static int s_retry_num = 0;
|
||||
|
||||
static void event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data) {
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_connect();
|
||||
} else if (event_base == WIFI_EVENT &&
|
||||
event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
if (s_retry_num < 3) {
|
||||
esp_wifi_connect();
|
||||
s_retry_num++;
|
||||
LOG(LL_DEBUG, ("retry to connect to the AP"));
|
||||
} else {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
}
|
||||
LOG(LL_ERROR, ("connect to the AP fail"));
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
|
||||
LOG(LL_INFO, ("got ip:%s", ip4addr_ntoa(&event->ip_info.ip)));
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_init(const char *ssid, const char *pass) {
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
tcpip_adapter_init();
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
|
||||
&event_handler, NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
|
||||
&event_handler, NULL));
|
||||
|
||||
wifi_config_t c = {};
|
||||
snprintf((char *) c.sta.ssid, sizeof(c.sta.ssid), "%s", ssid);
|
||||
snprintf((char *) c.sta.password, sizeof(c.sta.password), "%s", pass);
|
||||
if (pass != NULL && pass[0] != '\0') {
|
||||
c.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
|
||||
}
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &c));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
LOG(LL_INFO, ("connected to ap SSID:%s password:%s", ssid, pass));
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
LOG(LL_ERROR, ("Failed to connect to SSID:%s, password:%s", ssid, pass));
|
||||
} else {
|
||||
LOG(LL_ERROR, ("UNEXPECTED EVENT"));
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP,
|
||||
&event_handler));
|
||||
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID,
|
||||
&event_handler));
|
||||
vEventGroupDelete(s_wifi_event_group);
|
||||
}
|
@ -3810,7 +3810,8 @@ unsigned long mg_millis(void) {
|
||||
#elif MG_ARCH == MG_ARCH_ESP32
|
||||
return esp_timer_get_time() / 1000;
|
||||
#elif MG_ARCH == MG_ARCH_ESP8266
|
||||
return system_get_time() / 1000;
|
||||
// return system_get_time() / 1000;
|
||||
return xTaskGetTickCount() * portTICK_PERIOD_MS;
|
||||
#elif MG_ARCH == MG_ARCH_FREERTOS
|
||||
return xTaskGetTickCount() * portTICK_PERIOD_MS;
|
||||
#else
|
||||
|
23
mongoose.h
23
mongoose.h
@ -61,10 +61,10 @@
|
||||
#define MG_ARCH MG_ARCH_NXP_KINETIS
|
||||
#elif defined(PIC32)
|
||||
#define MG_ARCH MG_ARCH_PIC32
|
||||
#elif defined(ESP_PLATFORM)
|
||||
#define MG_ARCH MG_ARCH_ESP32
|
||||
#elif defined(ICACHE_FLASH) || defined(ICACHE_RAM_ATTR)
|
||||
#define MG_ARCH MG_ARCH_ESP8266
|
||||
#elif defined(ESP_PLATFORM)
|
||||
#define MG_ARCH MG_ARCH_ESP32
|
||||
#elif defined(TARGET_IS_TM4C129_RA0) || defined(TARGET_IS_TM4C129_RA1) || \
|
||||
defined(TARGET_IS_TM4C129_RA2)
|
||||
#define MG_ARCH MG_ARCH_TM4C129
|
||||
@ -128,26 +128,11 @@
|
||||
#if MG_ARCH == MG_ARCH_ESP8266
|
||||
|
||||
#include <dirent.h>
|
||||
#include <esp_system.h>
|
||||
#include <netdb.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/time.h>
|
||||
#include <user_interface.h>
|
||||
|
||||
#define MG_DIRSEP '/'
|
||||
#ifndef PATH_MAX
|
||||
#define PATH_MAX 128
|
||||
#endif
|
||||
#define MG_ENABLE_LWIP 1
|
||||
#define LWIP_TIMEVAL_PRIVATE 0 // struct timeval is defined in sys/time.h
|
||||
|
||||
#if LWIP_SOCKET
|
||||
#include <lwip/sockets.h>
|
||||
#else
|
||||
#undef LWIP_SOCKET
|
||||
#define LWIP_SOCKET 1
|
||||
#include <lwip/sockets.h> // Source sockaddr_in anyway
|
||||
#undef LWIP_SOCKET
|
||||
#define LWIP_SOCKET 0
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -42,10 +42,10 @@
|
||||
#define MG_ARCH MG_ARCH_NXP_KINETIS
|
||||
#elif defined(PIC32)
|
||||
#define MG_ARCH MG_ARCH_PIC32
|
||||
#elif defined(ESP_PLATFORM)
|
||||
#define MG_ARCH MG_ARCH_ESP32
|
||||
#elif defined(ICACHE_FLASH) || defined(ICACHE_RAM_ATTR)
|
||||
#define MG_ARCH MG_ARCH_ESP8266
|
||||
#elif defined(ESP_PLATFORM)
|
||||
#define MG_ARCH MG_ARCH_ESP32
|
||||
#elif defined(TARGET_IS_TM4C129_RA0) || defined(TARGET_IS_TM4C129_RA1) || \
|
||||
defined(TARGET_IS_TM4C129_RA2)
|
||||
#define MG_ARCH MG_ARCH_TM4C129
|
||||
|
@ -3,25 +3,10 @@
|
||||
#if MG_ARCH == MG_ARCH_ESP8266
|
||||
|
||||
#include <dirent.h>
|
||||
#include <esp_system.h>
|
||||
#include <netdb.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/time.h>
|
||||
#include <user_interface.h>
|
||||
|
||||
#define MG_DIRSEP '/'
|
||||
#ifndef PATH_MAX
|
||||
#define PATH_MAX 128
|
||||
#endif
|
||||
#define MG_ENABLE_LWIP 1
|
||||
#define LWIP_TIMEVAL_PRIVATE 0 // struct timeval is defined in sys/time.h
|
||||
|
||||
#if LWIP_SOCKET
|
||||
#include <lwip/sockets.h>
|
||||
#else
|
||||
#undef LWIP_SOCKET
|
||||
#define LWIP_SOCKET 1
|
||||
#include <lwip/sockets.h> // Source sockaddr_in anyway
|
||||
#undef LWIP_SOCKET
|
||||
#define LWIP_SOCKET 0
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@ -269,7 +269,8 @@ unsigned long mg_millis(void) {
|
||||
#elif MG_ARCH == MG_ARCH_ESP32
|
||||
return esp_timer_get_time() / 1000;
|
||||
#elif MG_ARCH == MG_ARCH_ESP8266
|
||||
return system_get_time() / 1000;
|
||||
// return system_get_time() / 1000;
|
||||
return xTaskGetTickCount() * portTICK_PERIOD_MS;
|
||||
#elif MG_ARCH == MG_ARCH_FREERTOS
|
||||
return xTaskGetTickCount() * portTICK_PERIOD_MS;
|
||||
#else
|
||||
|
Loading…
Reference in New Issue
Block a user