Sat, 18 May 2019 22:58:28 +0200
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.
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 | ||
278 | // SYSTEM_EVENT_AP_PROBEREQRECVED 17 | |
279 | // SYSTEM_EVENT_GOT_IP6 18 | |
280 | // SYSTEM_EVENT_ETH_START 19 | |
281 | // SYSTEM_EVENT_ETH_STOP 20 | |
282 | // SYSTEM_EVENT_ETH_CONNECTED 21 | |
283 | // SYSTEM_EVENT_ETH_DISCONNECTED 22 | |
284 | // SYSTEM_EVENT_ETH_GOT_IP 23 | |
285 | ||
286 | default: | |
287 | printf("Unknown event %d\n", event->event_id); | |
288 | break; | |
289 | } | |
290 | return ESP_OK; | |
291 | } | |
292 | ||
293 | ||
294 | ||
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
|
295 | 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
|
296 | { |
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 | 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
|
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 | 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
|
300 | 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
|
301 | 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
|
302 | 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
|
303 | 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
|
304 | 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
|
305 | } 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
|
306 | 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
|
307 | } |
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 | } |
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 | |
0 | 312 | void task_wifi( void * pvParameters ) |
313 | { | |
314 | esp_err_t ret; | |
315 | ||
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
|
316 | ESP_LOGI(TAG, "Starting WiFi"); |
0 | 317 | |
318 | /* | |
319 | * Initialize NVS | |
320 | */ | |
321 | ret = nvs_flash_init(); | |
322 | if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { | |
323 | ESP_ERROR_CHECK(nvs_flash_erase()); | |
324 | ret = nvs_flash_init(); | |
325 | } | |
326 | ESP_ERROR_CHECK(ret); | |
327 | ||
328 | /* event handler and event group for the wifi driver */ | |
329 | xEventGroupWifi = xEventGroupCreate(); | |
330 | /* initialize the tcp stack */ | |
331 | tcpip_adapter_init(); | |
332 | ESP_ERROR_CHECK(esp_event_loop_init(task_wifi_EventHandler, NULL)); | |
333 | ||
334 | /* | |
335 | * memory allocation of objects used by the task | |
336 | */ | |
337 | accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * MAX_AP_NUM); | |
338 | task_wifi_ConfigSTA = (wifi_config_t*)malloc(sizeof(wifi_config_t)); | |
339 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
340 | ||
341 | xSemaphoreWiFi = xSemaphoreCreateMutex(); | |
342 | wifi_state = malloc(sizeof(WIFI_State)); | |
343 | wifi_state->AP_clients = 0; | |
344 | wifi_state->AP_active = false; | |
345 | 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
|
346 | wifi_state->STA_online = false; |
0 | 347 | wifi_state->STA_rssi = 0; |
348 | ||
349 | /* wifi scanner config */ | |
350 | wifi_scan_config_t scan_config = { | |
351 | .ssid = 0, | |
352 | .bssid = 0, | |
353 | .channel = 0, | |
354 | .show_hidden = false | |
355 | }; | |
356 | ||
357 | /* | |
358 | * start the softAP access point | |
359 | * stop DHCP server | |
360 | */ | |
361 | ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); | |
362 | ||
363 | /* | |
364 | * Assign a static IP to the AP network interface | |
365 | */ | |
366 | tcpip_adapter_ip_info_t info; | |
367 | memset(&info, 0x00, sizeof(info)); | |
368 | IP4_ADDR(&info.ip, 192, 168, 1, 1); | |
369 | IP4_ADDR(&info.gw, 192, 168, 1, 1); | |
370 | IP4_ADDR(&info.netmask, 255, 255, 255, 0); | |
371 | ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info)); | |
372 | ||
373 | /* start dhcp server */ | |
374 | ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP)); | |
375 | ESP_LOGI(TAG, "AP start dhcps ip: 192.168.1.1 nm: 255.255.255.0 gw: 192.168.1.1"); | |
376 | ||
377 | /* start dhcp client */ | |
378 | ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA)); | |
379 | ESP_LOGI(TAG, "Start DHCP client for STA interface."); | |
380 | ||
381 | /* | |
382 | * init wifi as station + access point | |
383 | */ | |
384 | wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); | |
385 | ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); | |
386 | ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); | |
387 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA)); | |
388 | ESP_ERROR_CHECK(esp_wifi_set_bandwidth(WIFI_IF_AP, config.ap_bandwidth)); | |
389 | ||
390 | // configure the softAP and start it */ | |
391 | wifi_config_t ap_config = { | |
392 | .ap = { | |
393 | .ssid_len = 0, | |
394 | .channel = config.ap_channel, | |
395 | .authmode = WIFI_AUTH_WPA2_PSK, | |
396 | .ssid_hidden = config.ap_ssid_hidden, | |
397 | .max_connection = AP_MAX_CONNECTIONS, | |
398 | .beacon_interval = 100, | |
399 | }, | |
400 | }; | |
401 | memcpy(ap_config.ap.ssid, config.ap_ssid , sizeof(config.ap_ssid)); | |
402 | memcpy(ap_config.ap.password, config.ap_pwd, sizeof(config.ap_pwd)); | |
403 | ret = esp_wifi_set_config(WIFI_IF_AP, &ap_config); | |
404 | if (ret != ESP_OK) { | |
405 | ESP_LOGE(TAG, "esp_wifi_set_config(WIFI_IF_AP, nnn) rc=%d", ret); | |
406 | } | |
407 | ESP_ERROR_CHECK(esp_wifi_start()); | |
408 | ||
409 | ESP_LOGI(TAG, "SoftAP start ssid: `%s' pwd: `%s' channel: %d, hidden: %s", | |
410 | ap_config.ap.ssid, ap_config.ap.password, ap_config.ap.channel, ap_config.ap.ssid_hidden ? "yes":"no"); | |
411 | ||
412 | /* | |
413 | * try to get access to previously saved wifi | |
414 | */ | |
415 | if (FetchStaConfig()) { | |
416 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
417 | } | |
418 | ||
419 | /* | |
420 | * Wait for access point to start | |
421 | */ | |
422 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); | |
423 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_AP_STARTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
424 | EventBits_t uxBits; | |
425 | ||
426 | for(;;) { | |
427 | ||
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
|
428 | // ESP_LOGI(TAG, "1 wait for %08x", TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT); |
0 | 429 | /* actions that can trigger: request a connection, a scan, or a disconnection */ |
430 | uxBits = xEventGroupWaitBits(xEventGroupWifi, | |
431 | TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT, | |
432 | 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
|
433 | // ESP_LOGI(TAG, "1 waitbits %08x", uxBits); |
0 | 434 | |
435 | if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { | |
436 | /* | |
437 | * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory | |
438 | */ | |
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
|
439 | ESP_LOGI(TAG, "Request STA disconnect"); |
0 | 440 | sntp_stop(); |
441 | ESP_ERROR_CHECK(esp_wifi_disconnect()); | |
442 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
443 | ||
444 | /* | |
445 | * erase configuration | |
446 | */ | |
447 | if (task_wifi_ConfigSTA) { | |
448 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
449 | } | |
450 | config.lastSSID[0] = '\0'; | |
451 | write_config(); | |
452 | ||
453 | /* finally: release the scan request bit */ | |
454 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
455 | ESP_LOGI(TAG, "Request disconnect is finished."); | |
456 | ||
457 | } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) { | |
458 | ||
459 | //someone requested a connection! | |
460 | ESP_LOGI(TAG, "Request STA connect `%s'", task_wifi_ConfigSTA->sta.ssid); | |
461 | /* set the new config and connect - reset the failed bit first as it is later tested */ | |
462 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); | |
463 | ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, task_wifi_ConfigSTA)); | |
464 | ||
465 | esp_err_t wifierror = esp_wifi_connect(); | |
466 | if (wifierror != ESP_OK) { | |
467 | ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x", (int)wifierror); | |
468 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); | |
469 | } | |
470 | ||
471 | /* | |
472 | * 2 scenarios here: connection is successful and TASK_WIFI_HAS_IP will be posted | |
473 | * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code. | |
474 | * Note that the reason code is not exploited. For all intent and purposes a failure is a failure. | |
475 | */ | |
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
|
476 | // 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
|
477 | 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
|
478 | // ESP_LOGI(TAG, "2 waitbits %08x", uxBits); |
0 | 479 | |
480 | if (uxBits & (TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED)) { | |
481 | /* | |
482 | * only save the config if the connection was successful! | |
483 | */ | |
484 | if(uxBits & TASK_WIFI_HAS_IP) { | |
485 | /* save wifi config */ | |
486 | SaveStaConfig(); | |
487 | } else { | |
488 | ESP_LOGI(TAG, "Connection failed"); // TODO: Scan other SSID's for known networks. | |
489 | /* failed attempt to connect regardles of the reason */ | |
490 | ||
491 | /* otherwise: reset the config */ | |
492 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
493 | } | |
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
|
494 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
0 | 495 | } 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
|
496 | /* 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
|
497 | 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
|
498 | 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
|
499 | 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
|
500 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
0 | 501 | } |
502 | ||
503 | } else if (uxBits & TASK_WIFI_REQUEST_WIFI_SCAN) { | |
504 | ||
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
|
505 | ESP_LOGI(TAG, "Request WiFi scan"); |
0 | 506 | /* safe guard against overflow */ |
507 | ap_num = MAX_AP_NUM; | |
508 | ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); | |
509 | ||
510 | /* finally: release the scan request bit */ | |
511 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_WIFI_SCAN); | |
512 | } | |
513 | ||
514 | /* | |
515 | * Here we should check for reconnect actions. | |
516 | */ | |
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
|
517 | // ESP_LOGI(TAG, "check reconnect"); |
0 | 518 | |
519 | } /* for(;;) */ | |
520 | vTaskDelay( (TickType_t)10); | |
521 | } | |
522 | ||
523 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
524 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
525 | * @brief Array with AP security names |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
526 | */ |
0 | 527 | const char *apsec[] = { "Open", "WEP", "WPA", "WPA2", "WPA WPA2", "Enterprise" }; |
528 | ||
529 | ||
530 | /** | |
531 | * @brief Show an AP station as a button. The buttons are already defined. | |
532 | * @param idx The index position on the display, 1 to 7. | |
533 | * @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
|
534 | * @param show How to display. 0 is blank, 1 is unknown, 2 is known, 3 is a connected AP. |
0 | 535 | */ |
536 | void Show_AP(uint8_t idx, wifi_ap_record_t ap, int show) | |
537 | { | |
538 | char tmp[33]; | |
539 | ||
540 | if ((idx > 7) || (idx < 1)) | |
541 | return; | |
542 | ||
543 | if (show == 0) { | |
544 | _bg = TFT_BLACK; | |
545 | } else { | |
546 | _bg = (color_t){ 63, 63, 64 }; | |
547 | } | |
548 | ||
549 | TFT_fillRect(Buttons[idx].x, Buttons[idx].y, Buttons[idx].w, Buttons[idx].h, _bg); | |
550 | if (show == 0) | |
551 | return; | |
552 | ||
553 | TFT_drawRect(Buttons[idx].x, Buttons[idx].y, Buttons[idx].w, Buttons[idx].h, TFT_NAVY); | |
554 | if (show == 3) { | |
555 | _fg = TFT_WHITE; | |
556 | } else if (show == 1) { | |
557 | _fg = TFT_YELLOW; | |
558 | } else { | |
559 | _fg = TFT_CYAN; | |
560 | } | |
561 | ||
562 | TFT_setFont(DEJAVU18_FONT, NULL); | |
563 | sprintf(tmp, "%s", ap.ssid); | |
564 | TFT_print(tmp, Buttons[idx].x + 5, Buttons[idx].y + 6); | |
565 | sprintf(tmp, "%d", ap.rssi); | |
566 | TFT_setFont(DEF_SMALL_FONT, NULL); | |
567 | TFT_print(tmp, Buttons[idx].x + Buttons[idx].w - (TFT_getStringWidth(tmp) + 5), Buttons[idx].y + 4); | |
568 | sprintf(tmp, "%s", apsec[ap.authmode]); | |
569 | TFT_print(tmp, Buttons[idx].x + Buttons[idx].w - (TFT_getStringWidth(tmp) + 5), Buttons[idx].y + 15); | |
570 | } | |
571 | ||
572 | ||
573 | ||
574 | bool WiFi_Init(void) | |
575 | { | |
576 | char pwd[65], pmpt[32]; | |
577 | ||
578 | switch (Main_Screen) { | |
579 | case MAIN_TOOLS_SETUP_WIFI: | |
580 | TopMessage("WiFi"); | |
581 | TFT_setFont(DEJAVU24_FONT, NULL); | |
582 | _fg = TFT_WHITE; | |
583 | TFT_print("Momentje ..", CENTER, CENTER); | |
584 | _wifi_ScanAPs = true; | |
585 | _wifi_ScanDone = false; | |
586 | Buttons_Add(260, 200, 60, 40, "Ok", 0); | |
587 | Buttons[0].dark = true; | |
588 | Buttons_Show(); | |
589 | // Now add the buttons we draw manually. | |
590 | Buttons_Add( 0, 30, 250, 30, "", 1); | |
591 | Buttons_Add( 0, 60, 250, 30, "", 2); | |
592 | Buttons_Add( 0, 90, 250, 30, "", 3); | |
593 | Buttons_Add( 0,120, 250, 30, "", 4); | |
594 | Buttons_Add( 0,150, 250, 30, "", 5); | |
595 | Buttons_Add( 0,180, 250, 30, "", 6); | |
596 | Buttons_Add( 0,210, 250, 30, "", 7); | |
597 | break; | |
598 | ||
599 | case MAIN_TOOLS_SETUP_WIFI_CUR: | |
600 | TopMessage("WiFi verbinding"); | |
601 | // Get extra information. | |
602 | wifi_ap_record_t ap_info; | |
603 | esp_wifi_sta_get_ap_info(&ap_info); | |
604 | ||
605 | wifi_config_t *wconfig = task_wifi_ConfigSTA /*task_wifi_GetWifiStaConfig( ) */; | |
606 | if (wconfig) { | |
607 | ||
608 | tcpip_adapter_ip_info_t ip_info; | |
609 | ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info)); | |
610 | char ip[IP4ADDR_STRLEN_MAX]; | |
611 | char gw[IP4ADDR_STRLEN_MAX]; | |
612 | char netmask[IP4ADDR_STRLEN_MAX]; | |
613 | strcpy(ip, ip4addr_ntoa(&ip_info.ip)); | |
614 | strcpy(netmask, ip4addr_ntoa(&ip_info.netmask)); | |
615 | strcpy(gw, ip4addr_ntoa(&ip_info.gw)); | |
616 | TFT_setFont(DEFAULT_FONT, NULL); | |
617 | _fg = TFT_WHITE; | |
618 | TFT_print("SSID", 155 - TFT_getStringWidth("SSID"), 40); | |
619 | TFT_print("Kanaal", 155 - TFT_getStringWidth("Kanaal"), 60); | |
620 | TFT_print("Rssi", 155 - TFT_getStringWidth("Rssi"), 80); | |
621 | TFT_print("Mode", 155 - TFT_getStringWidth("Mode"), 100); | |
622 | TFT_print("IP adres", 155 - TFT_getStringWidth("IP adres"), 120); | |
623 | TFT_print("Netmask", 155 - TFT_getStringWidth("Netmask"), 140); | |
624 | TFT_print("Gateway", 155 - TFT_getStringWidth("Gateway"), 160); | |
625 | _fg = TFT_YELLOW; | |
626 | TFT_print((char*)wconfig->sta.ssid, 165, 40); | |
627 | sprintf(pmpt, "%d", ap_info.primary); | |
628 | TFT_print(pmpt, 165, 60); | |
629 | sprintf(pmpt, "%d", ap_info.rssi); | |
630 | TFT_print(pmpt, 165, 80); | |
631 | sprintf(pmpt, "%s%s%s", ap_info.phy_11b ? "b":"", ap_info.phy_11g ? "g":"", ap_info.phy_11n ? "n":""); | |
632 | TFT_print(pmpt, 165, 100); | |
633 | TFT_print((char*)ip, 165, 120); | |
634 | TFT_print((char*)netmask, 165, 140); | |
635 | TFT_print((char*)gw, 165, 160); | |
636 | } | |
637 | Buttons_Add(130, 200, 60, 40, "Ok", 0); | |
638 | Buttons[0].dark = true; | |
639 | Buttons_Show(); | |
640 | break; | |
641 | ||
642 | case MAIN_TOOLS_SETUP_WIFI_CON: | |
643 | TopMessage("WiFi verbinden"); | |
644 | TFT_setFont(DEJAVU18_FONT, NULL); | |
645 | _fg = TFT_WHITE; | |
646 | TFT_print("SSID", 155 - TFT_getStringWidth("SSID"), 70); | |
647 | _fg = TFT_YELLOW; | |
648 | TFT_print((char*)_wifi_ssid, 165, 70); | |
649 | Buttons_Add( 0, 200, 100, 40, "Annuleer", 0); | |
650 | Buttons_Add(110, 200, 100, 40, "Vergeet", 1); | |
651 | Buttons_Add(220, 200, 100, 40, "Verbind", 2); | |
652 | Buttons_Show(); | |
653 | Buttons[0].dark = true; | |
654 | break; | |
655 | ||
656 | case MAIN_TOOLS_SETUP_WIFI_NEW: | |
657 | TopMessage("WiFi nieuw"); | |
658 | snprintf(pmpt, 32, "Password for %s", _wifi_ssid); | |
659 | pwd[0] = '\0'; | |
660 | EditTextMin(pmpt, pwd, 64, 8); | |
661 | /* | |
662 | * Disconnect first | |
663 | */ | |
664 | _bg = TFT_BLACK; | |
665 | TFT_fillScreen(_bg); | |
666 | TFT_setFont(DEJAVU24_FONT, NULL); | |
667 | _fg = TFT_WHITE; | |
668 | TFT_print("Momentje ..", CENTER, CENTER); | |
669 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
670 | vTaskDelay(100 / portTICK_PERIOD_MS); | |
671 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
672 | ||
673 | /* | |
674 | * Setup new connection | |
675 | */ | |
676 | if (strlen(pwd)) { | |
677 | wifi_config_t* config = task_wifi_ConfigSTA; | |
678 | memset(config, 0x00, sizeof(wifi_config_t)); | |
679 | memcpy(config->sta.ssid, _wifi_ssid, strlen((char*)_wifi_ssid)); | |
680 | memcpy(config->sta.password, pwd, strlen(pwd)); | |
681 | ESP_LOGI(TAG, "new AP %s %s", _wifi_ssid, pwd); | |
682 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
683 | } else { | |
684 | // TODO: what about WPS, it works but how to insert it in this app. | |
685 | return true; | |
686 | } | |
687 | // We must wait here for the result. | |
688 | break; | |
689 | ||
690 | default: | |
691 | break; | |
692 | } | |
693 | ||
694 | return false; | |
695 | } | |
696 | ||
697 | ||
698 | bool WiFi_Loop(void) | |
699 | { | |
700 | uint8_t idx; | |
701 | int Choice; | |
702 | static int AP[8]; | |
703 | wifi_ap_record_t ap; | |
704 | ||
705 | switch (Main_Screen) { | |
706 | case MAIN_TOOLS_SETUP_WIFI: | |
707 | if (_wifi_ScanAPs) { | |
708 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_WIFI_SCAN); | |
709 | _wifi_ScanAPs = false; | |
710 | TimeSpent = 0; | |
711 | } | |
712 | if (_wifi_ScanDone) { | |
713 | /* | |
714 | * Show scan results. There is room for 7 entries. If we have a connection, | |
715 | * the first one is that connection followed by available Access Points. | |
716 | * If there is no connection yet, there is only a iist of available Access | |
717 | * points. | |
718 | * The list is sorted by signal strength and is filled by the eventhandler. | |
719 | */ | |
720 | idx = 1; | |
721 | _wifi_ScanDone = false; | |
722 | ||
723 | if ((xEventGroupGetBits(xEventGroupWifi) & TASK_WIFI_STA_CONNECTED) == TASK_WIFI_STA_CONNECTED) { | |
724 | // We are connected. Search and display this one. | |
725 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
726 | for (int i = 0; i < _wifi_Scanned; i++) { | |
727 | ap = accessp_records[i]; | |
728 | if (strcmp(wifi_state->STA_ssid, (char *)ap.ssid) == 0) { | |
729 | AP[idx] = i; | |
730 | Show_AP(idx, ap, 3); | |
731 | idx++; | |
732 | break; | |
733 | } | |
734 | } | |
735 | xSemaphoreGive(xSemaphoreWiFi); | |
736 | } | |
737 | } | |
738 | ||
739 | // Display available Access Points. | |
740 | for (int i = 0; i < _wifi_Scanned; i++) { | |
741 | // The connected AP can be somewhere in this list, don't display it again. | |
742 | ap = accessp_records[i]; | |
743 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
744 | if (strcmp(wifi_state->STA_ssid, (char *)ap.ssid) == 0) { | |
745 | xSemaphoreGive(xSemaphoreWiFi); | |
746 | continue; // Skip connected AP, already on top of the list. | |
747 | } | |
748 | xSemaphoreGive(xSemaphoreWiFi); | |
749 | } | |
750 | // Check if we know this AP in the database. | |
751 | if ((read_station(ap.ssid) == -1)) { | |
752 | Show_AP(idx, ap, 2); // Unknown | |
753 | } else { | |
754 | if (wifiStation.hide) { | |
755 | continue; // Blacklisted. | |
756 | } | |
757 | Show_AP(idx, ap, 1); // We know this one. | |
758 | } | |
759 | AP[idx] = i; | |
760 | idx++; | |
761 | if (idx == 8) | |
762 | break; | |
763 | } | |
764 | if (idx < 7) { | |
765 | for (int i = idx; i < 8; i++) { | |
766 | Show_AP(i, ap, 0); | |
767 | AP[i] = 0; | |
768 | } | |
769 | } | |
770 | } | |
771 | ||
772 | Choice = Buttons_Scan(); | |
773 | if ((Choice >= 1) && (Choice <= 7)) { | |
774 | ap = accessp_records[AP[Choice]]; | |
775 | sprintf((char *)_wifi_ssid, "%s", ap.ssid); // Save selected SSID. | |
776 | } | |
777 | if ((Choice == 1) && ((xEventGroupGetBits(xEventGroupWifi) & TASK_WIFI_STA_CONNECTED) == TASK_WIFI_STA_CONNECTED)) { | |
778 | Main_Screen = MAIN_TOOLS_SETUP_WIFI_CUR; | |
779 | // Cancel a possible scan. | |
780 | ESP_ERROR_CHECK(esp_wifi_scan_stop()); | |
781 | } else if ((Choice >= 1) && (Choice <= 7)) { | |
782 | if ((read_station(_wifi_ssid) != -1)) { | |
783 | Main_Screen = MAIN_TOOLS_SETUP_WIFI_CON; | |
784 | } else { | |
785 | Main_Screen = MAIN_TOOLS_SETUP_WIFI_NEW; | |
786 | } | |
787 | ESP_ERROR_CHECK(esp_wifi_scan_stop()); | |
788 | } else if (Choice == 0) { | |
789 | Main_Screen = MAIN_TOOLS_SETUP; | |
790 | 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
|
791 | } else if (TimeSpent > 10) { |
0 | 792 | _wifi_ScanAPs = true; |
793 | } | |
794 | break; | |
795 | ||
796 | case MAIN_TOOLS_SETUP_WIFI_CUR: | |
797 | ||
798 | if (Buttons_Scan() == 0) { | |
799 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
800 | } | |
801 | break; | |
802 | ||
803 | case MAIN_TOOLS_SETUP_WIFI_CON: | |
804 | switch (Buttons_Scan()) { | |
805 | case 0: // Cancel choice | |
806 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
807 | break; | |
808 | ||
809 | case 1: // Forget connection | |
810 | remove_station(_wifi_ssid); | |
811 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
812 | break; | |
813 | ||
814 | case 2: // Connect | |
815 | _bg = TFT_BLACK; | |
816 | TFT_fillScreen(_bg); | |
817 | TFT_setFont(DEJAVU24_FONT, NULL); | |
818 | _fg = TFT_WHITE; | |
819 | TFT_print("Momentje ..", CENTER, CENTER); | |
820 | /* | |
821 | * Disconnect old connections and wait until it's gone. | |
822 | */ | |
823 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
824 | vTaskDelay(100 / portTICK_PERIOD_MS); | |
825 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
826 | /* | |
827 | * Setup new connection. | |
828 | */ | |
829 | if ((read_station(_wifi_ssid) != -1)) { | |
830 | wifi_config_t* config = task_wifi_ConfigSTA; | |
831 | memset(config, 0x00, sizeof(wifi_config_t)); | |
832 | memcpy(config->sta.ssid, wifiStation.SSID, strlen(wifiStation.SSID)); | |
833 | memcpy(config->sta.password, wifiStation.Password, strlen(wifiStation.Password)); | |
834 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
835 | vTaskDelay(1000 / portTICK_PERIOD_MS); | |
836 | } | |
837 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
838 | break; | |
839 | ||
840 | default: break; | |
841 | } | |
842 | break; | |
843 | ||
844 | case MAIN_TOOLS_SETUP_WIFI_NEW: | |
845 | // All work is already done, jump to the base screen. | |
846 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
847 | break; | |
848 | ||
849 | default: | |
850 | break; | |
851 | } | |
852 | ||
853 | return false; | |
854 | } | |
855 | ||
856 |