10
10
namespace audio_tools {
11
11
12
12
/* *
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
14
15
* @author Phil Schatzmann
15
16
* @ingroup http
16
17
* @copyright GPLv3
@@ -25,15 +26,7 @@ class WiFiESP32 {
25
26
return false ;
26
27
}
27
28
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 ;
37
30
}
38
31
39
32
void end () {
@@ -46,41 +39,13 @@ class WiFiESP32 {
46
39
is_open = false ;
47
40
}
48
41
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; }
75
43
76
- void setPowerSave (wifi_ps_type_t powerSave){
77
- power_save = powerSave;
78
- }
44
+ bool isConnected () { return is_open; }
79
45
80
46
protected:
81
47
volatile bool is_open = false ;
82
48
esp_ip4_addr_t ip = {0 };
83
- Vector<int > numbers;
84
49
wifi_ps_type_t power_save = WIFI_PS_NONE;
85
50
86
51
bool setupWIFI (const char * ssid, const char * password) {
@@ -123,13 +88,12 @@ class WiFiESP32 {
123
88
esp_wifi_set_mode (WIFI_MODE_STA);
124
89
esp_wifi_set_ps (power_save);
125
90
126
-
127
91
wifi_config_t sta_config;
128
92
memset (&sta_config, 0 , sizeof (wifi_config_t ));
129
93
strncpy ((char *)sta_config.sta .ssid , ssid, 32 );
130
94
strncpy ((char *)sta_config.sta .password , password, 32 );
131
95
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);
133
97
134
98
// start wifi
135
99
bool rc = esp_wifi_start () == ESP_OK;
@@ -170,20 +134,21 @@ class WiFiESP32 {
170
134
}
171
135
}
172
136
173
- } static esp32_wifi ;
137
+ } static IDF_WIFI ;
174
138
175
139
/* *
176
140
* @brief URLStream using the ESP32 IDF API.
177
141
*
178
142
* 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
180
145
*
181
146
* To completely disable the certificate check, you will need to go to ESP-TLS
182
147
* in menuconfig, enable "Allow potentially insecure options" and then enable
183
148
* "Skip server certificate verification by default" (accepting risks).
184
149
*
185
150
* This is unfortunately not an option when using Arduino!
186
- *
151
+ *
187
152
* @author Phil Schatzmann
188
153
* @ingroup http
189
154
* @copyright GPLv3
@@ -194,25 +159,30 @@ class URLStreamESP32 : public AbstractURLStream {
194
159
URLStreamESP32 (const char * ssid, const char * pwd) {
195
160
setSSID (ssid);
196
161
setPassword (pwd);
197
- id = esp32_wifi.doRegister ();
198
162
_timeout = 8000 ;
199
163
}
200
164
URLStreamESP32 () : URLStreamESP32(nullptr , nullptr ) {}
201
- ~URLStreamESP32 () {
202
- end ();
203
- esp32_wifi.deRegister (id);
204
- }
165
+ ~URLStreamESP32 () { end (); }
205
166
// executes the URL request
206
167
virtual bool begin (const char * urlStr, const char * acceptMime = " " ,
207
168
MethodID action = GET, const char * reqMime = " " ,
208
169
const char * reqData = " " ) {
209
170
TRACED ();
210
171
// start wifi if necessary and possible
211
172
if (ssid != nullptr ) {
212
- if (!esp32_wifi .begin (ssid, password)) {
173
+ if (!IDF_WIFI .begin (ssid, password)) {
213
174
LOGE (" Wifi failed" );
214
175
return false ;
215
176
}
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
+ }
216
186
}
217
187
218
188
// determine wifi country
@@ -309,7 +279,9 @@ class URLStreamESP32 : public AbstractURLStream {
309
279
virtual void setPassword (const char * password) { this ->password = password; }
310
280
311
281
// / 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
+ }
313
285
314
286
size_t write (const uint8_t * data, size_t len) override {
315
287
TRACED ();
0 commit comments