main/task_wifi.c

Sat, 15 Apr 2023 13:29:40 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 15 Apr 2023 13:29:40 +0200
changeset 29
551a53b31373
parent 24
74609f70411e
child 35
9827c5a08c63
permissions
-rw-r--r--

Final release and installed for production.

4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /**
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * @file task_wifi.c
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 * @brief WiFi task. Connects to a known Access Point.
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 */
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 #include "config.h"
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 static const char *TAG = "task_wifi";
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 #define ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 #define ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 SemaphoreHandle_t xSemaphoreWiFi = NULL; ///< Semaphore WiFi task.
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 EventGroupHandle_t xEventGroupWifi; ///< Events WiFi task.
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
17 esp_event_handler_instance_t instance_any_id; ///< WiFi event handler.
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
18 esp_event_handler_instance_t instance_got_ip; ///< IP event handler.
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
19
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 WIFI_State *wifi_state = NULL; ///< Public state for other tasks.
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
21 esp_netif_t *sta_netif = NULL; ///< Station interface.
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1; ///< When set, means a client requested to disconnect from currently connected AP.
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2; ///< When set, means a client requested to connect to an access point.
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 const int TASK_WIFI_HAS_IP = BIT3; ///< Indicate that we have an IP address
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 const int TASK_WIFI_STA_FAILED = BIT5; ///< Indicate that we could not get a connection to AP as station.
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 const int TASK_WIFI_STA_DISCONNECTED = BIT6; ///< Indicate that we are disconnected from an ap station.
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30 const int TASK_WIFI_STA_CONNECTED = BIT7; ///< Indicate that we are connected to AP as station, flip of BIT6.
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
33 static void init_wifi(void);
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
34 void wifi_connect(void);
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
35
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
36
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 /****************************************************************************/
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40 bool ready_WiFi(void)
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 {
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
42 if ((xEventGroupGetBits(xEventGroupWifi) & (TASK_WIFI_STA_CONNECTED | TASK_WIFI_HAS_IP)) == (TASK_WIFI_STA_CONNECTED | TASK_WIFI_HAS_IP))
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 return true;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 return false;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49 void request_WiFi(bool connect)
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50 {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51 if (connect)
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53 else
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61 switch (event_id) {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63 case WIFI_EVENT_STA_START:
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64 ESP_LOGI(TAG, "Event wifi START");
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65 // Set the hostname for the dhcp client.
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
66 #ifdef CONFIG_CODE_PRODUCTION
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67 ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, "balkon"));
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68 #endif
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69 #ifdef CONFIG_CODE_TESTING
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
70 ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, "wemos"));
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
71 #endif
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72 break;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
73
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
74 case WIFI_EVENT_STA_CONNECTED: {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75 wifi_ap_record_t ap_info;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76 esp_wifi_sta_get_ap_info(&ap_info);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77 ESP_LOGI(TAG, "Event STA connected rssi=%d", ap_info.rssi);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78 if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
79 wifi_state->STA_connected = true;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
80 wifi_state->STA_rssi = ap_info.rssi;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81 xSemaphoreGive(xSemaphoreWiFi);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82 } else {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_CONNECTED");
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87 break;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
88 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
89
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
90 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
91 wifi_event_sta_disconnected_t* disconnected = (wifi_event_sta_disconnected_t*) event_data;
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
92 ESP_LOGI(TAG, "Event STA disconnected, reason: %d", disconnected->reason);
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
93
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
94 /*
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
95 * If it's not a normal request to disconnect, make sure the mqtt
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
96 * connection will be removed.
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
97 */
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
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);
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
100
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
101 /*
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
102 * Error conditions.
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
103 */
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
104 if (disconnected->reason == 2) {
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
105 ESP_LOGW(TAG, "Auth Expire: try to recover");
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
106 wifi_connect();
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
107 } else if (disconnected->reason == 39) {
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
108 ESP_LOGW(TAG, "Timeout: try to recover");
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
109 ESP_ERROR_CHECK(esp_wifi_connect());
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
110 }
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
111 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
112 wifi_state->STA_connected = false;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
113 wifi_state->STA_online = false;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
114 xSemaphoreGive(xSemaphoreWiFi);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
115 } else {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
116 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_DISCONNECTED");
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
117 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
118 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
119 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
120 break;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
121 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
123 default:
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
124 ESP_LOGW(TAG, "Unknown WiFi event %d", (int)event_id);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
125 break;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
126 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
127 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
128
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
129
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
130
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131 static void got_ip_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
132 {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
133 switch (event_id) {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
135 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
136 //ESP_LOGE(TAG, "got_ip_event_handler()");
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
137 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138 ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
139 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
140 wifi_state->STA_online = true;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
141 snprintf(wifi_state->STA_ip, 16, IPSTR, IP2STR(&event->ip_info.ip));
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
142 snprintf(wifi_state->STA_nm, 16, IPSTR, IP2STR(&event->ip_info.netmask));
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143 snprintf(wifi_state->STA_gw, 16, IPSTR, IP2STR(&event->ip_info.gw));
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
144 xSemaphoreGive(xSemaphoreWiFi);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
145 } else {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
146 ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_GOT_IP");
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
147 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
148 break;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
149
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
150 case IP_EVENT_STA_LOST_IP:
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
151 ESP_LOGW(TAG, "Lost IP address");
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
152 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
153 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
154 wifi_state->STA_ip[0] = '\0';
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
155 wifi_state->STA_nm[0] = '\0';
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
156 wifi_state->STA_gw[0] = '\0';
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
157 wifi_state->STA_online = false;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
158 xSemaphoreGive(xSemaphoreWiFi);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
159 } else {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
160 ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_LOST_IP");
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
161 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
162 break;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
163
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
164 default:
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
165 ESP_LOGW(TAG, "Unknown IP event %d", (int)event_id);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
166 break;
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
167 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
168 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
169
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
170
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
171 static void init_wifi(void)
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
172 {
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
173 ESP_ERROR_CHECK(esp_event_loop_create_default());
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
174 xSemaphoreWiFi = xSemaphoreCreateMutex();
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
175
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
176 /* initialize the tcp stack */
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
177 ESP_ERROR_CHECK(esp_netif_init());
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
178 sta_netif = esp_netif_create_default_wifi_sta();
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
179 assert(sta_netif);
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
180
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
181 wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
182 ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
183
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
184 ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) );
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
185 ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) );
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
186
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
187 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
188 ESP_ERROR_CHECK(esp_wifi_start());
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
189
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
190 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
191 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
192 }
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
193
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
194
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
195 void wifi_connect(void)
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
196 {
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
197 ESP_LOGI(TAG, "wifi_connect() start");
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
198 wifi_config_t wifi_Config = { ///< Current STA configuration.
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
199 .sta = {
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
200 .ssid = ESP_WIFI_SSID,
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
201 .password = ESP_WIFI_PASS,
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
202 .threshold.authmode = WIFI_AUTH_WPA2_PSK,
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
203 },
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
204 };
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
205 // .threshold.authmode = WIFI_AUTH_WPA2_PSK,
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
206
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
207 ESP_ERROR_CHECK(esp_wifi_disconnect());
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
208 ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_Config) );
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
209
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
210 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED);
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
211 esp_err_t wifierror = esp_wifi_connect();
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
212
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
213 if (wifierror != ESP_OK) {
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
214 ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x %s", (int)wifierror, esp_err_to_name(wifierror));
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
215 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED);
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
216 } else {
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
217 ESP_LOGI(TAG, "Connected Ok");
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
218 }
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
219 ESP_LOGI(TAG, "wifi_connect() done");
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
220 }
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
221
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
222
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
223 void task_wifi( void * pvParameters )
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
224 {
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
225 // uint64_t starttime = 0;
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
226
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
227 ESP_LOGI(TAG, "Starting WiFi task");
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
228 esp_log_level_set("wifi", ESP_LOG_ERROR);
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
229
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
230 /*
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
231 * memory allocation of objects used by the task
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
232 */
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
233 wifi_state = malloc(sizeof(WIFI_State));
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
234 memset(wifi_state, 0x00, sizeof(WIFI_State));
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
235
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
236 /*
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
237 * event group for the wifi driver
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
238 */
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
239 xEventGroupWifi = xEventGroupCreate();
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
240
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
241 /*
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
242 * init wifi as station
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
243 */
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
244 init_wifi();
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
245 EventBits_t uxBits;
23
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
246 int8_t tx_level;
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
247 ESP_ERROR_CHECK(esp_wifi_get_max_tx_power(&tx_level));
2cc30d828d6e Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.
Michiel Broek <mbroek@mbse.eu>
parents: 22
diff changeset
248 ESP_LOGI(TAG, "Startup completed, maximum transmit power %d dBm", tx_level / 4);
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
249
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
250 for(;;) {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
251
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
252 /*
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
253 * Actions that can trigger: request a connection or a disconnection
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
254 */
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
255 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT,
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
256 pdFALSE, pdFALSE, portMAX_DELAY );
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
257
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
258 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
259 /*
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
260 * user requested a disconnect, this will in effect disconnect the wifi
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
261 */
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
262 ESP_LOGI(TAG, "Request STA disconnect");
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
263 ESP_ERROR_CHECK(esp_wifi_disconnect());
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
264 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY );
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
265
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
266 /*
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
267 * Finally: release the request bit
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
268 */
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
269 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT);
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
270 // ESP_LOGI(TAG, "Connection time %llu", (esp_timer_get_time() / 1000) - starttime);
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
271
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
272 } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
273
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
274 ESP_LOGI(TAG, "Request STA connect `%s' `%s'", ESP_WIFI_SSID, ESP_WIFI_PASS);
24
74609f70411e Reduced I2C master speed from 400 to 100 Khz. Hope to get power-on init for BMP280 more reliable. Added warnings for I2C devices that are missing. Some logmessages reduced to debug log messages. Added extra 10 mSec delays before read shunt voltage in the INA219 task. Removed and reduced log levels in the MQTT task. Show received data events. Remove WiFi total time debug logging, it's ok now.
Michiel Broek <mbroek@mbse.eu>
parents: 23
diff changeset
275 // starttime = esp_timer_get_time() / 1000;
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
276 wifi_connect();
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
277
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
278 /*
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
279 * 3 scenarios here: connection is successful and TASK_WIFI_STA_CONNECTED will be posted
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
280 * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code.
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
281 * Or, option 3, the 5 seconds timeout is reached. This happens when the AP is not in range.
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
282 * Note that the reason code is not exploited. For all intent and purposes a failure is a failure.
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
283 */
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
284 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 5000 / portTICK_PERIOD_MS);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
285
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
286 if (uxBits & (TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED)) {
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
287 /*
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
288 * only save the config if the connection was successful!
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
289 */
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
290 if (uxBits & TASK_WIFI_STA_FAILED) {
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
291 ESP_LOGI(TAG, "No AP found");
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
292 vTaskDelay(3000 / portTICK_PERIOD_MS);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
293 ESP_LOGW(TAG, "Connection failed");
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
294 /* failed attempt to connect regardles of the reason */
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
295 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
296 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
297
22
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
298 /*
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
299 * Finally: release the request bit
9c0bcc91fe1a Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
300 */
4
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
301 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
302 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
303
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
304 } /* for(;;) */
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
305 }
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
306
d0155c16e992 Added Wifi task.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
307

mercurial