Sat, 18 May 2019 23:01:16 +0200
Don't ignore SYSTEM_EVENT_AP_PROBEREQRECVED
0 | 1 | /** |
2 | * @file task_wifi.c | |
3 | * @brief WiFi task. Connects to a known Access Point. If we know more then | |
4 | * one AP, try to connect all of them until it succeeds (Not yet written). | |
5 | */ | |
6 | ||
7 | ||
8 | #include "config.h" | |
9 | ||
10 | ||
11 | static const char *TAG = "task_wifi"; | |
12 | ||
13 | ||
14 | SemaphoreHandle_t xSemaphoreWiFi = NULL; ///< Semaphore WiFi task. | |
15 | EventGroupHandle_t xEventGroupWifi; ///< Events WiFi task. | |
16 | uint16_t ap_num = MAX_AP_NUM; ///< Scan counter. | |
17 | wifi_ap_record_t *accessp_records; ///< [MAX_AP_NUM] records array with scan results. | |
18 | wifi_config_t *task_wifi_ConfigSTA = NULL; ///< Current STA configuration. | |
19 | WIFI_State *wifi_state = NULL; ///< Public state for other tasks. | |
20 | ||
21 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
22 | uint8_t _wifi_ssid[33]; ///< Current SSID |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
23 | bool _wifi_ScanAPs = false; ///< Scanning |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
24 | bool _wifi_ScanDone = false; ///< Scan ready |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
25 | uint16_t _wifi_Scanned = 0; ///< Total scanned APs. |
0 | 26 | |
27 | extern int Main_Screen; ///< Current Screen number. | |
28 | extern sButton Buttons[MAXBUTTONS]; ///< Buttons definitions. | |
29 | extern uint32_t TimeSpent; ///< Counter that is increased each second. | |
49
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
30 | extern bool System_TimeOk; |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
31 | extern time_t now; |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
32 | extern char strftime_buf[64]; |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
33 | extern struct tm timeinfo; |
0 | 34 | |
35 | const int TASK_WIFI_REQUEST_WIFI_SCAN = BIT0; ///< When set, means a client requested to scan wireless networks. | |
36 | const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1; ///< When set, means a client requested to disconnect from currently connected AP. | |
37 | const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2; ///< When set, means a client requested to connect to an access point. | |
38 | ||
39 | const int TASK_WIFI_HAS_IP = BIT3; ///< Indicate that we have an IP address | |
40 | const int TASK_WIFI_AP_STARTED = BIT4; ///< Indicate that the SoftAP is started | |
41 | const int TASK_WIFI_STA_FAILED = BIT5; ///< Indicate that we could not get a connection to AP as station. | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
42 | const int TASK_WIFI_STA_DISCONNECTED = BIT6; ///< Indicate that we are disconnected from an ap station. |
0 | 43 | const int TASK_WIFI_STA_CONNECTED = BIT7; ///< Indicate that we are connected to AP as station, flip of BIT6. |
44 | ||
45 | ||
46 | /** | |
47 | * @brief Local function, save station configuration. | |
48 | * @return Esp error code. | |
49 | */ | |
50 | esp_err_t SaveStaConfig(void); | |
51 | ||
52 | /** | |
53 | * @brief Local function, fetch last connected station configuration. | |
54 | * @return True if there was a last connection, false if there was not. | |
55 | */ | |
56 | bool FetchStaConfig(void); | |
57 | ||
58 | /** | |
59 | * @brief Local function, WiFi event handler. | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
60 | * @param ctx Context |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
61 | * @param event The event |
0 | 62 | * @return Esp error code. |
63 | */ | |
64 | esp_err_t task_wifi_EventHandler(void *ctx, system_event_t *event); | |
65 | ||
49
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
66 | /** |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
67 | * @brief local callback function. Is called when the timesync is valid |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
68 | * from a timeserver. Sets the global boolean System_TimeOk value. |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
69 | * @param tv is the received time. Not used. |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
70 | */ |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
71 | void time_sync_notification_cb(struct timeval *tv); |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
72 | |
0 | 73 | |
74 | /****************************************************************************/ | |
75 | ||
76 | ||
77 | ||
78 | esp_err_t SaveStaConfig(void) | |
79 | { | |
80 | int record; | |
81 | ||
82 | if (task_wifi_ConfigSTA && strlen((char *)task_wifi_ConfigSTA->sta.ssid)) { | |
83 | /* | |
84 | * Store in /spiffs/stations.conf if it's a new station. | |
85 | */ | |
86 | record = read_station(task_wifi_ConfigSTA->sta.ssid); | |
87 | if (record == -1) { | |
88 | add_station(task_wifi_ConfigSTA->sta.ssid, task_wifi_ConfigSTA->sta.password); | |
89 | } | |
90 | ||
91 | /* | |
92 | * Update main configuration if needed. | |
93 | */ | |
94 | if (strcmp(config.lastSSID, (char *)task_wifi_ConfigSTA->sta.ssid)) { | |
95 | sprintf(config.lastSSID, "%s", task_wifi_ConfigSTA->sta.ssid); | |
96 | write_config(); | |
97 | } | |
98 | ESP_LOGI(TAG, "SaveStaConfig %s, record %d", wifiStation.SSID, record); | |
99 | } | |
100 | ||
101 | return ESP_OK; | |
102 | } | |
103 | ||
104 | ||
105 | ||
106 | bool FetchStaConfig(void) | |
107 | { | |
108 | if (task_wifi_ConfigSTA == NULL) { | |
109 | task_wifi_ConfigSTA = (wifi_config_t*)malloc(sizeof(wifi_config_t)); | |
110 | } | |
111 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
112 | ||
113 | /* | |
114 | * Search last connected AP as station. | |
115 | */ | |
51
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
116 | if (strlen(config.lastSSID) && (read_station((uint8_t *)config.lastSSID) >= 0)) { |
0 | 117 | |
118 | /* ssid */ | |
119 | size_t sz = sizeof(task_wifi_ConfigSTA->sta.ssid); | |
120 | memcpy(task_wifi_ConfigSTA->sta.ssid, wifiStation.SSID, sz); | |
121 | ||
122 | /* password */ | |
123 | sz = sizeof(task_wifi_ConfigSTA->sta.password); | |
124 | memcpy(task_wifi_ConfigSTA->sta.password, wifiStation.Password, sz); | |
125 | ||
126 | ESP_LOGI(TAG, "FetchStaConfig: last connected to ssid: %s", task_wifi_ConfigSTA->sta.ssid); | |
127 | return task_wifi_ConfigSTA->sta.ssid[0] != '\0'; | |
128 | } | |
129 | ||
130 | ESP_LOGI(TAG, "FetchStaConfig: no last connection found"); | |
131 | return false; | |
132 | } | |
133 | ||
134 | ||
135 | ||
136 | esp_err_t task_wifi_EventHandler(void *ctx, system_event_t *event) | |
137 | { | |
138 | switch(event->event_id) { | |
139 | case SYSTEM_EVENT_SCAN_DONE: | |
140 | // Get the results so the memory used is freed. | |
141 | ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_num, accessp_records)); | |
142 | _wifi_Scanned = ap_num; | |
143 | _wifi_ScanDone = true; | |
144 | break; | |
145 | ||
146 | case SYSTEM_EVENT_STA_START: | |
147 | ESP_LOGD(TAG, "Event STA started"); | |
148 | // Set the configured hostname for the dhcp client. | |
149 | ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, config.hostname)); | |
150 | esp_wifi_connect(); | |
151 | break; | |
152 | ||
153 | // SYSTEM_EVENT_STA_STOP 3 | |
154 | case SYSTEM_EVENT_STA_CONNECTED: | |
34
5c92103c5e72
Version 0.2.10. Fixed spelling error in websocket component. Updated esp-idf to v4.0-dev-459-gba1ff1692 and adjusted several sources for changed headers. Finalized keeping the AP running at all times. Increased websocket server stack depth from 6000 to 7000.
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
155 | ESP_LOGI(TAG, "Event STA connected"); |
0 | 156 | wifi_ap_record_t ap_info; |
157 | esp_wifi_sta_get_ap_info(&ap_info); | |
158 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
159 | wifi_state->STA_connected = true; | |
160 | wifi_state->STA_rssi = ap_info.rssi; | |
161 | sprintf(wifi_state->STA_ssid, "%s", ap_info.ssid); | |
162 | xSemaphoreGive(xSemaphoreWiFi); | |
163 | } | |
164 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); | |
165 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); | |
166 | break; | |
167 | ||
168 | case SYSTEM_EVENT_STA_DISCONNECTED: | |
34
5c92103c5e72
Version 0.2.10. Fixed spelling error in websocket component. Updated esp-idf to v4.0-dev-459-gba1ff1692 and adjusted several sources for changed headers. Finalized keeping the AP running at all times. Increased websocket server stack depth from 6000 to 7000.
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
169 | ESP_LOGI(TAG, "Event STA disconnected"); |
0 | 170 | if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { |
171 | wifi_state->STA_connected = false; | |
22
90f22a101fc6
Boot now checks got IP status before installing the http and vnc servers.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
172 | wifi_state->STA_online = false; |
0 | 173 | wifi_state->STA_rssi = 0; |
174 | xSemaphoreGive(xSemaphoreWiFi); | |
175 | } | |
176 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); | |
177 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); | |
49
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
178 | sntp_stop(); |
0 | 179 | |
180 | /* | |
181 | * Meanwhile, try to reconnect. | |
182 | */ | |
183 | if (FetchStaConfig()) { | |
184 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
185 | } | |
186 | break; | |
187 | ||
188 | // SYSTEM_EVENT_STA_AUTHMODE_CHANGE 6 | |
189 | case SYSTEM_EVENT_STA_GOT_IP: | |
190 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP); | |
191 | tcpip_adapter_ip_info_t ip; | |
192 | memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t)); | |
193 | if (tcpip_adapter_get_ip_info(ESP_IF_WIFI_STA, &ip) == 0) { | |
194 | if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { | |
22
90f22a101fc6
Boot now checks got IP status before installing the http and vnc servers.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
195 | wifi_state->STA_online = true; |
0 | 196 | snprintf(wifi_state->STA_ip, 15, IPSTR, IP2STR(&ip.ip)); |
197 | snprintf(wifi_state->STA_nm, 15, IPSTR, IP2STR(&ip.netmask)); | |
198 | snprintf(wifi_state->STA_gw, 15, IPSTR, IP2STR(&ip.gw)); | |
199 | xSemaphoreGive(xSemaphoreWiFi); | |
200 | } | |
201 | } | |
49
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
202 | /* |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
203 | * There doesn't seem to be support for configuring NTP via DHCP so |
51
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
204 | * we need to hardcode the ntp servers. The preffered server can be |
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
205 | * set via the setup screen. It should be on your LAN, else leave it |
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
206 | * empty. And if you are on a different lan someday, there is no extra |
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
207 | * delay because the hostname will not be found. |
49
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
208 | */ |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
209 | sntp_stop(); |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
210 | if (strlen(config.ntp_server)) |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
211 | sntp_setservername(0, config.ntp_server); |
51
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
212 | sntp_setservername(1, "pool.ntp.org"); // Will get you servers nearby |
49
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
213 | sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED); |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
214 | sntp_set_time_sync_notification_cb(time_sync_notification_cb); |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
215 | sntp_init(); |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
216 | if (strlen(config.ntp_server)) |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
217 | ESP_LOGI(TAG, "NTP server %s", sntp_getservername(0)); |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
218 | ESP_LOGI(TAG, "NTP server %s", sntp_getservername(1)); |
0 | 219 | break; |
220 | ||
221 | case SYSTEM_EVENT_STA_LOST_IP: | |
222 | ESP_LOGI(TAG, "Lost IP address"); | |
223 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP); | |
224 | if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { | |
225 | wifi_state->STA_ip[0] = '\0'; | |
226 | wifi_state->STA_nm[0] = '\0'; | |
227 | wifi_state->STA_gw[0] = '\0'; | |
22
90f22a101fc6
Boot now checks got IP status before installing the http and vnc servers.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
228 | wifi_state->STA_online = false; |
0 | 229 | xSemaphoreGive(xSemaphoreWiFi); |
230 | } | |
49
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
231 | sntp_stop(); |
0 | 232 | break; |
233 | ||
234 | // SYSTEM_EVENT_STA_WPS_ER_SUCCESS 9 | |
235 | // SYSTEM_EVENT_STA_WPS_ER_FAILED 10 | |
236 | // SYSTEM_EVENT_STA_WPS_ER_TIMEOUT 11 | |
237 | // SYSTEM_EVENT_STA_WPS_ER_PIN 12 | |
238 | ||
239 | case SYSTEM_EVENT_AP_START: | |
240 | ESP_LOGD(TAG, "Event AP started"); | |
241 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_AP_STARTED); | |
242 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
243 | wifi_state->AP_active = true; | |
244 | wifi_state->AP_clients = 0; | |
245 | xSemaphoreGive(xSemaphoreWiFi); | |
246 | } | |
247 | break; | |
248 | ||
249 | case SYSTEM_EVENT_AP_STOP: | |
250 | ESP_LOGD(TAG, "Event AP stopped"); | |
251 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_AP_STARTED); | |
252 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
253 | wifi_state->AP_active = false; | |
254 | wifi_state->AP_clients = 0; | |
255 | xSemaphoreGive(xSemaphoreWiFi); | |
256 | } | |
257 | break; | |
258 | ||
259 | case SYSTEM_EVENT_AP_STACONNECTED: | |
260 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
261 | wifi_state->AP_clients++; | |
262 | xSemaphoreGive(xSemaphoreWiFi); | |
263 | } | |
264 | ESP_LOGI(TAG, "Event AP new client, %d connections", wifi_state->AP_clients); | |
265 | break; | |
266 | ||
267 | case SYSTEM_EVENT_AP_STADISCONNECTED: | |
268 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
269 | if (wifi_state->AP_clients > 0) | |
270 | wifi_state->AP_clients--; | |
271 | else | |
272 | wifi_state->AP_clients = 0; | |
273 | xSemaphoreGive(xSemaphoreWiFi); | |
274 | } | |
275 | ESP_LOGI(TAG, "Event AP client disconnect, %d connections", wifi_state->AP_clients); | |
276 | break; | |
277 | ||
52
4b5b28b0ad42
Don't ignore SYSTEM_EVENT_AP_PROBEREQRECVED
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
278 | case SYSTEM_EVENT_AP_PROBEREQRECVED: |
4b5b28b0ad42
Don't ignore SYSTEM_EVENT_AP_PROBEREQRECVED
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
279 | break; |
4b5b28b0ad42
Don't ignore SYSTEM_EVENT_AP_PROBEREQRECVED
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
280 | |
0 | 281 | // SYSTEM_EVENT_GOT_IP6 18 |
282 | // SYSTEM_EVENT_ETH_START 19 | |
283 | // SYSTEM_EVENT_ETH_STOP 20 | |
284 | // SYSTEM_EVENT_ETH_CONNECTED 21 | |
285 | // SYSTEM_EVENT_ETH_DISCONNECTED 22 | |
286 | // SYSTEM_EVENT_ETH_GOT_IP 23 | |
287 | ||
288 | default: | |
289 | printf("Unknown event %d\n", event->event_id); | |
290 | break; | |
291 | } | |
292 | return ESP_OK; | |
293 | } | |
294 | ||
295 | ||
296 | ||
49
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
297 | void time_sync_notification_cb(struct timeval *tv) |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
298 | { |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
299 | int rc = sntp_get_sync_status(); |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
300 | |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
301 | if (rc == SNTP_SYNC_STATUS_COMPLETED) { |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
302 | time(&now); |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
303 | localtime_r(&now, &timeinfo); |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
304 | System_TimeOk = true; |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
305 | strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
306 | ESP_LOGI(TAG, "System time is set: %s", strftime_buf); |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
307 | } else { |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
308 | ESP_LOGI(TAG, "Notification of unknown time synchronization event rc=%d", rc); |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
309 | } |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
310 | } |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
311 | |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
312 | |
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
313 | |
0 | 314 | void task_wifi( void * pvParameters ) |
315 | { | |
316 | esp_err_t ret; | |
317 | ||
34
5c92103c5e72
Version 0.2.10. Fixed spelling error in websocket component. Updated esp-idf to v4.0-dev-459-gba1ff1692 and adjusted several sources for changed headers. Finalized keeping the AP running at all times. Increased websocket server stack depth from 6000 to 7000.
Michiel Broek <mbroek@mbse.eu>
parents:
22
diff
changeset
|
318 | ESP_LOGI(TAG, "Starting WiFi"); |
0 | 319 | |
320 | /* | |
321 | * Initialize NVS | |
322 | */ | |
323 | ret = nvs_flash_init(); | |
324 | if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { | |
325 | ESP_ERROR_CHECK(nvs_flash_erase()); | |
326 | ret = nvs_flash_init(); | |
327 | } | |
328 | ESP_ERROR_CHECK(ret); | |
329 | ||
330 | /* event handler and event group for the wifi driver */ | |
331 | xEventGroupWifi = xEventGroupCreate(); | |
332 | /* initialize the tcp stack */ | |
333 | tcpip_adapter_init(); | |
334 | ESP_ERROR_CHECK(esp_event_loop_init(task_wifi_EventHandler, NULL)); | |
335 | ||
336 | /* | |
337 | * memory allocation of objects used by the task | |
338 | */ | |
339 | accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * MAX_AP_NUM); | |
340 | task_wifi_ConfigSTA = (wifi_config_t*)malloc(sizeof(wifi_config_t)); | |
341 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
342 | ||
343 | xSemaphoreWiFi = xSemaphoreCreateMutex(); | |
344 | wifi_state = malloc(sizeof(WIFI_State)); | |
345 | wifi_state->AP_clients = 0; | |
346 | wifi_state->AP_active = false; | |
347 | wifi_state->STA_connected = false; | |
22
90f22a101fc6
Boot now checks got IP status before installing the http and vnc servers.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
348 | wifi_state->STA_online = false; |
0 | 349 | wifi_state->STA_rssi = 0; |
350 | ||
351 | /* wifi scanner config */ | |
352 | wifi_scan_config_t scan_config = { | |
353 | .ssid = 0, | |
354 | .bssid = 0, | |
355 | .channel = 0, | |
356 | .show_hidden = false | |
357 | }; | |
358 | ||
359 | /* | |
360 | * start the softAP access point | |
361 | * stop DHCP server | |
362 | */ | |
363 | ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); | |
364 | ||
365 | /* | |
366 | * Assign a static IP to the AP network interface | |
367 | */ | |
368 | tcpip_adapter_ip_info_t info; | |
369 | memset(&info, 0x00, sizeof(info)); | |
370 | IP4_ADDR(&info.ip, 192, 168, 1, 1); | |
371 | IP4_ADDR(&info.gw, 192, 168, 1, 1); | |
372 | IP4_ADDR(&info.netmask, 255, 255, 255, 0); | |
373 | ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info)); | |
374 | ||
375 | /* start dhcp server */ | |
376 | ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP)); | |
377 | ESP_LOGI(TAG, "AP start dhcps ip: 192.168.1.1 nm: 255.255.255.0 gw: 192.168.1.1"); | |
378 | ||
379 | /* start dhcp client */ | |
380 | ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA)); | |
381 | ESP_LOGI(TAG, "Start DHCP client for STA interface."); | |
382 | ||
383 | /* | |
384 | * init wifi as station + access point | |
385 | */ | |
386 | wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); | |
387 | ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); | |
388 | ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); | |
389 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA)); | |
390 | ESP_ERROR_CHECK(esp_wifi_set_bandwidth(WIFI_IF_AP, config.ap_bandwidth)); | |
391 | ||
392 | // configure the softAP and start it */ | |
393 | wifi_config_t ap_config = { | |
394 | .ap = { | |
395 | .ssid_len = 0, | |
396 | .channel = config.ap_channel, | |
397 | .authmode = WIFI_AUTH_WPA2_PSK, | |
398 | .ssid_hidden = config.ap_ssid_hidden, | |
399 | .max_connection = AP_MAX_CONNECTIONS, | |
400 | .beacon_interval = 100, | |
401 | }, | |
402 | }; | |
403 | memcpy(ap_config.ap.ssid, config.ap_ssid , sizeof(config.ap_ssid)); | |
404 | memcpy(ap_config.ap.password, config.ap_pwd, sizeof(config.ap_pwd)); | |
405 | ret = esp_wifi_set_config(WIFI_IF_AP, &ap_config); | |
406 | if (ret != ESP_OK) { | |
407 | ESP_LOGE(TAG, "esp_wifi_set_config(WIFI_IF_AP, nnn) rc=%d", ret); | |
408 | } | |
409 | ESP_ERROR_CHECK(esp_wifi_start()); | |
410 | ||
411 | ESP_LOGI(TAG, "SoftAP start ssid: `%s' pwd: `%s' channel: %d, hidden: %s", | |
412 | ap_config.ap.ssid, ap_config.ap.password, ap_config.ap.channel, ap_config.ap.ssid_hidden ? "yes":"no"); | |
413 | ||
414 | /* | |
415 | * try to get access to previously saved wifi | |
416 | */ | |
417 | if (FetchStaConfig()) { | |
418 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
419 | } | |
420 | ||
421 | /* | |
422 | * Wait for access point to start | |
423 | */ | |
424 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); | |
425 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_AP_STARTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
426 | EventBits_t uxBits; | |
427 | ||
428 | for(;;) { | |
429 | ||
51
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
430 | // ESP_LOGI(TAG, "1 wait for %08x", TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT); |
0 | 431 | /* actions that can trigger: request a connection, a scan, or a disconnection */ |
432 | uxBits = xEventGroupWaitBits(xEventGroupWifi, | |
433 | TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT, | |
434 | pdFALSE, pdFALSE, portMAX_DELAY ); | |
51
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
435 | // ESP_LOGI(TAG, "1 waitbits %08x", uxBits); |
0 | 436 | |
437 | if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { | |
438 | /* | |
439 | * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory | |
440 | */ | |
49
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
441 | ESP_LOGI(TAG, "Request STA disconnect"); |
0 | 442 | sntp_stop(); |
443 | ESP_ERROR_CHECK(esp_wifi_disconnect()); | |
444 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
445 | ||
446 | /* | |
447 | * erase configuration | |
448 | */ | |
449 | if (task_wifi_ConfigSTA) { | |
450 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
451 | } | |
452 | config.lastSSID[0] = '\0'; | |
453 | write_config(); | |
454 | ||
455 | /* finally: release the scan request bit */ | |
456 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
457 | ESP_LOGI(TAG, "Request disconnect is finished."); | |
458 | ||
459 | } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) { | |
460 | ||
461 | //someone requested a connection! | |
462 | ESP_LOGI(TAG, "Request STA connect `%s'", task_wifi_ConfigSTA->sta.ssid); | |
463 | /* set the new config and connect - reset the failed bit first as it is later tested */ | |
464 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); | |
465 | ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, task_wifi_ConfigSTA)); | |
466 | ||
467 | esp_err_t wifierror = esp_wifi_connect(); | |
468 | if (wifierror != ESP_OK) { | |
469 | ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x", (int)wifierror); | |
470 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); | |
471 | } | |
472 | ||
473 | /* | |
474 | * 2 scenarios here: connection is successful and TASK_WIFI_HAS_IP will be posted | |
475 | * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code. | |
476 | * Note that the reason code is not exploited. For all intent and purposes a failure is a failure. | |
477 | */ | |
51
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
478 | // ESP_LOGI(TAG, "2 wait for %08x", TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED); |
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
479 | uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 10000 / portTICK_PERIOD_MS); |
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
480 | // ESP_LOGI(TAG, "2 waitbits %08x", uxBits); |
0 | 481 | |
482 | if (uxBits & (TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED)) { | |
483 | /* | |
484 | * only save the config if the connection was successful! | |
485 | */ | |
486 | if(uxBits & TASK_WIFI_HAS_IP) { | |
487 | /* save wifi config */ | |
488 | SaveStaConfig(); | |
489 | } else { | |
490 | ESP_LOGI(TAG, "Connection failed"); // TODO: Scan other SSID's for known networks. | |
491 | /* failed attempt to connect regardles of the reason */ | |
492 | ||
493 | /* otherwise: reset the config */ | |
494 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
495 | } | |
51
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
496 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
0 | 497 | } else { |
51
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
498 | /* hit 10 seconds timeout */ |
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
499 | ESP_LOGI(TAG, "Connection timeout"); |
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
500 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
501 | vTaskDelay(100 / portTICK_PERIOD_MS); |
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
502 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
0 | 503 | } |
504 | ||
505 | } else if (uxBits & TASK_WIFI_REQUEST_WIFI_SCAN) { | |
506 | ||
51
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
507 | ESP_LOGI(TAG, "Request WiFi scan"); |
0 | 508 | /* safe guard against overflow */ |
509 | ap_num = MAX_AP_NUM; | |
510 | ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); | |
511 | ||
512 | /* finally: release the scan request bit */ | |
513 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_WIFI_SCAN); | |
514 | } | |
515 | ||
516 | /* | |
517 | * Here we should check for reconnect actions. | |
518 | */ | |
51
0624a9a3ce75
Changed NTP default pool name. Added 10 seconds timeout after the wifi_connect function. In that timeout do a proper disconnect and connect request so that a AP that was down will be used again if it's up again.
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
519 | // ESP_LOGI(TAG, "check reconnect"); |
0 | 520 | |
521 | } /* for(;;) */ | |
522 | vTaskDelay( (TickType_t)10); | |
523 | } | |
524 | ||
525 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
526 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
527 | * @brief Array with AP security names |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
528 | */ |
0 | 529 | const char *apsec[] = { "Open", "WEP", "WPA", "WPA2", "WPA WPA2", "Enterprise" }; |
530 | ||
531 | ||
532 | /** | |
533 | * @brief Show an AP station as a button. The buttons are already defined. | |
534 | * @param idx The index position on the display, 1 to 7. | |
535 | * @param ap The AP information from the WiFi scan. | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
536 | * @param show How to display. 0 is blank, 1 is unknown, 2 is known, 3 is a connected AP. |
0 | 537 | */ |
538 | void Show_AP(uint8_t idx, wifi_ap_record_t ap, int show) | |
539 | { | |
540 | char tmp[33]; | |
541 | ||
542 | if ((idx > 7) || (idx < 1)) | |
543 | return; | |
544 | ||
545 | if (show == 0) { | |
546 | _bg = TFT_BLACK; | |
547 | } else { | |
548 | _bg = (color_t){ 63, 63, 64 }; | |
549 | } | |
550 | ||
551 | TFT_fillRect(Buttons[idx].x, Buttons[idx].y, Buttons[idx].w, Buttons[idx].h, _bg); | |
552 | if (show == 0) | |
553 | return; | |
554 | ||
555 | TFT_drawRect(Buttons[idx].x, Buttons[idx].y, Buttons[idx].w, Buttons[idx].h, TFT_NAVY); | |
556 | if (show == 3) { | |
557 | _fg = TFT_WHITE; | |
558 | } else if (show == 1) { | |
559 | _fg = TFT_YELLOW; | |
560 | } else { | |
561 | _fg = TFT_CYAN; | |
562 | } | |
563 | ||
564 | TFT_setFont(DEJAVU18_FONT, NULL); | |
565 | sprintf(tmp, "%s", ap.ssid); | |
566 | TFT_print(tmp, Buttons[idx].x + 5, Buttons[idx].y + 6); | |
567 | sprintf(tmp, "%d", ap.rssi); | |
568 | TFT_setFont(DEF_SMALL_FONT, NULL); | |
569 | TFT_print(tmp, Buttons[idx].x + Buttons[idx].w - (TFT_getStringWidth(tmp) + 5), Buttons[idx].y + 4); | |
570 | sprintf(tmp, "%s", apsec[ap.authmode]); | |
571 | TFT_print(tmp, Buttons[idx].x + Buttons[idx].w - (TFT_getStringWidth(tmp) + 5), Buttons[idx].y + 15); | |
572 | } | |
573 | ||
574 | ||
575 | ||
576 | bool WiFi_Init(void) | |
577 | { | |
578 | char pwd[65], pmpt[32]; | |
579 | ||
580 | switch (Main_Screen) { | |
581 | case MAIN_TOOLS_SETUP_WIFI: | |
582 | TopMessage("WiFi"); | |
583 | TFT_setFont(DEJAVU24_FONT, NULL); | |
584 | _fg = TFT_WHITE; | |
585 | TFT_print("Momentje ..", CENTER, CENTER); | |
586 | _wifi_ScanAPs = true; | |
587 | _wifi_ScanDone = false; | |
588 | Buttons_Add(260, 200, 60, 40, "Ok", 0); | |
589 | Buttons[0].dark = true; | |
590 | Buttons_Show(); | |
591 | // Now add the buttons we draw manually. | |
592 | Buttons_Add( 0, 30, 250, 30, "", 1); | |
593 | Buttons_Add( 0, 60, 250, 30, "", 2); | |
594 | Buttons_Add( 0, 90, 250, 30, "", 3); | |
595 | Buttons_Add( 0,120, 250, 30, "", 4); | |
596 | Buttons_Add( 0,150, 250, 30, "", 5); | |
597 | Buttons_Add( 0,180, 250, 30, "", 6); | |
598 | Buttons_Add( 0,210, 250, 30, "", 7); | |
599 | break; | |
600 | ||
601 | case MAIN_TOOLS_SETUP_WIFI_CUR: | |
602 | TopMessage("WiFi verbinding"); | |
603 | // Get extra information. | |
604 | wifi_ap_record_t ap_info; | |
605 | esp_wifi_sta_get_ap_info(&ap_info); | |
606 | ||
607 | wifi_config_t *wconfig = task_wifi_ConfigSTA /*task_wifi_GetWifiStaConfig( ) */; | |
608 | if (wconfig) { | |
609 | ||
610 | tcpip_adapter_ip_info_t ip_info; | |
611 | ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info)); | |
612 | char ip[IP4ADDR_STRLEN_MAX]; | |
613 | char gw[IP4ADDR_STRLEN_MAX]; | |
614 | char netmask[IP4ADDR_STRLEN_MAX]; | |
615 | strcpy(ip, ip4addr_ntoa(&ip_info.ip)); | |
616 | strcpy(netmask, ip4addr_ntoa(&ip_info.netmask)); | |
617 | strcpy(gw, ip4addr_ntoa(&ip_info.gw)); | |
618 | TFT_setFont(DEFAULT_FONT, NULL); | |
619 | _fg = TFT_WHITE; | |
620 | TFT_print("SSID", 155 - TFT_getStringWidth("SSID"), 40); | |
621 | TFT_print("Kanaal", 155 - TFT_getStringWidth("Kanaal"), 60); | |
622 | TFT_print("Rssi", 155 - TFT_getStringWidth("Rssi"), 80); | |
623 | TFT_print("Mode", 155 - TFT_getStringWidth("Mode"), 100); | |
624 | TFT_print("IP adres", 155 - TFT_getStringWidth("IP adres"), 120); | |
625 | TFT_print("Netmask", 155 - TFT_getStringWidth("Netmask"), 140); | |
626 | TFT_print("Gateway", 155 - TFT_getStringWidth("Gateway"), 160); | |
627 | _fg = TFT_YELLOW; | |
628 | TFT_print((char*)wconfig->sta.ssid, 165, 40); | |
629 | sprintf(pmpt, "%d", ap_info.primary); | |
630 | TFT_print(pmpt, 165, 60); | |
631 | sprintf(pmpt, "%d", ap_info.rssi); | |
632 | TFT_print(pmpt, 165, 80); | |
633 | sprintf(pmpt, "%s%s%s", ap_info.phy_11b ? "b":"", ap_info.phy_11g ? "g":"", ap_info.phy_11n ? "n":""); | |
634 | TFT_print(pmpt, 165, 100); | |
635 | TFT_print((char*)ip, 165, 120); | |
636 | TFT_print((char*)netmask, 165, 140); | |
637 | TFT_print((char*)gw, 165, 160); | |
638 | } | |
639 | Buttons_Add(130, 200, 60, 40, "Ok", 0); | |
640 | Buttons[0].dark = true; | |
641 | Buttons_Show(); | |
642 | break; | |
643 | ||
644 | case MAIN_TOOLS_SETUP_WIFI_CON: | |
645 | TopMessage("WiFi verbinden"); | |
646 | TFT_setFont(DEJAVU18_FONT, NULL); | |
647 | _fg = TFT_WHITE; | |
648 | TFT_print("SSID", 155 - TFT_getStringWidth("SSID"), 70); | |
649 | _fg = TFT_YELLOW; | |
650 | TFT_print((char*)_wifi_ssid, 165, 70); | |
651 | Buttons_Add( 0, 200, 100, 40, "Annuleer", 0); | |
652 | Buttons_Add(110, 200, 100, 40, "Vergeet", 1); | |
653 | Buttons_Add(220, 200, 100, 40, "Verbind", 2); | |
654 | Buttons_Show(); | |
655 | Buttons[0].dark = true; | |
656 | break; | |
657 | ||
658 | case MAIN_TOOLS_SETUP_WIFI_NEW: | |
659 | TopMessage("WiFi nieuw"); | |
660 | snprintf(pmpt, 32, "Password for %s", _wifi_ssid); | |
661 | pwd[0] = '\0'; | |
662 | EditTextMin(pmpt, pwd, 64, 8); | |
663 | /* | |
664 | * Disconnect first | |
665 | */ | |
666 | _bg = TFT_BLACK; | |
667 | TFT_fillScreen(_bg); | |
668 | TFT_setFont(DEJAVU24_FONT, NULL); | |
669 | _fg = TFT_WHITE; | |
670 | TFT_print("Momentje ..", CENTER, CENTER); | |
671 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
672 | vTaskDelay(100 / portTICK_PERIOD_MS); | |
673 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
674 | ||
675 | /* | |
676 | * Setup new connection | |
677 | */ | |
678 | if (strlen(pwd)) { | |
679 | wifi_config_t* config = task_wifi_ConfigSTA; | |
680 | memset(config, 0x00, sizeof(wifi_config_t)); | |
681 | memcpy(config->sta.ssid, _wifi_ssid, strlen((char*)_wifi_ssid)); | |
682 | memcpy(config->sta.password, pwd, strlen(pwd)); | |
683 | ESP_LOGI(TAG, "new AP %s %s", _wifi_ssid, pwd); | |
684 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
685 | } else { | |
686 | // TODO: what about WPS, it works but how to insert it in this app. | |
687 | return true; | |
688 | } | |
689 | // We must wait here for the result. | |
690 | break; | |
691 | ||
692 | default: | |
693 | break; | |
694 | } | |
695 | ||
696 | return false; | |
697 | } | |
698 | ||
699 | ||
700 | bool WiFi_Loop(void) | |
701 | { | |
702 | uint8_t idx; | |
703 | int Choice; | |
704 | static int AP[8]; | |
705 | wifi_ap_record_t ap; | |
706 | ||
707 | switch (Main_Screen) { | |
708 | case MAIN_TOOLS_SETUP_WIFI: | |
709 | if (_wifi_ScanAPs) { | |
710 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_WIFI_SCAN); | |
711 | _wifi_ScanAPs = false; | |
712 | TimeSpent = 0; | |
713 | } | |
714 | if (_wifi_ScanDone) { | |
715 | /* | |
716 | * Show scan results. There is room for 7 entries. If we have a connection, | |
717 | * the first one is that connection followed by available Access Points. | |
718 | * If there is no connection yet, there is only a iist of available Access | |
719 | * points. | |
720 | * The list is sorted by signal strength and is filled by the eventhandler. | |
721 | */ | |
722 | idx = 1; | |
723 | _wifi_ScanDone = false; | |
724 | ||
725 | if ((xEventGroupGetBits(xEventGroupWifi) & TASK_WIFI_STA_CONNECTED) == TASK_WIFI_STA_CONNECTED) { | |
726 | // We are connected. Search and display this one. | |
727 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
728 | for (int i = 0; i < _wifi_Scanned; i++) { | |
729 | ap = accessp_records[i]; | |
730 | if (strcmp(wifi_state->STA_ssid, (char *)ap.ssid) == 0) { | |
731 | AP[idx] = i; | |
732 | Show_AP(idx, ap, 3); | |
733 | idx++; | |
734 | break; | |
735 | } | |
736 | } | |
737 | xSemaphoreGive(xSemaphoreWiFi); | |
738 | } | |
739 | } | |
740 | ||
741 | // Display available Access Points. | |
742 | for (int i = 0; i < _wifi_Scanned; i++) { | |
743 | // The connected AP can be somewhere in this list, don't display it again. | |
744 | ap = accessp_records[i]; | |
745 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
746 | if (strcmp(wifi_state->STA_ssid, (char *)ap.ssid) == 0) { | |
747 | xSemaphoreGive(xSemaphoreWiFi); | |
748 | continue; // Skip connected AP, already on top of the list. | |
749 | } | |
750 | xSemaphoreGive(xSemaphoreWiFi); | |
751 | } | |
752 | // Check if we know this AP in the database. | |
753 | if ((read_station(ap.ssid) == -1)) { | |
754 | Show_AP(idx, ap, 2); // Unknown | |
755 | } else { | |
756 | if (wifiStation.hide) { | |
757 | continue; // Blacklisted. | |
758 | } | |
759 | Show_AP(idx, ap, 1); // We know this one. | |
760 | } | |
761 | AP[idx] = i; | |
762 | idx++; | |
763 | if (idx == 8) | |
764 | break; | |
765 | } | |
766 | if (idx < 7) { | |
767 | for (int i = idx; i < 8; i++) { | |
768 | Show_AP(i, ap, 0); | |
769 | AP[i] = 0; | |
770 | } | |
771 | } | |
772 | } | |
773 | ||
774 | Choice = Buttons_Scan(); | |
775 | if ((Choice >= 1) && (Choice <= 7)) { | |
776 | ap = accessp_records[AP[Choice]]; | |
777 | sprintf((char *)_wifi_ssid, "%s", ap.ssid); // Save selected SSID. | |
778 | } | |
779 | if ((Choice == 1) && ((xEventGroupGetBits(xEventGroupWifi) & TASK_WIFI_STA_CONNECTED) == TASK_WIFI_STA_CONNECTED)) { | |
780 | Main_Screen = MAIN_TOOLS_SETUP_WIFI_CUR; | |
781 | // Cancel a possible scan. | |
782 | ESP_ERROR_CHECK(esp_wifi_scan_stop()); | |
783 | } else if ((Choice >= 1) && (Choice <= 7)) { | |
784 | if ((read_station(_wifi_ssid) != -1)) { | |
785 | Main_Screen = MAIN_TOOLS_SETUP_WIFI_CON; | |
786 | } else { | |
787 | Main_Screen = MAIN_TOOLS_SETUP_WIFI_NEW; | |
788 | } | |
789 | ESP_ERROR_CHECK(esp_wifi_scan_stop()); | |
790 | } else if (Choice == 0) { | |
791 | Main_Screen = MAIN_TOOLS_SETUP; | |
792 | ESP_ERROR_CHECK(esp_wifi_scan_stop()); | |
49
4ec04c6f1551
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
Michiel Broek <mbroek@mbse.eu>
parents:
34
diff
changeset
|
793 | } else if (TimeSpent > 10) { |
0 | 794 | _wifi_ScanAPs = true; |
795 | } | |
796 | break; | |
797 | ||
798 | case MAIN_TOOLS_SETUP_WIFI_CUR: | |
799 | ||
800 | if (Buttons_Scan() == 0) { | |
801 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
802 | } | |
803 | break; | |
804 | ||
805 | case MAIN_TOOLS_SETUP_WIFI_CON: | |
806 | switch (Buttons_Scan()) { | |
807 | case 0: // Cancel choice | |
808 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
809 | break; | |
810 | ||
811 | case 1: // Forget connection | |
812 | remove_station(_wifi_ssid); | |
813 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
814 | break; | |
815 | ||
816 | case 2: // Connect | |
817 | _bg = TFT_BLACK; | |
818 | TFT_fillScreen(_bg); | |
819 | TFT_setFont(DEJAVU24_FONT, NULL); | |
820 | _fg = TFT_WHITE; | |
821 | TFT_print("Momentje ..", CENTER, CENTER); | |
822 | /* | |
823 | * Disconnect old connections and wait until it's gone. | |
824 | */ | |
825 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
826 | vTaskDelay(100 / portTICK_PERIOD_MS); | |
827 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
828 | /* | |
829 | * Setup new connection. | |
830 | */ | |
831 | if ((read_station(_wifi_ssid) != -1)) { | |
832 | wifi_config_t* config = task_wifi_ConfigSTA; | |
833 | memset(config, 0x00, sizeof(wifi_config_t)); | |
834 | memcpy(config->sta.ssid, wifiStation.SSID, strlen(wifiStation.SSID)); | |
835 | memcpy(config->sta.password, wifiStation.Password, strlen(wifiStation.Password)); | |
836 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
837 | vTaskDelay(1000 / portTICK_PERIOD_MS); | |
838 | } | |
839 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
840 | break; | |
841 | ||
842 | default: break; | |
843 | } | |
844 | break; | |
845 | ||
846 | case MAIN_TOOLS_SETUP_WIFI_NEW: | |
847 | // All work is already done, jump to the base screen. | |
848 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
849 | break; | |
850 | ||
851 | default: | |
852 | break; | |
853 | } | |
854 | ||
855 | return false; | |
856 | } | |
857 | ||
858 |