diff options
| author | Edvin <[email protected]> | 2025-10-14 17:35:18 +0200 |
|---|---|---|
| committer | Edvin <[email protected]> | 2025-10-14 17:35:18 +0200 |
| commit | 674697a333c67d79a77ab12b3923922d77e882b5 (patch) | |
| tree | cf08bd17bcfb6f39716acb7a0b1fb47b6cba8ebd | |
Initial commit
| -rw-r--r-- | CMakeLists.txt | 4 | ||||
| -rw-r--r-- | EZWIFI.c | 107 | ||||
| -rw-r--r-- | include/EZWIFI.h | 39 |
3 files changed, 150 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..df032e5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,4 @@ +idf_component_register(SRCS "EZWIFI.c" + INCLUDE_DIRS "include" + REQUIRES "esp_wifi" + REQUIRES "nvs_flash") diff --git a/EZWIFI.c b/EZWIFI.c new file mode 100644 index 0000000..388b6a6 --- /dev/null +++ b/EZWIFI.c @@ -0,0 +1,107 @@ +#include "EZWIFI.h" + +void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) +{ + +} + +void setup_station(void) +{ + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + cfg.csi_enable = 1; + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL)); + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, NULL)); + + esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); + + wifi_config_t wifi_config = { + .sta = { + .ssid = SSID, + .password = PASS, + .scan_method = WIFI_ALL_CHANNEL_SCAN, + .sort_method = WIFI_CONNECT_AP_BY_SIGNAL, + .threshold.rssi = -127, + .threshold.authmode = WIFI_AUTH_WPA2_PSK, + .threshold.rssi_5g_adjustment = 0, + }, + }; + + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); +} + +void setup_ap(void) +{ + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); +} + +void mesh_scan_done_handler(int num) +{ +} + +void mesh_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) +{ +} + +void ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) +{ + ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data; + ESP_LOGI(TAG, "<IP_EVENT_STA_GOT_IP>IP:" IPSTR, IP2STR(&event->ip_info.ip)); +} + +void ezmesh_init(void) +{ + // Init networking, storage and event loop + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + ESP_ERROR_CHECK(esp_netif_create_default_wifi_mesh_netifs(&netif_sta, NULL)); + // Init WiFi + wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT(); + + // TODO: Enable CSI HERE! + + ESP_ERROR_CHECK(esp_wifi_init(&config)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH)); + ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); + + // Start WiFi + ESP_ERROR_CHECK(esp_wifi_start()); + + // Init mesh + ESP_ERROR_CHECK(esp_mesh_init()); + ESP_ERROR_CHECK(esp_event_handler_register(MESH_EVENT, ESP_EVENT_ANY_ID, &mesh_event_handler, NULL)); + + // Mesh config + mesh_cfg_t cfg = MESH_INIT_CONFIG_DEFAULT(); + + // Mesh ID + memcpy((uint8_t *) &cfg.mesh_id, MESH_ID, 6); + + // Router + cfg.channel = MESH_CHANNEL; + cfg.router.ssid_len = strlen(MESH_ROUTER_SSID); + memcpy((uint8_t *) &cfg.router.ssid, MESH_ROUTER_SSID, cfg.router.ssid_len); + memcpy((uint8_t *) &cfg.router.password, MESH_ROUTER_PASS, strlen(MESH_ROUTER_PASS)); + + // Mesh softAP + ESP_ERROR_CHECK(esp_mesh_set_ap_authmode(MESH_AP_AUTHMODE)); + cfg.mesh_ap.max_connection = MESH_AP_CONNECTIONS; + cfg.mesh_ap.nonmesh_max_connection = MESH_NON_MESH_AP_CONNECTIONS; + memcpy((uint8_t *) &cfg.mesh_ap.password, MESH_AP_PASS, strlen(MESH_AP_PASS)); + ESP_ERROR_CHECK(esp_mesh_set_config(&cfg)); + + ESP_LOGI(TAG, "<Config>disable IE crypto"); + + ESP_ERROR_CHECK(esp_mesh_start()); + ESP_LOGI(TAG, "Mesh starts successfully, heap:%" PRId32, esp_get_free_heap_size()); +} diff --git a/include/EZWIFI.h b/include/EZWIFI.h new file mode 100644 index 0000000..6470300 --- /dev/null +++ b/include/EZWIFI.h @@ -0,0 +1,39 @@ +#include <string.h> +#include "esp_wifi.h" +#include "esp_mac.h" +#include "esp_event.h" +#include "esp_log.h" +#include "esp_mesh.h" +#include "nvs_flash.h" + +#define SSID "CDIO_MESH" +#define PASS "CDIO_PASS" + +#define MESH_AP_AUTHMODE WIFI_AUTH_WPA2_PSK +#define MESH_AP_CONNECTIONS 5 +#define MESH_NON_MESH_AP_CONNECTIONS 2 +#define MESH_AP_PASS "CDIO_PASS" + +#define MESH_CHANNEL 0 +#define MESH_ROUTER_SSID "CDIO_MESH" +#define MESH_ROUTER_PASS "CDIO_PASS" + +static const char *TAG = "wifi_main"; +static const uint8_t MESH_ID[6] = { 0x77, 0x77, 0x77, 0x77, 0x77, 0x77}; +static mesh_addr_t mesh_parent_addr; +static int mesh_layer = -1; +static esp_netif_t *netif_sta = NULL; + +void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data); + +void setup_station(void); + +void setup_ap(void); + +void mesh_scan_done_handler(int num); + +void mesh_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); + +void ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); + +void ezmesh_init(void); |
