From cf9a549cf2ff522cb46b73f932d64887b6673be6 Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Sat, 28 Oct 2023 00:58:33 +0100 Subject: [PATCH] Added Wifi Seems to be working. It's 90% the same code as the examples --- components/pms5003/pms5003.c | 4 ++ components/wifi/CMakeLists.txt | 4 ++ components/wifi/Kconfig.projbuild | 14 +++++++ components/wifi/wifi.c | 61 +++++++++++++++++++++++++++++++ components/wifi/wifi.h | 3 ++ main/main.c | 3 ++ 6 files changed, 89 insertions(+) create mode 100644 components/wifi/CMakeLists.txt create mode 100644 components/wifi/Kconfig.projbuild create mode 100644 components/wifi/wifi.c create mode 100644 components/wifi/wifi.h diff --git a/components/pms5003/pms5003.c b/components/pms5003/pms5003.c index e69de29..fc346b7 100644 --- a/components/pms5003/pms5003.c +++ b/components/pms5003/pms5003.c @@ -0,0 +1,4 @@ +int test() { + int i = 5; + return i; +} diff --git a/components/wifi/CMakeLists.txt b/components/wifi/CMakeLists.txt new file mode 100644 index 0000000..03dbf77 --- /dev/null +++ b/components/wifi/CMakeLists.txt @@ -0,0 +1,4 @@ +idf_component_register(SRCS "wifi.c" + INCLUDE_DIRS "." + REQUIRES driver esp_wifi +) diff --git a/components/wifi/Kconfig.projbuild b/components/wifi/Kconfig.projbuild new file mode 100644 index 0000000..13e2fbc --- /dev/null +++ b/components/wifi/Kconfig.projbuild @@ -0,0 +1,14 @@ +menu "WiFi" + config WIFI_SSID + string "WiFi SSID" + default "VM8094728" + help + SSID (network name) for the example to connect to. + + config WIFI_PASSWORD + string "WiFi Password" + default "tx3YrxvzMrbr" + help + WiFi password (WPA or WPA2) for the example to use. + +endmenu diff --git a/components/wifi/wifi.c b/components/wifi/wifi.c new file mode 100644 index 0000000..f113a5e --- /dev/null +++ b/components/wifi/wifi.c @@ -0,0 +1,61 @@ +#include + +#include "esp_system.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "esp_log.h" + +static const char* TAG = "WIFI"; + +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) { + esp_wifi_connect(); + ESP_LOGI(TAG,"connect to the AP fail - trying to reconnect"); + } + 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; + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); + } +} + +void start_wifi() { + + 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 wifi_config = { + .sta = { + .ssid = CONFIG_WIFI_SSID, + .password = CONFIG_WIFI_PASSWORD, + .failure_retry_cnt = 20, + // TODO Figure out what I actually removed here + }, + }; + 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() ); + ESP_LOGI(TAG, "wifi_init_sta finished."); +} diff --git a/components/wifi/wifi.h b/components/wifi/wifi.h new file mode 100644 index 0000000..d4aab6b --- /dev/null +++ b/components/wifi/wifi.h @@ -0,0 +1,3 @@ +#pragma once + +void start_wifi(); diff --git a/main/main.c b/main/main.c index cd6e871..a4c6fc9 100644 --- a/main/main.c +++ b/main/main.c @@ -5,6 +5,7 @@ */ #include "dht22.h" +#include "wifi.h" #include "esp_log.h" #include "esp_wifi.h" #include "freertos/FreeRTOS.h" @@ -91,4 +92,6 @@ app_main(void) &pms5003_handle); #endif /* CONFIG_PMS5003_ENABLED */ + + start_wifi(); }