-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* MIT License | ||
* Copyright (c) 2024 William Vallet | ||
*/ | ||
|
||
#ifndef WIFI_H_ | ||
#define WIFI_H_ | ||
|
||
extern void wifi_init(void); | ||
|
||
#endif // WIFI_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
idf_component_register( | ||
SRCS | ||
main.c board.c led.c ir_decoder.c ir_decoder_nec.c | ||
command.c | ||
command.c wifi.c | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* MIT License | ||
* Copyright (c) 2024 William Vallet | ||
*/ | ||
|
||
#include "wifi.h" | ||
#include "esp_event.h" | ||
#include "esp_err.h" | ||
#include "esp_log.h" | ||
#include "esp_wifi.h" | ||
#include "wifi_provisioning/manager.h" | ||
#include "wifi_provisioning/scheme_softap.h" | ||
|
||
#define LOGGER_TAG "wifi_api" | ||
|
||
#define WIFI_PROV_SSID "UPnP Remote Provisioning" | ||
#define WIFI_PROV_PASS "password" | ||
#define WIFI_PROV_PROOF "abcd1234" | ||
#define WIFI_PROV_SECURITY WIFI_PROV_SECURITY_1 | ||
|
||
static void wifi_event_handler( | ||
void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) | ||
{ | ||
(void) arg; | ||
if (event_base != WIFI_PROV_EVENT) | ||
return; | ||
switch (event_id) | ||
{ | ||
case WIFI_PROV_INIT: | ||
ESP_LOGD(LOGGER_TAG, "Provisioning initialised"); | ||
break; | ||
case WIFI_PROV_START: | ||
ESP_LOGI(LOGGER_TAG, "Provisioning started"); | ||
break; | ||
case WIFI_PROV_CRED_RECV: | ||
const wifi_sta_config_t * const wifi_sta_cfg = | ||
(wifi_sta_config_t *) event_data; | ||
ESP_LOGI(LOGGER_TAG, | ||
"Credentials received for ssid='%s'", | ||
wifi_sta_cfg->ssid); | ||
break; | ||
case WIFI_PROV_CRED_FAIL: | ||
const wifi_prov_sta_fail_reason_t * const reason = | ||
(wifi_prov_sta_fail_reason_t *) event_data; | ||
ESP_LOGE(LOGGER_TAG, | ||
"Provisioning failed reason=%s", | ||
(*reason == WIFI_PROV_STA_AUTH_ERROR) ? | ||
"auth_failed" : "ap_not_found"); | ||
break; | ||
case WIFI_PROV_CRED_SUCCESS: | ||
ESP_LOGI(LOGGER_TAG, "Provisioning successful"); | ||
break; | ||
case WIFI_PROV_END: | ||
ESP_LOGI(LOGGER_TAG, "Provisioning ended"); | ||
wifi_prov_mgr_deinit(); | ||
break; | ||
case WIFI_PROV_DEINIT: | ||
ESP_LOGD(LOGGER_TAG, "Provisioning de-initialised"); | ||
break; | ||
default: | ||
// Nothing to do. | ||
break; | ||
} | ||
} | ||
|
||
static bool wifi_is_provisioned(void) | ||
{ | ||
bool provisioned = false; | ||
ESP_ERROR_CHECK(wifi_prov_mgr_is_provisioned(&provisioned)); | ||
return provisioned; | ||
} | ||
|
||
static void wifi_provisioning(void) | ||
{ | ||
const wifi_prov_mgr_config_t config = { | ||
.scheme = wifi_prov_scheme_softap, | ||
.scheme_event_handler = WIFI_PROV_EVENT_HANDLER_NONE | ||
}; | ||
ESP_ERROR_CHECK(esp_event_handler_instance_register( | ||
WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL)); | ||
ESP_ERROR_CHECK(wifi_prov_mgr_init(config)); | ||
ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning( | ||
WIFI_PROV_SECURITY, WIFI_PROV_PROOF, WIFI_PROV_SSID, WIFI_PROV_PASS)); | ||
} | ||
|
||
static void wifi_connect(void) | ||
{ | ||
// TODO | ||
} | ||
|
||
void wifi_init(void) | ||
{ | ||
// Initialise WiFi component. | ||
const wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT(); | ||
ESP_ERROR_CHECK(esp_netif_init()); | ||
esp_netif_create_default_wifi_ap(); | ||
ESP_ERROR_CHECK(esp_wifi_init(&wifi_cfg)); | ||
// Check credentials status. | ||
if (!wifi_is_provisioned()) | ||
{ | ||
ESP_LOGI(LOGGER_TAG, "Credentials not available"); | ||
// No credentials, start provisioning process. | ||
wifi_provisioning(); | ||
} | ||
else | ||
{ | ||
ESP_LOGI(LOGGER_TAG, "Credentials available"); | ||
// Credentials are present, try connection. | ||
wifi_connect(); | ||
} | ||
} |