mirror of
https://github.com/alexandrebobkov/ESP-Nodes.git
synced 2025-08-08 03:22:21 +00:00
mongoose
This commit is contained in:
4
ESP-Mongoose_UI/main/CMakeLists.txt
Normal file
4
ESP-Mongoose_UI/main/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
FILE(GLOB cfiles *.c)
|
||||
FILE(GLOB mongoose_files ../mongoose/*.c)
|
||||
idf_component_register(SRCS ${cfiles} ${mongoose_files}
|
||||
INCLUDE_DIRS "." "../mongoose")
|
15
ESP-Mongoose_UI/main/main.c
Normal file
15
ESP-Mongoose_UI/main/main.c
Normal file
@@ -0,0 +1,15 @@
|
||||
// SPDX-FileCopyrightText: 2024 Cesanta Software Limited
|
||||
// SPDX-License-Identifier: GPL-2.0-only or commercial
|
||||
// Generated by Mongoose Wizard, https://mongoose.ws/wizard/
|
||||
|
||||
#include "mongoose_glue.h"
|
||||
|
||||
#define WIFI_SSID "My_WiFi_Network"
|
||||
#define WIFI_PASS "My_WiFi_Password"
|
||||
|
||||
extern void wifi_init(const char *ssid, const char *pass);
|
||||
|
||||
void app_main() {
|
||||
wifi_init(WIFI_SSID, WIFI_PASS); // This blocks until connected
|
||||
run_mongoose();
|
||||
}
|
70
ESP-Mongoose_UI/main/wifi.c
Normal file
70
ESP-Mongoose_UI/main/wifi.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "esp_wifi.h"
|
||||
#include "mongoose.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
static void event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data) {
|
||||
static int retry_count = 0;
|
||||
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) {
|
||||
esp_wifi_connect();
|
||||
retry_count++;
|
||||
MG_INFO(("Connecting to the AP fail, attempt #%d", retry_count));
|
||||
} 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;
|
||||
MG_INFO(("Got IP ADDRESS: " IPSTR, IP2STR(&event->ip_info.ip)));
|
||||
retry_count = 0;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_init(const char *ssid, const char *pass) {
|
||||
// Initialize NVS
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
||||
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_sta();
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
||||
WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
||||
IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
|
||||
wifi_config_t wc = {};
|
||||
strncpy((char *) wc.sta.ssid, ssid, sizeof(wc.sta.ssid));
|
||||
strncpy((char *) wc.sta.password, pass, sizeof(wc.sta.password));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wc));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
MG_INFO(("Trying to connect to SSID:%s pass:%s", ssid, pass));
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
MG_INFO(("connected to ap SSID:%s pass:%s", ssid, pass));
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
MG_ERROR(("Failed to connect to SSID:%s, pass:%s", ssid, pass));
|
||||
} else {
|
||||
MG_ERROR(("UNEXPECTED EVENT"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user