Sun, 18 Jul 2021 11:25:55 +0200
Version 0.3.17. Made the Hendi PWM change official.
0 | 1 | /** |
2 | * @file task_wifi.c | |
3 | * @brief WiFi task. Connects to a known Access Point. If we know more then | |
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
4 | * one AP, try to connect all of them until it succeeds. |
0 | 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. | |
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
20 | esp_netif_t *sta_netif = NULL; ///< Station interface |
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
21 | |
0 | 22 | |
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
|
23 | 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
|
24 | .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
|
25 | .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
|
26 | .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
|
27 | .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
|
28 | }; |
0 | 29 | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
30 | uint8_t _wifi_ssid[33]; ///< Current SSID |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
31 | bool _wifi_ScanAPs = false; ///< Scanning |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
32 | bool _wifi_ScanDone = false; ///< Scan ready |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
33 | uint16_t _wifi_Scanned = 0; ///< Total scanned APs. |
0 | 34 | |
35 | extern int Main_Screen; ///< Current Screen number. | |
36 | extern sButton Buttons[MAXBUTTONS]; ///< Buttons definitions. | |
37 | 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
|
38 | 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
|
39 | 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
|
40 | 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
|
41 | extern struct tm timeinfo; |
0 | 42 | |
43 | const int TASK_WIFI_REQUEST_WIFI_SCAN = BIT0; ///< When set, means a client requested to scan wireless networks. | |
44 | const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1; ///< When set, means a client requested to disconnect from currently connected AP. | |
45 | const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2; ///< When set, means a client requested to connect to an access point. | |
46 | ||
47 | const int TASK_WIFI_HAS_IP = BIT3; ///< Indicate that we have an IP address | |
48 | 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
|
49 | const int TASK_WIFI_STA_DISCONNECTED = BIT6; ///< Indicate that we are disconnected from an ap station. |
0 | 50 | const int TASK_WIFI_STA_CONNECTED = BIT7; ///< Indicate that we are connected to AP as station, flip of BIT6. |
51 | ||
52 | ||
53 | /** | |
54 | * @brief Local function, save station configuration. | |
55 | * @return Esp error code. | |
56 | */ | |
57 | esp_err_t SaveStaConfig(void); | |
58 | ||
59 | /** | |
60 | * @brief Local function, fetch last connected station configuration. | |
61 | * @return True if there was a last connection, false if there was not. | |
62 | */ | |
63 | bool FetchStaConfig(void); | |
64 | ||
65 | /** | |
66 | * @brief Local function, WiFi event handler. | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
67 | * @param ctx Context |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
68 | * @param event The event |
0 | 69 | * @return Esp error code. |
70 | */ | |
71 | esp_err_t task_wifi_EventHandler(void *ctx, system_event_t *event); | |
72 | ||
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
|
73 | /** |
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 | * @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
|
75 | * 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
|
76 | * @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
|
77 | */ |
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 | 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
|
79 | |
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
|
80 | /** |
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 | * @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
|
82 | */ |
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 | 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
|
84 | |
0 | 85 | |
86 | /****************************************************************************/ | |
87 | ||
88 | ||
89 | ||
90 | esp_err_t SaveStaConfig(void) | |
91 | { | |
92 | int record; | |
93 | ||
94 | if (task_wifi_ConfigSTA && strlen((char *)task_wifi_ConfigSTA->sta.ssid)) { | |
95 | /* | |
96 | * Store in /spiffs/stations.conf if it's a new station. | |
97 | */ | |
98 | record = read_station(task_wifi_ConfigSTA->sta.ssid); | |
99 | if (record == -1) { | |
100 | add_station(task_wifi_ConfigSTA->sta.ssid, task_wifi_ConfigSTA->sta.password); | |
101 | } | |
102 | ||
103 | /* | |
104 | * Update main configuration if needed. | |
105 | */ | |
106 | if (strcmp(config.lastSSID, (char *)task_wifi_ConfigSTA->sta.ssid)) { | |
107 | sprintf(config.lastSSID, "%s", task_wifi_ConfigSTA->sta.ssid); | |
108 | write_config(); | |
109 | } | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
56
diff
changeset
|
110 | ESP_LOGD(TAG, "SaveStaConfig %s, record %d", wifiStation.SSID, record); |
0 | 111 | } |
112 | ||
113 | return ESP_OK; | |
114 | } | |
115 | ||
116 | ||
117 | ||
118 | bool FetchStaConfig(void) | |
119 | { | |
120 | if (task_wifi_ConfigSTA == NULL) { | |
121 | task_wifi_ConfigSTA = (wifi_config_t*)malloc(sizeof(wifi_config_t)); | |
122 | } | |
123 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
124 | ||
125 | /* | |
126 | * Search last connected AP as station. | |
127 | */ | |
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
|
128 | if (strlen(config.lastSSID) && (read_station((uint8_t *)config.lastSSID) >= 0)) { |
0 | 129 | |
130 | /* ssid */ | |
131 | size_t sz = sizeof(task_wifi_ConfigSTA->sta.ssid); | |
132 | memcpy(task_wifi_ConfigSTA->sta.ssid, wifiStation.SSID, sz); | |
133 | ||
134 | /* password */ | |
135 | sz = sizeof(task_wifi_ConfigSTA->sta.password); | |
136 | memcpy(task_wifi_ConfigSTA->sta.password, wifiStation.Password, sz); | |
137 | ||
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
71
diff
changeset
|
138 | ESP_LOGD(TAG, "FetchStaConfig: last connected to ssid: %s", task_wifi_ConfigSTA->sta.ssid); |
0 | 139 | return task_wifi_ConfigSTA->sta.ssid[0] != '\0'; |
140 | } | |
141 | ||
142 | ESP_LOGI(TAG, "FetchStaConfig: no last connection found"); | |
143 | return false; | |
144 | } | |
145 | ||
146 | ||
147 | ||
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
148 | static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) |
0 | 149 | { |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
150 | switch(event_id) { |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
151 | case WIFI_EVENT_SCAN_DONE: |
0 | 152 | // Get the results so the memory used is freed. |
153 | ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_num, accessp_records)); | |
154 | _wifi_Scanned = ap_num; | |
155 | _wifi_ScanDone = true; | |
156 | break; | |
157 | ||
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
158 | case WIFI_EVENT_STA_START: |
0 | 159 | // Set the configured hostname for the dhcp client. |
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
160 | ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, config.hostname)); |
0 | 161 | esp_wifi_connect(); |
162 | break; | |
163 | ||
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
164 | case WIFI_EVENT_STA_CONNECTED: { |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
165 | system_event_sta_connected_t* event = (wifi_event_sta_connected_t*) event_data; |
0 | 166 | wifi_ap_record_t ap_info; |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
167 | esp_wifi_sta_get_ap_info(&ap_info); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
168 | ESP_LOGI(TAG, "Event STA connected, ssid:%s, bssid:" MACSTR ", channel:%d, rssi: %d, authmode:%s", |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
169 | ap_info.ssid, MAC2STR(ap_info.bssid), event->channel, ap_info.rssi, apsec[event->authmode]); |
0 | 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); | |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
175 | } else { |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
176 | ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_CONNECTED"); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
177 | } |
0 | 178 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); |
179 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); | |
180 | 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
|
181 | } |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
182 | case WIFI_EVENT_STA_DISCONNECTED: { |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
183 | wifi_event_sta_disconnected_t* disconnected = (wifi_event_sta_disconnected_t*) event_data; |
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 | wifi_ap_record_t ap; |
0 | 185 | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
56
diff
changeset
|
186 | ESP_LOGW(TAG, "Event STA disconnected, ssid:%s, ssid_len:%d, bssid:" MACSTR ", reason:%d", |
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
|
187 | disconnected->ssid, disconnected->ssid_len, MAC2STR(disconnected->bssid), disconnected->reason); |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
188 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
0 | 189 | 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
|
190 | wifi_state->STA_online = false; |
0 | 191 | wifi_state->STA_rssi = 0; |
192 | xSemaphoreGive(xSemaphoreWiFi); | |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
193 | } else { |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
194 | ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_DISCONNECTED"); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
195 | } |
0 | 196 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); |
197 | 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
|
198 | 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
|
199 | |
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 | 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
|
201 | 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
|
202 | _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
|
203 | _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
|
204 | 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
|
205 | 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
|
206 | 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
|
207 | 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
|
208 | } |
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 | if (disconnected->reason == WIFI_REASON_NO_AP_FOUND && _wifi_ScanAPs && _wifi_ScanDone) { |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
56
diff
changeset
|
210 | ESP_LOGD(TAG, "Scan completed, look for another AP, found:%d", _wifi_Scanned); |
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
|
211 | _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
|
212 | _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
|
213 | 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
|
214 | 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
|
215 | 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
|
216 | 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
|
217 | 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
|
218 | } |
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 | /* 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
|
220 | 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
|
221 | 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
|
222 | 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
|
223 | memcpy(config->sta.password, wifiStation.Password, strlen(wifiStation.Password)); |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
56
diff
changeset
|
224 | ESP_LOGD(TAG, "new AP %s %s", wifiStation.SSID, wifiStation.Password); |
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
|
225 | 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
|
226 | 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
|
227 | } |
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
|
228 | } |
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 | 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
|
230 | } |
0 | 231 | |
232 | /* | |
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
|
233 | * Reconnect previous AP. |
0 | 234 | */ |
235 | if (FetchStaConfig()) { | |
236 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
237 | } | |
238 | 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
|
239 | } |
0 | 240 | default: |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
241 | ESP_LOGW(TAG, "Unknown WiFi event %d", event_id); |
0 | 242 | break; |
243 | } | |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
244 | } |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
245 | |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
246 | |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
247 | |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
248 | static void got_ip_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
249 | { |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
250 | switch (event_id) { |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
251 | |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
252 | case IP_EVENT_STA_GOT_IP: |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
253 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
254 | ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
255 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
256 | wifi_state->STA_online = true; |
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
257 | snprintf(wifi_state->STA_ip, 16, IPSTR, IP2STR(&event->ip_info.ip)); |
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
258 | snprintf(wifi_state->STA_nm, 16, IPSTR, IP2STR(&event->ip_info.netmask)); |
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
259 | snprintf(wifi_state->STA_gw, 16, IPSTR, IP2STR(&event->ip_info.gw)); |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
260 | xSemaphoreGive(xSemaphoreWiFi); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
261 | } else { |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
262 | ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_GOT_IP"); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
263 | } |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
264 | |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
265 | /* |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
266 | * There doesn't seem to be support for configuring NTP via DHCP so |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
267 | * we need to hardcode the ntp servers. The preffered server can be |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
268 | * set via the setup screen. It should be on your LAN, else leave it |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
269 | * empty. And if you are on a different lan someday, there is no extra |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
270 | * delay because the hostname will not be found. |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
271 | */ |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
272 | sntp_stop(); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
273 | if (strlen(config.ntp_server)) |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
274 | sntp_setservername(0, config.ntp_server); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
275 | sntp_setservername(1, (char *)"pool.ntp.org"); // Will get you servers nearby |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
276 | sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
277 | sntp_set_time_sync_notification_cb(time_sync_notification_cb); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
278 | sntp_init(); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
279 | #if 0 |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
280 | if (strlen(config.ntp_server)) |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
281 | ESP_LOGI(TAG, "NTP server %s", sntp_getservername(0)); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
282 | ESP_LOGI(TAG, "NTP server %s", sntp_getservername(1)); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
283 | #endif |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
284 | break; |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
285 | |
71 | 286 | case IP_EVENT_STA_LOST_IP: |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
287 | ESP_LOGW(TAG, "Lost IP address"); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
288 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
289 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
290 | wifi_state->STA_ip[0] = '\0'; |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
291 | wifi_state->STA_nm[0] = '\0'; |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
292 | wifi_state->STA_gw[0] = '\0'; |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
293 | wifi_state->STA_online = false; |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
294 | xSemaphoreGive(xSemaphoreWiFi); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
295 | } else { |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
296 | ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_LOST_IP"); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
297 | } |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
298 | sntp_stop(); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
299 | break; |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
300 | |
71 | 301 | case IP_EVENT_AP_STAIPASSIGNED: |
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
302 | ESP_LOGI(TAG, "IP_EVENT_AP_STAIPASSIGNED"); |
71 | 303 | break; |
304 | ||
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
305 | default: |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
306 | ESP_LOGW(TAG, "Unknown IP event %d", event_id); |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
307 | break; |
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
308 | } |
0 | 309 | } |
310 | ||
311 | ||
312 | ||
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
|
313 | 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
|
314 | { |
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
|
315 | 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
|
316 | |
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
|
317 | 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
|
318 | 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
|
319 | 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
|
320 | 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
|
321 | strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); |
62
2e90ada37476
Added syslog like logging on the sdcard for the manual and automatic processes.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
322 | log_msg(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
|
323 | } 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
|
324 | 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
|
325 | } |
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
|
326 | } |
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
|
327 | |
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
|
328 | |
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
|
329 | |
0 | 330 | void task_wifi( void * pvParameters ) |
331 | { | |
332 | esp_err_t ret; | |
333 | ||
88
7f02dbee58d0
Fix missing log entries. More code space saving.
Michiel Broek <mbroek@mbse.eu>
parents:
87
diff
changeset
|
334 | ESP_LOGI(TAG, "Start WiFi"); |
0 | 335 | |
336 | /* | |
337 | * Initialize NVS | |
338 | */ | |
339 | ret = nvs_flash_init(); | |
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
340 | if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
0 | 341 | ESP_ERROR_CHECK(nvs_flash_erase()); |
342 | ret = nvs_flash_init(); | |
343 | } | |
344 | ESP_ERROR_CHECK(ret); | |
345 | ||
346 | /* event handler and event group for the wifi driver */ | |
347 | xEventGroupWifi = xEventGroupCreate(); | |
348 | /* initialize the tcp stack */ | |
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
349 | ESP_ERROR_CHECK(esp_netif_init()); |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
350 | ESP_ERROR_CHECK(esp_event_loop_create_default()); |
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
351 | sta_netif = esp_netif_create_default_wifi_sta(); |
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
352 | assert(sta_netif); |
0 | 353 | |
354 | /* | |
355 | * memory allocation of objects used by the task | |
356 | */ | |
357 | accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * MAX_AP_NUM); | |
358 | task_wifi_ConfigSTA = (wifi_config_t*)malloc(sizeof(wifi_config_t)); | |
359 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
360 | ||
361 | xSemaphoreWiFi = xSemaphoreCreateMutex(); | |
362 | wifi_state = malloc(sizeof(WIFI_State)); | |
363 | 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
|
364 | wifi_state->STA_online = false; |
0 | 365 | wifi_state->STA_rssi = 0; |
366 | ||
367 | /* | |
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
368 | * init wifi as station |
0 | 369 | */ |
370 | wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); | |
371 | ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); | |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
372 | |
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
373 | esp_event_handler_instance_t instance_any_id; |
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
374 | esp_event_handler_instance_t instance_got_ip; |
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
375 | ESP_ERROR_CHECK( esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) ); |
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
376 | ESP_ERROR_CHECK( esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) ); |
70
d6838a268020
Version 0.3.7. The WiFi task uses the new event handlers. Cooling temperature top is now 45 instead of 30 degreees for pitching Kveik. One extra cosmetic message during OTA update.
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
377 | |
0 | 378 | ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
379 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
0 | 380 | |
381 | ESP_ERROR_CHECK(esp_wifi_start()); | |
382 | ||
383 | /* | |
384 | * try to get access to previously saved wifi | |
385 | */ | |
386 | if (FetchStaConfig()) { | |
387 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
388 | } | |
389 | ||
390 | /* | |
391 | * Wait for access point to start | |
392 | */ | |
393 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); | |
394 | EventBits_t uxBits; | |
395 | ||
396 | for(;;) { | |
397 | ||
398 | /* actions that can trigger: request a connection, a scan, or a disconnection */ | |
399 | uxBits = xEventGroupWaitBits(xEventGroupWifi, | |
400 | TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT, | |
401 | pdFALSE, pdFALSE, portMAX_DELAY ); | |
402 | ||
403 | if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { | |
404 | /* | |
405 | * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory | |
406 | */ | |
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
|
407 | ESP_LOGI(TAG, "Request STA disconnect"); |
0 | 408 | sntp_stop(); |
409 | ESP_ERROR_CHECK(esp_wifi_disconnect()); | |
410 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
411 | ||
412 | /* | |
413 | * erase configuration | |
414 | */ | |
415 | if (task_wifi_ConfigSTA) { | |
416 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
417 | } | |
418 | config.lastSSID[0] = '\0'; | |
419 | write_config(); | |
420 | ||
421 | /* finally: release the scan request bit */ | |
422 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
423 | ||
424 | } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) { | |
425 | ||
426 | //someone requested a connection! | |
427 | ESP_LOGI(TAG, "Request STA connect `%s'", task_wifi_ConfigSTA->sta.ssid); | |
428 | /* set the new config and connect - reset the failed bit first as it is later tested */ | |
429 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); | |
430 | ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, task_wifi_ConfigSTA)); | |
431 | ||
432 | esp_err_t wifierror = esp_wifi_connect(); | |
433 | if (wifierror != ESP_OK) { | |
434 | ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x", (int)wifierror); | |
435 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); | |
436 | } | |
437 | ||
438 | /* | |
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
|
439 | * 3 scenarios here: connection is successful and TASK_WIFI_STA_CONNECTED will be posted |
0 | 440 | * 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
|
441 | * Or, option 3, the 5 seconds timeout is reached. This happens when the AP is not in range. |
0 | 442 | * Note that the reason code is not exploited. For all intent and purposes a failure is a failure. |
443 | */ | |
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
|
444 | // 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
|
445 | 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
|
446 | // ESP_LOGI(TAG, "2 waitbits %08x", uxBits & (TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED)); |
0 | 447 | |
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
|
448 | if (uxBits & (TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED)) { |
0 | 449 | /* |
450 | * only save the config if the connection was successful! | |
451 | */ | |
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
|
452 | if (uxBits & TASK_WIFI_STA_CONNECTED) { |
0 | 453 | /* save wifi config */ |
454 | SaveStaConfig(); | |
455 | } else { | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
56
diff
changeset
|
456 | ESP_LOGW(TAG, "Connection failed"); |
0 | 457 | /* failed attempt to connect regardles of the reason */ |
458 | ||
459 | /* otherwise: reset the config */ | |
460 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
461 | } | |
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
|
462 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
0 | 463 | } 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
|
464 | /* hit 10 seconds timeout */ |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
56
diff
changeset
|
465 | ESP_LOGW(TAG, "Connection timeout"); |
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
|
466 | 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
|
467 | 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
|
468 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
0 | 469 | } |
470 | ||
471 | } else if (uxBits & TASK_WIFI_REQUEST_WIFI_SCAN) { | |
472 | ||
473 | /* safe guard against overflow */ | |
474 | ap_num = MAX_AP_NUM; | |
475 | ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); | |
476 | ||
477 | /* finally: release the scan request bit */ | |
478 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_WIFI_SCAN); | |
479 | } | |
480 | ||
481 | } /* for(;;) */ | |
482 | vTaskDelay( (TickType_t)10); | |
483 | } | |
484 | ||
485 | ||
486 | ||
487 | /** | |
488 | * @brief Show an AP station as a button. The buttons are already defined. | |
489 | * @param idx The index position on the display, 1 to 7. | |
490 | * @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
|
491 | * @param show How to display. 0 is blank, 1 is unknown, 2 is known, 3 is a connected AP. |
0 | 492 | */ |
493 | void Show_AP(uint8_t idx, wifi_ap_record_t ap, int show) | |
494 | { | |
495 | char tmp[33]; | |
496 | ||
497 | if ((idx > 7) || (idx < 1)) | |
498 | return; | |
499 | ||
500 | if (show == 0) { | |
501 | _bg = TFT_BLACK; | |
502 | } else { | |
503 | _bg = (color_t){ 63, 63, 64 }; | |
504 | } | |
505 | ||
506 | TFT_fillRect(Buttons[idx].x, Buttons[idx].y, Buttons[idx].w, Buttons[idx].h, _bg); | |
507 | if (show == 0) | |
508 | return; | |
509 | ||
510 | TFT_drawRect(Buttons[idx].x, Buttons[idx].y, Buttons[idx].w, Buttons[idx].h, TFT_NAVY); | |
511 | if (show == 3) { | |
512 | _fg = TFT_WHITE; | |
513 | } else if (show == 1) { | |
514 | _fg = TFT_YELLOW; | |
515 | } else { | |
516 | _fg = TFT_CYAN; | |
517 | } | |
518 | ||
519 | TFT_setFont(DEJAVU18_FONT, NULL); | |
520 | sprintf(tmp, "%s", ap.ssid); | |
521 | TFT_print(tmp, Buttons[idx].x + 5, Buttons[idx].y + 6); | |
522 | sprintf(tmp, "%d", ap.rssi); | |
523 | TFT_setFont(DEF_SMALL_FONT, NULL); | |
524 | TFT_print(tmp, Buttons[idx].x + Buttons[idx].w - (TFT_getStringWidth(tmp) + 5), Buttons[idx].y + 4); | |
525 | sprintf(tmp, "%s", apsec[ap.authmode]); | |
526 | TFT_print(tmp, Buttons[idx].x + Buttons[idx].w - (TFT_getStringWidth(tmp) + 5), Buttons[idx].y + 15); | |
527 | } | |
528 | ||
529 | ||
530 | ||
531 | bool WiFi_Init(void) | |
532 | { | |
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
|
533 | char pwd[65], pmpt[96]; |
0 | 534 | |
535 | switch (Main_Screen) { | |
536 | 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
|
537 | TopMessage((char *)"WiFi"); |
0 | 538 | TFT_setFont(DEJAVU24_FONT, NULL); |
539 | _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
|
540 | TFT_print((char *)"Momentje ..", CENTER, CENTER); |
0 | 541 | _wifi_ScanAPs = true; |
542 | _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
|
543 | Buttons_Add(260, 200, 60, 40, (char *)"Ok", 0); |
0 | 544 | Buttons[0].dark = true; |
545 | Buttons_Show(); | |
546 | // 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
|
547 | 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
|
548 | 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
|
549 | 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
|
550 | 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
|
551 | 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
|
552 | 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
|
553 | Buttons_Add( 0,210, 250, 30, (char *)"", 7); |
0 | 554 | break; |
555 | ||
556 | 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
|
557 | TopMessage((char *)"WiFi verbinding"); |
0 | 558 | // Get extra information. |
559 | wifi_ap_record_t ap_info; | |
560 | esp_wifi_sta_get_ap_info(&ap_info); | |
561 | ||
562 | wifi_config_t *wconfig = task_wifi_ConfigSTA /*task_wifi_GetWifiStaConfig( ) */; | |
563 | if (wconfig) { | |
564 | ||
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
565 | esp_netif_ip_info_t ip_info; |
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
566 | ESP_ERROR_CHECK(esp_netif_get_ip_info(sta_netif, &ip_info)); |
0 | 567 | char ip[IP4ADDR_STRLEN_MAX]; |
568 | char gw[IP4ADDR_STRLEN_MAX]; | |
569 | char netmask[IP4ADDR_STRLEN_MAX]; | |
94
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
570 | sprintf(ip, IPSTR, IP2STR(&ip_info.ip)); |
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
571 | sprintf(netmask, IPSTR, IP2STR(&ip_info.netmask)); |
87aa80b8e452
Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.
Michiel Broek <mbroek@mbse.eu>
parents:
88
diff
changeset
|
572 | sprintf(gw, IPSTR, IP2STR(&ip_info.gw)); |
0 | 573 | TFT_setFont(DEFAULT_FONT, NULL); |
574 | _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
|
575 | 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
|
576 | 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
|
577 | 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
|
578 | 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
|
579 | 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
|
580 | 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
|
581 | TFT_print((char *)"Gateway", 155 - TFT_getStringWidth((char *)"Gateway"), 160); |
0 | 582 | _fg = TFT_YELLOW; |
583 | TFT_print((char*)wconfig->sta.ssid, 165, 40); | |
584 | sprintf(pmpt, "%d", ap_info.primary); | |
585 | TFT_print(pmpt, 165, 60); | |
586 | sprintf(pmpt, "%d", ap_info.rssi); | |
587 | TFT_print(pmpt, 165, 80); | |
588 | sprintf(pmpt, "%s%s%s", ap_info.phy_11b ? "b":"", ap_info.phy_11g ? "g":"", ap_info.phy_11n ? "n":""); | |
589 | TFT_print(pmpt, 165, 100); | |
590 | TFT_print((char*)ip, 165, 120); | |
591 | TFT_print((char*)netmask, 165, 140); | |
592 | TFT_print((char*)gw, 165, 160); | |
593 | } | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
594 | Buttons_Add(130, 200, 60, 40, (char *)"Ok", 0); |
0 | 595 | Buttons[0].dark = true; |
596 | Buttons_Show(); | |
597 | break; | |
598 | ||
599 | 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
|
600 | TopMessage((char *)"WiFi verbinden"); |
0 | 601 | TFT_setFont(DEJAVU18_FONT, NULL); |
602 | _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
|
603 | TFT_print((char *)"SSID", 155 - TFT_getStringWidth((char *)"SSID"), 70); |
0 | 604 | _fg = TFT_YELLOW; |
605 | 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
|
606 | 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
|
607 | 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
|
608 | Buttons_Add(220, 200, 100, 40, (char *)"Verbind", 2); |
0 | 609 | Buttons_Show(); |
610 | Buttons[0].dark = true; | |
611 | break; | |
612 | ||
613 | 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
|
614 | 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
|
615 | snprintf(pmpt, 95, "Password for %s", _wifi_ssid); |
0 | 616 | pwd[0] = '\0'; |
617 | EditTextMin(pmpt, pwd, 64, 8); | |
618 | /* | |
619 | * Disconnect first | |
620 | */ | |
621 | _bg = TFT_BLACK; | |
622 | TFT_fillScreen(_bg); | |
623 | TFT_setFont(DEJAVU24_FONT, NULL); | |
624 | _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
|
625 | TFT_print((char *)"Momentje ..", CENTER, CENTER); |
0 | 626 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); |
627 | vTaskDelay(100 / portTICK_PERIOD_MS); | |
628 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
629 | ||
630 | /* | |
631 | * Setup new connection | |
632 | */ | |
633 | if (strlen(pwd)) { | |
634 | wifi_config_t* config = task_wifi_ConfigSTA; | |
635 | memset(config, 0x00, sizeof(wifi_config_t)); | |
636 | memcpy(config->sta.ssid, _wifi_ssid, strlen((char*)_wifi_ssid)); | |
637 | memcpy(config->sta.password, pwd, strlen(pwd)); | |
638 | ESP_LOGI(TAG, "new AP %s %s", _wifi_ssid, pwd); | |
639 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
640 | } else { | |
641 | // TODO: what about WPS, it works but how to insert it in this app. | |
642 | return true; | |
643 | } | |
644 | // We must wait here for the result. | |
645 | break; | |
646 | ||
647 | default: | |
648 | break; | |
649 | } | |
650 | ||
651 | return false; | |
652 | } | |
653 | ||
654 | ||
655 | bool WiFi_Loop(void) | |
656 | { | |
657 | uint8_t idx; | |
658 | int Choice; | |
659 | static int AP[8]; | |
660 | wifi_ap_record_t ap; | |
661 | ||
662 | switch (Main_Screen) { | |
663 | case MAIN_TOOLS_SETUP_WIFI: | |
664 | if (_wifi_ScanAPs) { | |
665 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_WIFI_SCAN); | |
666 | _wifi_ScanAPs = false; | |
667 | TimeSpent = 0; | |
668 | } | |
669 | if (_wifi_ScanDone) { | |
670 | /* | |
671 | * Show scan results. There is room for 7 entries. If we have a connection, | |
672 | * the first one is that connection followed by available Access Points. | |
673 | * If there is no connection yet, there is only a iist of available Access | |
674 | * points. | |
675 | * The list is sorted by signal strength and is filled by the eventhandler. | |
676 | */ | |
677 | idx = 1; | |
678 | _wifi_ScanDone = false; | |
679 | ||
680 | if ((xEventGroupGetBits(xEventGroupWifi) & TASK_WIFI_STA_CONNECTED) == TASK_WIFI_STA_CONNECTED) { | |
681 | // We are connected. Search and display this one. | |
682 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
683 | for (int i = 0; i < _wifi_Scanned; i++) { | |
684 | ap = accessp_records[i]; | |
685 | if (strcmp(wifi_state->STA_ssid, (char *)ap.ssid) == 0) { | |
686 | AP[idx] = i; | |
687 | Show_AP(idx, ap, 3); | |
688 | idx++; | |
689 | break; | |
690 | } | |
691 | } | |
692 | xSemaphoreGive(xSemaphoreWiFi); | |
693 | } | |
694 | } | |
695 | ||
696 | // Display available Access Points. | |
697 | for (int i = 0; i < _wifi_Scanned; i++) { | |
698 | // The connected AP can be somewhere in this list, don't display it again. | |
699 | ap = accessp_records[i]; | |
700 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
701 | if (strcmp(wifi_state->STA_ssid, (char *)ap.ssid) == 0) { | |
702 | xSemaphoreGive(xSemaphoreWiFi); | |
703 | continue; // Skip connected AP, already on top of the list. | |
704 | } | |
705 | xSemaphoreGive(xSemaphoreWiFi); | |
706 | } | |
707 | // Check if we know this AP in the database. | |
708 | if ((read_station(ap.ssid) == -1)) { | |
709 | Show_AP(idx, ap, 2); // Unknown | |
710 | } else { | |
711 | if (wifiStation.hide) { | |
712 | continue; // Blacklisted. | |
713 | } | |
714 | Show_AP(idx, ap, 1); // We know this one. | |
715 | } | |
716 | AP[idx] = i; | |
717 | idx++; | |
718 | if (idx == 8) | |
719 | break; | |
720 | } | |
721 | if (idx < 7) { | |
722 | for (int i = idx; i < 8; i++) { | |
723 | Show_AP(i, ap, 0); | |
724 | AP[i] = 0; | |
725 | } | |
726 | } | |
727 | } | |
728 | ||
729 | Choice = Buttons_Scan(); | |
730 | if ((Choice >= 1) && (Choice <= 7)) { | |
731 | ap = accessp_records[AP[Choice]]; | |
732 | sprintf((char *)_wifi_ssid, "%s", ap.ssid); // Save selected SSID. | |
733 | } | |
734 | if ((Choice == 1) && ((xEventGroupGetBits(xEventGroupWifi) & TASK_WIFI_STA_CONNECTED) == TASK_WIFI_STA_CONNECTED)) { | |
735 | Main_Screen = MAIN_TOOLS_SETUP_WIFI_CUR; | |
736 | // Cancel a possible scan. | |
737 | ESP_ERROR_CHECK(esp_wifi_scan_stop()); | |
738 | } else if ((Choice >= 1) && (Choice <= 7)) { | |
739 | if ((read_station(_wifi_ssid) != -1)) { | |
740 | Main_Screen = MAIN_TOOLS_SETUP_WIFI_CON; | |
741 | } else { | |
742 | Main_Screen = MAIN_TOOLS_SETUP_WIFI_NEW; | |
743 | } | |
744 | ESP_ERROR_CHECK(esp_wifi_scan_stop()); | |
745 | } else if (Choice == 0) { | |
746 | 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
|
747 | _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
|
748 | _wifi_ScanDone = false; |
0 | 749 | 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
|
750 | } else if (TimeSpent > 10) { |
0 | 751 | _wifi_ScanAPs = true; |
752 | } | |
753 | break; | |
754 | ||
755 | case MAIN_TOOLS_SETUP_WIFI_CUR: | |
756 | ||
757 | if (Buttons_Scan() == 0) { | |
758 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
759 | } | |
760 | break; | |
761 | ||
762 | case MAIN_TOOLS_SETUP_WIFI_CON: | |
763 | switch (Buttons_Scan()) { | |
764 | case 0: // Cancel choice | |
765 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
766 | break; | |
767 | ||
768 | case 1: // Forget connection | |
769 | remove_station(_wifi_ssid); | |
770 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
771 | break; | |
772 | ||
773 | case 2: // Connect | |
774 | _bg = TFT_BLACK; | |
775 | TFT_fillScreen(_bg); | |
776 | TFT_setFont(DEJAVU24_FONT, NULL); | |
777 | _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
|
778 | TFT_print((char *)"Momentje ..", CENTER, CENTER); |
0 | 779 | /* |
780 | * Disconnect old connections and wait until it's gone. | |
781 | */ | |
782 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
783 | vTaskDelay(100 / portTICK_PERIOD_MS); | |
784 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
785 | /* | |
786 | * Setup new connection. | |
787 | */ | |
788 | if ((read_station(_wifi_ssid) != -1)) { | |
789 | wifi_config_t* config = task_wifi_ConfigSTA; | |
790 | memset(config, 0x00, sizeof(wifi_config_t)); | |
791 | memcpy(config->sta.ssid, wifiStation.SSID, strlen(wifiStation.SSID)); | |
792 | memcpy(config->sta.password, wifiStation.Password, strlen(wifiStation.Password)); | |
793 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
794 | vTaskDelay(1000 / portTICK_PERIOD_MS); | |
795 | } | |
796 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
797 | break; | |
798 | ||
799 | default: break; | |
800 | } | |
801 | break; | |
802 | ||
803 | case MAIN_TOOLS_SETUP_WIFI_NEW: | |
804 | // All work is already done, jump to the base screen. | |
805 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
806 | break; | |
807 | ||
808 | default: | |
809 | break; | |
810 | } | |
811 | ||
812 | return false; | |
813 | } | |
814 | ||
815 |