|
1 | | -#include "freertos/FreeRTOS.h" |
2 | | -#include "freertos/task.h" |
3 | | -#include "esp_wifi.h" |
4 | | -#include "esp_event.h" |
5 | | -#include "esp_log.h" |
6 | | -#include <cstring> |
7 | | - |
8 | | -#include <wifi_provisioning/manager.h> |
9 | | -#include <wifi_provisioning/scheme_ble.h> |
| 1 | +#include <esp_log.h> |
| 2 | +#include <esp_wifi.h> |
| 3 | +#include <nvs_wifi_connect.h> |
10 | 4 |
|
11 | 5 | static const char* TAG = "wifi"; |
12 | | -static const int WIFI_CONNECTED_EVENT = BIT0; |
13 | | -static EventGroupHandle_t wifi_event_group; |
14 | | - |
15 | | -static void get_device_service_name(char *service_name, size_t max) |
16 | | -{ |
17 | | - uint8_t eth_mac[6]; |
18 | | - const char *ssid_prefix = "PROV_"; |
19 | | - esp_wifi_get_mac(WIFI_IF_STA, eth_mac); |
20 | | - snprintf(service_name, max, "%s%02X%02X%02X", |
21 | | - ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]); |
22 | | -} |
23 | | - |
24 | | -static void event_handler(void* arg, esp_event_base_t event_base, |
25 | | - int32_t event_id, void* event_data) |
26 | | -{ |
27 | | - if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { |
28 | | - esp_wifi_connect(); |
29 | | - ESP_LOGI(TAG, "Connecting to Wi-Fi..."); |
30 | | - |
31 | | - } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) { |
32 | | - esp_netif_create_ip6_linklocal((esp_netif_t *)arg); |
33 | | - |
34 | | - } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { |
35 | | - ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; |
36 | | - ESP_LOGI(TAG, "Connected with IP Address:" IPSTR, IP2STR(&event->ip_info.ip)); |
37 | | - xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_EVENT); |
38 | | - |
39 | | - } else if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) { |
40 | | - ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; |
41 | | - ESP_LOGI(TAG, "Connected with IPv6 Address:" IPV6STR, IPV62STR(event->ip6_info.ip)); |
42 | | - |
43 | | - } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { |
44 | | - ESP_LOGI(TAG, "Disconnected from Wi-Fi. Reconnecting..."); |
45 | | - esp_wifi_connect(); |
46 | 6 |
|
47 | | - } else if (event_base == WIFI_PROV_EVENT) { |
48 | | - |
49 | | - switch (event_id) { |
50 | | - |
51 | | - case WIFI_PROV_START: |
52 | | - ESP_LOGI(TAG, "Provisioning started"); |
53 | | - break; |
54 | | - |
55 | | - case WIFI_PROV_CRED_RECV: { |
56 | | - wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data; |
57 | | - ESP_LOGI(TAG, "Received Wi-Fi credentials" |
58 | | - "\n\tSSID : %s\n\tPassword : %s", |
59 | | - (const char *) wifi_sta_cfg->ssid, |
60 | | - (const char *) wifi_sta_cfg->password); |
61 | | - break; |
62 | | - } |
63 | | - |
64 | | - case WIFI_PROV_CRED_FAIL: { |
65 | | - wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data; |
66 | | - ESP_LOGE(TAG, "Provisioning failed!\n\tReason : %s" |
67 | | - "\n\tPlease reset to factory and retry provisioning", |
68 | | - (*reason == WIFI_PROV_STA_AUTH_ERROR) ? |
69 | | - "Wi-Fi station authentication failed" : "Wi-Fi access-point not found"); |
70 | | - break; |
71 | | - } |
72 | | - |
73 | | - case WIFI_PROV_CRED_SUCCESS: |
74 | | - ESP_LOGI(TAG, "Provisioning successful"); |
75 | | - break; |
76 | | - |
77 | | - case WIFI_PROV_END: |
78 | | - /* De-initialize manager once provisioning is finished */ |
79 | | - wifi_prov_mgr_deinit(); |
80 | | - break; |
81 | | - default: |
82 | | - break; |
83 | | - } |
| 7 | +void app_wifi_init(void) { |
| 8 | + if (nvs_wifi_connect() != ESP_OK) { |
| 9 | + nvs_wifi_connect_start_http_server(NVS_WIFI_CONNECT_MODE_RESTART_ESP32, nullptr); |
| 10 | + return; |
84 | 11 | } |
85 | | -} |
86 | | - |
87 | | -void app_wifi_init(void) |
88 | | -{ |
89 | | - /* Initialize TCP/IP */ |
90 | | - esp_netif_init(); |
91 | | - |
92 | | - /* Initialize the event loop */ |
93 | | - ESP_ERROR_CHECK(esp_event_loop_create_default()); |
94 | | - wifi_event_group = xEventGroupCreate(); |
95 | | - |
96 | | - /* Initialize Wi-Fi including netif with default config */ |
97 | | - esp_netif_t *wifi_netif = esp_netif_create_default_wifi_sta(); |
98 | | - |
99 | | - /* Register our event handler for Wi-Fi, IP and Provisioning related events */ |
100 | | - ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, wifi_netif)); |
101 | | - ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); |
102 | | - ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &event_handler, NULL)); |
103 | | - |
104 | | - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); |
105 | | - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); |
106 | | -} |
107 | | - |
108 | | -void wifi_init_sta() |
109 | | -{ |
110 | | - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
111 | | - ESP_ERROR_CHECK(esp_wifi_start()); |
112 | | -} |
113 | | - |
114 | | -esp_err_t app_wifi_start(TickType_t ticks_to_wait) |
115 | | -{ |
116 | | - wifi_prov_mgr_config_t config = {}; |
117 | | - config.scheme = wifi_prov_scheme_ble; |
118 | | - config.scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM; |
119 | | - |
120 | | - ESP_ERROR_CHECK(wifi_prov_mgr_init(config)); |
121 | | - |
122 | | - bool provisioned = false; |
123 | | - ESP_ERROR_CHECK(wifi_prov_mgr_is_provisioned(&provisioned)); |
124 | | - |
125 | | - if (!provisioned) { |
126 | | - ESP_LOGI(TAG, "Starting provisioning"); |
127 | | - esp_event_handler_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL); |
128 | | - char service_name[12]; |
129 | | - get_device_service_name(service_name, sizeof(service_name)); |
130 | | - |
131 | | - wifi_prov_security_t security = WIFI_PROV_SECURITY_0; |
132 | | - |
133 | | - uint8_t custom_service_uuid[] = { |
134 | | - /* This is a random uuid. This can be modified if you want to change the BLE uuid. */ |
135 | | - /* 12th and 13th bit will be replaced by internal bits. */ |
136 | | - 0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf, |
137 | | - 0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02, |
138 | | - }; |
139 | | - esp_err_t err = wifi_prov_scheme_ble_set_service_uuid(custom_service_uuid); |
140 | | - if (err != ESP_OK) { |
141 | | - ESP_LOGE(TAG, "wifi_prov_scheme_ble_set_service_uuid failed %d", err); |
142 | | - return err; |
143 | | - } |
144 | | - |
145 | | - ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, nullptr, service_name, nullptr)); |
146 | | - ESP_LOGI(TAG, "Provisioning Started. Name : %s", service_name); |
| 12 | + wifi_mode_t mode; |
| 13 | + esp_wifi_get_mode(&mode); |
| 14 | + if (mode == WIFI_MODE_STA) { |
| 15 | + ESP_LOGI(TAG, "Connected in STA mode"); |
| 16 | + } else if (mode == WIFI_MODE_AP) { |
| 17 | + ESP_LOGI(TAG, "Running in AP mode"); |
| 18 | + nvs_wifi_connect_start_http_server(NVS_WIFI_CONNECT_MODE_RESTART_ESP32, nullptr); |
147 | 19 | } else { |
148 | | - ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA"); |
149 | | - wifi_prov_mgr_deinit(); |
150 | | - wifi_init_sta(); |
| 20 | + ESP_LOGI(TAG, "WiFi not configured"); |
151 | 21 | } |
152 | | - /* Wait for Wi-Fi connection */ |
153 | | - xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_EVENT, false, true, ticks_to_wait); |
154 | | - return ESP_OK; |
155 | 22 | } |
| 23 | + |
0 commit comments