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