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