main/task_wifi.c

Sat, 29 Jun 2024 16:09:03 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 29 Jun 2024 16:09:03 +0200
branch
idf 5.1
changeset 140
ac6ad7bd55c3
parent 139
68404c07aa2b
permissions
-rw-r--r--

Version 0.4.1 Fixed deprecation message. Ignore WiFi channel change event

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

mercurial