diff --git a/bathroom-esp32/.vscode/settings.json b/bathroom-esp32/.vscode/settings.json index 989737b..b7fa1c1 100755 --- a/bathroom-esp32/.vscode/settings.json +++ b/bathroom-esp32/.vscode/settings.json @@ -12,7 +12,7 @@ "interface/ftdi/esp32_devkitj_v1.cfg", "target/esp32.cfg" ], - "idf.portWin": "COM8", + "idf.portWin": "COM7", "idf.pythonBinPathWin": "C:\\Projects\\Esp32\\espressif\\python_env\\idf5.3_py3.11_env\\Scripts\\python.exe", "idf.toolsPathWin": "C:\\Projects\\Esp32\\espressif", "idf.flashType": "UART" diff --git a/bathroom-esp32/.vscode/settings.json b/bathroom-esp32/.vscode/settings.json index 989737b..b7fa1c1 100755 --- a/bathroom-esp32/.vscode/settings.json +++ b/bathroom-esp32/.vscode/settings.json @@ -12,7 +12,7 @@ "interface/ftdi/esp32_devkitj_v1.cfg", "target/esp32.cfg" ], - "idf.portWin": "COM8", + "idf.portWin": "COM7", "idf.pythonBinPathWin": "C:\\Projects\\Esp32\\espressif\\python_env\\idf5.3_py3.11_env\\Scripts\\python.exe", "idf.toolsPathWin": "C:\\Projects\\Esp32\\espressif", "idf.flashType": "UART" diff --git a/bathroom-esp32/main/CMakeLists.txt b/bathroom-esp32/main/CMakeLists.txt index 5c66350..a715e11 100755 --- a/bathroom-esp32/main/CMakeLists.txt +++ b/bathroom-esp32/main/CMakeLists.txt @@ -1,2 +1,4 @@ idf_component_register(SRCS "main.cpp" - INCLUDE_DIRS ".") \ No newline at end of file + INCLUDE_DIRS "." + REQUIRES driver + REQUIRES esp_wifi) \ No newline at end of file diff --git a/bathroom-esp32/.vscode/settings.json b/bathroom-esp32/.vscode/settings.json index 989737b..b7fa1c1 100755 --- a/bathroom-esp32/.vscode/settings.json +++ b/bathroom-esp32/.vscode/settings.json @@ -12,7 +12,7 @@ "interface/ftdi/esp32_devkitj_v1.cfg", "target/esp32.cfg" ], - "idf.portWin": "COM8", + "idf.portWin": "COM7", "idf.pythonBinPathWin": "C:\\Projects\\Esp32\\espressif\\python_env\\idf5.3_py3.11_env\\Scripts\\python.exe", "idf.toolsPathWin": "C:\\Projects\\Esp32\\espressif", "idf.flashType": "UART" diff --git a/bathroom-esp32/main/CMakeLists.txt b/bathroom-esp32/main/CMakeLists.txt index 5c66350..a715e11 100755 --- a/bathroom-esp32/main/CMakeLists.txt +++ b/bathroom-esp32/main/CMakeLists.txt @@ -1,2 +1,4 @@ idf_component_register(SRCS "main.cpp" - INCLUDE_DIRS ".") \ No newline at end of file + INCLUDE_DIRS "." + REQUIRES driver + REQUIRES esp_wifi) \ No newline at end of file diff --git a/bathroom-esp32/main/main.cpp b/bathroom-esp32/main/main.cpp index 48e448d..c4a247a 100755 --- a/bathroom-esp32/main/main.cpp +++ b/bathroom-esp32/main/main.cpp @@ -23,6 +23,9 @@ #include "led_strip.h" #include "sdkconfig.h" #include "esp_random.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "esp_err.h" static const char *TAG = "example"; @@ -32,9 +35,9 @@ #define BLINK_GPIO ((gpio_num_t)16) #define CONFIG_BLINK_LED_STRIP 1 #define CONFIG_BLINK_LED_STRIP_BACKEND_RMT 1 -#define CONFIG_BLINK_PERIOD 3000 +#define CONFIG_BLINK_PERIOD 500 -static uint8_t s_led_state = 0; +static uint8_t s_led_state = 1; #ifdef CONFIG_BLINK_LED_STRIP @@ -106,8 +109,77 @@ #error "unsupported LED type" #endif +static EventGroupHandle_t s_wifi_event_group; + +static void configure_wifi(void) +{ + 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 wifi_config = { + .sta = { + .ssid = EXAMPLE_ESP_WIFI_SSID, + .password = EXAMPLE_ESP_WIFI_PASS, + /* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8). + * If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value + * to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to + * WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards. + */ + .threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD, + .sae_pwe_h2e = ESP_WIFI_SAE_MODE, + .sae_h2e_identifier = EXAMPLE_H2E_IDENTIFIER, + }, + }; + 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."); + + /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum + * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ + EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, + WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, + pdFALSE, + pdFALSE, + portMAX_DELAY); + + /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually + * happened. */ + if (bits & WIFI_CONNECTED_BIT) { + ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", + EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); + } else if (bits & WIFI_FAIL_BIT) { + ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", + EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); + } else { + ESP_LOGE(TAG, "UNEXPECTED EVENT"); + } +} + extern "C" void app_main(void) { + configure_wifi(); /* Configure the peripheral according to the LED type */ configure_led(); @@ -116,7 +188,7 @@ ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF"); blink_led(); /* Toggle the LED state */ - s_led_state = !s_led_state; + //s_led_state = !s_led_state; vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS); } }