Tue, 04 Apr 2023 20:57:49 +0200
Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
4 | 1 | /** |
2 | * @file task_wifi.c | |
3 | * @brief WiFi task. Connects to a known Access Point. | |
4 | */ | |
5 | ||
6 | ||
7 | #include "config.h" | |
8 | ||
9 | ||
10 | static const char *TAG = "task_wifi"; | |
11 | ||
12 | #define ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID | |
13 | #define ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD | |
14 | ||
15 | SemaphoreHandle_t xSemaphoreWiFi = NULL; ///< Semaphore WiFi task. | |
16 | EventGroupHandle_t xEventGroupWifi; ///< Events WiFi task. | |
17 | wifi_config_t wifi_Config = { ///< Current STA configuration. | |
18 | .sta = { | |
19 | .ssid = ESP_WIFI_SSID, | |
20 | .password = ESP_WIFI_PASS, | |
21 | .scan_method = WIFI_FAST_SCAN, | |
22 | .sort_method = WIFI_CONNECT_AP_BY_SECURITY, | |
23 | .threshold.rssi = -127, | |
24 | .threshold.authmode = WIFI_AUTH_WPA2_PSK, | |
25 | }, | |
26 | }; | |
27 | WIFI_State *wifi_state = NULL; ///< Public state for other tasks. | |
28 | esp_netif_t *sta_netif = NULL; ///< Station interface | |
29 | ||
30 | ||
31 | const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1; ///< When set, means a client requested to disconnect from currently connected AP. | |
32 | const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2; ///< When set, means a client requested to connect to an access point. | |
33 | ||
34 | const int TASK_WIFI_HAS_IP = BIT3; ///< Indicate that we have an IP address | |
35 | const int TASK_WIFI_STA_FAILED = BIT5; ///< Indicate that we could not get a connection to AP as station. | |
36 | const int TASK_WIFI_STA_DISCONNECTED = BIT6; ///< Indicate that we are disconnected from an ap station. | |
37 | const int TASK_WIFI_STA_CONNECTED = BIT7; ///< Indicate that we are connected to AP as station, flip of BIT6. | |
38 | ||
39 | ||
40 | /****************************************************************************/ | |
41 | ||
42 | ||
43 | bool ready_WiFi(void) | |
44 | { | |
15
64028e178ff1
Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
45 | if ((xEventGroupGetBits(xEventGroupWifi) & (TASK_WIFI_STA_CONNECTED | TASK_WIFI_HAS_IP)) == (TASK_WIFI_STA_CONNECTED | TASK_WIFI_HAS_IP)) |
4 | 46 | return true; |
47 | return false; | |
48 | } | |
49 | ||
50 | ||
51 | ||
52 | void request_WiFi(bool connect) | |
53 | { | |
54 | if (connect) | |
55 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
56 | else | |
57 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
58 | } | |
59 | ||
60 | ||
61 | ||
62 | static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) | |
63 | { | |
64 | switch (event_id) { | |
65 | ||
66 | case WIFI_EVENT_STA_START: | |
67 | ESP_LOGI(TAG, "Event wifi START"); | |
68 | // Set the hostname for the dhcp client. | |
69 | #ifdef CONFIG_CODE_PRODUCTION | |
70 | ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, "balkon")); | |
71 | #endif | |
72 | #ifdef CONFIG_CODE_TESTING | |
73 | ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, "wemos")); | |
74 | #endif | |
75 | break; | |
76 | ||
77 | case WIFI_EVENT_STA_CONNECTED: { | |
78 | // system_event_sta_connected_t* event = (wifi_event_sta_connected_t*) event_data; | |
79 | wifi_ap_record_t ap_info; | |
80 | esp_wifi_sta_get_ap_info(&ap_info); | |
81 | ESP_LOGI(TAG, "Event STA connected rssi=%d", ap_info.rssi); | |
82 | if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) { | |
83 | wifi_state->STA_connected = true; | |
84 | wifi_state->STA_rssi = ap_info.rssi; | |
85 | xSemaphoreGive(xSemaphoreWiFi); | |
86 | } else { | |
87 | ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_CONNECTED"); | |
88 | } | |
89 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); | |
90 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); | |
91 | break; | |
92 | } | |
93 | ||
94 | case WIFI_EVENT_STA_DISCONNECTED: { | |
15
64028e178ff1
Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
95 | wifi_event_sta_disconnected_t* disconnected = (wifi_event_sta_disconnected_t*) event_data; |
64028e178ff1
Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
96 | |
64028e178ff1
Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
97 | ESP_LOGI(TAG, "Event STA disconnected, reason: %d", disconnected->reason); |
64028e178ff1
Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
98 | if (disconnected->reason != 8) |
64028e178ff1
Splitted wifi and mqtt connect states to make the connections more robust. Protect the mqtt connection request against requests when a request is already done or in progress.
Michiel Broek <mbroek@mbse.eu>
parents:
9
diff
changeset
|
99 | request_mqtt(false); |
4 | 100 | if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) { |
101 | wifi_state->STA_connected = false; | |
102 | wifi_state->STA_online = false; | |
103 | xSemaphoreGive(xSemaphoreWiFi); | |
104 | } else { | |
105 | ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_DISCONNECTED"); | |
106 | } | |
107 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); | |
108 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); | |
109 | break; | |
110 | } | |
111 | ||
112 | default: | |
113 | ESP_LOGW(TAG, "Unknown WiFi event %d", (int)event_id); | |
114 | break; | |
115 | } | |
116 | } | |
117 | ||
118 | ||
119 | ||
120 | static void got_ip_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) | |
121 | { | |
122 | switch (event_id) { | |
123 | ||
124 | case IP_EVENT_STA_GOT_IP: | |
6
bad3414f7bc4
Added the getTempBaro and getVoltsCurrent functions to process the measured data. Added some main task code. Added subscribe to MQTT events.
Michiel Broek <mbroek@mbse.eu>
parents:
5
diff
changeset
|
125 | //ESP_LOGE(TAG, "got_ip_event_handler()"); |
4 | 126 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP); |
127 | ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; | |
128 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
129 | wifi_state->STA_online = true; | |
130 | snprintf(wifi_state->STA_ip, 16, IPSTR, IP2STR(&event->ip_info.ip)); | |
131 | snprintf(wifi_state->STA_nm, 16, IPSTR, IP2STR(&event->ip_info.netmask)); | |
132 | snprintf(wifi_state->STA_gw, 16, IPSTR, IP2STR(&event->ip_info.gw)); | |
133 | xSemaphoreGive(xSemaphoreWiFi); | |
134 | } else { | |
135 | ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_GOT_IP"); | |
136 | } | |
137 | break; | |
138 | ||
139 | case IP_EVENT_STA_LOST_IP: | |
140 | ESP_LOGW(TAG, "Lost IP address"); | |
141 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP); | |
142 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
143 | wifi_state->STA_ip[0] = '\0'; | |
144 | wifi_state->STA_nm[0] = '\0'; | |
145 | wifi_state->STA_gw[0] = '\0'; | |
146 | wifi_state->STA_online = false; | |
147 | xSemaphoreGive(xSemaphoreWiFi); | |
148 | } else { | |
149 | ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_LOST_IP"); | |
150 | } | |
151 | break; | |
152 | ||
153 | default: | |
154 | ESP_LOGW(TAG, "Unknown IP event %d", (int)event_id); | |
155 | break; | |
156 | } | |
157 | } | |
158 | ||
159 | ||
160 | ||
161 | void task_wifi( void * pvParameters ) | |
162 | { | |
163 | ESP_LOGI(TAG, "Start WiFi"); | |
164 | ||
165 | /* event handler and event group for the wifi driver */ | |
166 | xEventGroupWifi = xEventGroupCreate(); | |
167 | /* initialize the tcp stack */ | |
168 | ESP_ERROR_CHECK(esp_netif_init()); | |
169 | ESP_ERROR_CHECK(esp_event_loop_create_default()); | |
170 | sta_netif = esp_netif_create_default_wifi_sta(); | |
171 | assert(sta_netif); | |
172 | ||
173 | /* | |
174 | * memory allocation of objects used by the task | |
175 | */ | |
176 | xSemaphoreWiFi = xSemaphoreCreateMutex(); | |
177 | wifi_state = malloc(sizeof(WIFI_State)); | |
178 | memset(wifi_state, 0x00, sizeof(WIFI_State)); | |
179 | sprintf(wifi_state->STA_ssid, "%s", ESP_WIFI_SSID); | |
180 | ||
181 | /* | |
182 | * init wifi as station | |
183 | */ | |
184 | wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); | |
185 | ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); | |
186 | ||
187 | esp_event_handler_instance_t instance_any_id; | |
188 | esp_event_handler_instance_t instance_got_ip; | |
189 | ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) ); | |
190 | ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, /*IP_EVENT_STA_GOT_IP*/ ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) ); | |
191 | ||
192 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); | |
193 | ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_Config) ); | |
194 | ESP_ERROR_CHECK(esp_wifi_start()); | |
195 | ||
196 | //xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
197 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); | |
198 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); | |
199 | EventBits_t uxBits; | |
200 | ||
201 | ESP_LOGI(TAG, "Startup completed, enter task loop"); | |
202 | ||
203 | for(;;) { | |
204 | ||
205 | /* actions that can trigger: request a connection or a disconnection */ | |
206 | uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT, | |
207 | pdFALSE, pdFALSE, portMAX_DELAY ); | |
208 | ||
209 | if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { | |
210 | /* | |
211 | * user requested a disconnect, this will in effect disconnect the wifi | |
212 | */ | |
7
2b337dd92f25
Added volts/current loop calculations. Added millis() timer running on the hardware clock. Completed most of the main state loop. Added MQTT wait for disconnect. MQTT disconnect in two passes, disconnect and stop.
Michiel Broek <mbroek@mbse.eu>
parents:
6
diff
changeset
|
213 | ESP_LOGI(TAG, "Request STA disconnect"); |
4 | 214 | ESP_ERROR_CHECK(esp_wifi_disconnect()); |
215 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
216 | ||
217 | /* finally: release the request bit */ | |
218 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
219 | ||
220 | } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) { | |
221 | ||
222 | ESP_LOGI(TAG, "Request STA connect `%s' `%s'", wifi_Config.sta.ssid, wifi_Config.sta.password); | |
223 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); | |
224 | ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_Config)); | |
225 | ||
226 | esp_err_t wifierror = esp_wifi_connect(); | |
227 | if (wifierror != ESP_OK) { | |
228 | ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x", (int)wifierror); | |
229 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); | |
230 | } else { | |
231 | ESP_LOGI(TAG, "Connected Ok"); | |
232 | } | |
233 | ||
234 | /* | |
235 | * 3 scenarios here: connection is successful and TASK_WIFI_STA_CONNECTED will be posted | |
236 | * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code. | |
237 | * Or, option 3, the 5 seconds timeout is reached. This happens when the AP is not in range. | |
238 | * Note that the reason code is not exploited. For all intent and purposes a failure is a failure. | |
239 | */ | |
240 | uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 5000 / portTICK_PERIOD_MS); | |
241 | ||
242 | if (uxBits & (TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED)) { | |
243 | /* | |
244 | * only save the config if the connection was successful! | |
245 | */ | |
246 | if (uxBits & TASK_WIFI_STA_CONNECTED) { | |
247 | /* save wifi config */ | |
248 | //SaveStaConfig(); | |
249 | } else { | |
250 | ESP_LOGI(TAG, "No AP found"); | |
251 | vTaskDelay(3000 / portTICK_PERIOD_MS); | |
252 | ESP_LOGW(TAG, "Connection failed"); | |
253 | /* failed attempt to connect regardles of the reason */ | |
254 | ||
255 | /* otherwise: reset the config */ | |
256 | //memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
257 | } | |
258 | } | |
259 | ||
260 | /* finally: release the request bit */ | |
261 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
262 | } | |
263 | ||
264 | } /* for(;;) */ | |
265 | vTaskDelay(10 / portTICK_PERIOD_MS); | |
266 | } | |
267 | ||
268 |