1010namespace audio_tools {
1111
1212/* *
13- * @brief Login to Wifi using the ESP32 IDF functionality
13+ * @brief Login to Wifi using the ESP32 IDF functionality. This can be
14+ * accessed with the global object IDF_WIFI
1415 * @author Phil Schatzmann
1516 * @ingroup http
1617 * @copyright GPLv3
@@ -25,15 +26,7 @@ class WiFiESP32 {
2526 return false ;
2627 }
2728
28- // wait for connection
29- Serial.print (" Waiting for connection " );
30- while (!is_open) {
31- delay (200 );
32- Serial.print (" ." );
33- }
34- Serial.println ();
35-
36- return is_open;
29+ return true ;
3730 }
3831
3932 void end () {
@@ -46,41 +39,13 @@ class WiFiESP32 {
4639 is_open = false ;
4740 }
4841
49- // / Register a new URLStream
50- int doRegister () {
51- int count = numbers.size ();
52- int next_number;
53- if (count == 0 ) {
54- // provide first number
55- next_number = 1 ;
56- } else {
57- next_number = numbers[count - 1 ];
58- }
59- numbers.push_back (next_number);
60- return next_number;
61- }
62-
63- // / Deregister the URLStream: when all are removed we close the wifi
64- void deRegister (int id) {
65- for (int j = 0 ; j < numbers.size (); j++) {
66- if (numbers[j] == id) {
67- numbers.erase (j);
68- break ;
69- }
70- }
71- if (numbers.size () == 0 ) {
72- end ();
73- }
74- }
42+ void setPowerSave (wifi_ps_type_t powerSave) { power_save = powerSave; }
7543
76- void setPowerSave (wifi_ps_type_t powerSave){
77- power_save = powerSave;
78- }
44+ bool isConnected () { return is_open; }
7945
8046 protected:
8147 volatile bool is_open = false ;
8248 esp_ip4_addr_t ip = {0 };
83- Vector<int > numbers;
8449 wifi_ps_type_t power_save = WIFI_PS_NONE;
8550
8651 bool setupWIFI (const char * ssid, const char * password) {
@@ -123,13 +88,12 @@ class WiFiESP32 {
12388 esp_wifi_set_mode (WIFI_MODE_STA);
12489 esp_wifi_set_ps (power_save);
12590
126-
12791 wifi_config_t sta_config;
12892 memset (&sta_config, 0 , sizeof (wifi_config_t ));
12993 strncpy ((char *)sta_config.sta .ssid , ssid, 32 );
13094 strncpy ((char *)sta_config.sta .password , password, 32 );
13195 sta_config.sta .threshold .authmode = WIFI_AUTH_WPA_WPA2_PSK;
132- esp_wifi_set_config (WIFI_IF_STA, &sta_config);
96+ esp_wifi_set_config (WIFI_IF_STA, &sta_config);
13397
13498 // start wifi
13599 bool rc = esp_wifi_start () == ESP_OK;
@@ -170,20 +134,21 @@ class WiFiESP32 {
170134 }
171135 }
172136
173- } static esp32_wifi ;
137+ } static IDF_WIFI ;
174138
175139/* *
176140 * @brief URLStream using the ESP32 IDF API.
177141 *
178142 * For Https you need to provide the certificate.
179- * Execute: openssl s_client -showcerts -connect www.howsmyssl.com:443 </dev/null
143+ * Execute: openssl s_client -showcerts -connect www.howsmyssl.com:443
144+ * </dev/null
180145 *
181146 * To completely disable the certificate check, you will need to go to ESP-TLS
182147 * in menuconfig, enable "Allow potentially insecure options" and then enable
183148 * "Skip server certificate verification by default" (accepting risks).
184149 *
185150 * This is unfortunately not an option when using Arduino!
186- *
151+ *
187152 * @author Phil Schatzmann
188153 * @ingroup http
189154 * @copyright GPLv3
@@ -194,25 +159,30 @@ class URLStreamESP32 : public AbstractURLStream {
194159 URLStreamESP32 (const char * ssid, const char * pwd) {
195160 setSSID (ssid);
196161 setPassword (pwd);
197- id = esp32_wifi.doRegister ();
198162 _timeout = 8000 ;
199163 }
200164 URLStreamESP32 () : URLStreamESP32(nullptr , nullptr ) {}
201- ~URLStreamESP32 () {
202- end ();
203- esp32_wifi.deRegister (id);
204- }
165+ ~URLStreamESP32 () { end (); }
205166 // executes the URL request
206167 virtual bool begin (const char * urlStr, const char * acceptMime = " " ,
207168 MethodID action = GET, const char * reqMime = " " ,
208169 const char * reqData = " " ) {
209170 TRACED ();
210171 // start wifi if necessary and possible
211172 if (ssid != nullptr ) {
212- if (!esp32_wifi .begin (ssid, password)) {
173+ if (!IDF_WIFI .begin (ssid, password)) {
213174 LOGE (" Wifi failed" );
214175 return false ;
215176 }
177+ if (!IDF_WIFI.isConnected ()) {
178+ // wait for connection
179+ Serial.print (" Waiting for connection " );
180+ while (!IDF_WIFI.isConnected ()) {
181+ delay (200 );
182+ Serial.print (" ." );
183+ }
184+ Serial.println ();
185+ }
216186 }
217187
218188 // determine wifi country
@@ -309,7 +279,9 @@ class URLStreamESP32 : public AbstractURLStream {
309279 virtual void setPassword (const char * password) { this ->password = password; }
310280
311281 // / Sets the power save mode (default false)!
312- virtual void setPowerSave (bool ps) { esp32_wifi.setPowerSave (ps?WIFI_PS_MAX_MODEM: WIFI_PS_NONE); }
282+ virtual void setPowerSave (bool ps) {
283+ IDF_WIFI.setPowerSave (ps ? WIFI_PS_MAX_MODEM : WIFI_PS_NONE);
284+ }
313285
314286 size_t write (const uint8_t * data, size_t len) override {
315287 TRACED ();
0 commit comments