arduino-audio-tools
Loading...
Searching...
No Matches
URLStreamESP32.h
Go to the documentation of this file.
1#pragma once
2
7#include "esp_http_client.h"
8#include "esp_idf_version.h"
9#include "esp_system.h"
10#include "esp_wifi.h"
11#include "nvs_flash.h"
12
13namespace audio_tools {
14
22class WiFiESP32 {
23 public:
24 bool begin(const char* ssid, const char* password) {
25 TRACEI();
26 if (is_open) return true;
27 if (!setupWIFI(ssid, password)) {
28 LOGE("setupWIFI failed");
29 return false;
30 }
31
32 return true;
33 }
34
35 void end() {
36 TRACED();
37 if (is_open) {
38 TRACEI();
41 }
42 is_open = false;
43 }
44
46
47 bool isConnected() { return is_open; }
48
49 protected:
50 volatile bool is_open = false;
53
54 bool setupWIFI(const char* ssid, const char* password) {
55 assert(ssid != nullptr);
56 assert(password != nullptr);
57 LOGI("setupWIFI: %s", ssid);
59 if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
62 ret = nvs_flash_init();
63 }
64
65 if (esp_netif_init() != ESP_OK) {
66 LOGE("esp_netif_init");
67 return false;
68 };
69
71 LOGE("esp_event_loop_create_default");
72 return false;
73 };
74
76 if (itf == nullptr) {
77 LOGE("esp_netif_create_default_wifi_sta");
78 return false;
79 };
80
86 if (esp_wifi_init(&cfg) != ESP_OK) {
87 LOGE("esp_wifi_init");
88 return false;
89 }
90
93
95 memset(&sta_config, 0, sizeof(wifi_config_t));
96 strncpy((char*)sta_config.sta.ssid, ssid, 32);
97 strncpy((char*)sta_config.sta.password, password, 32);
98 sta_config.sta.threshold.authmode = WIFI_AUTH_WPA_WPA2_PSK;
100
101 // start wifi
102 bool rc = esp_wifi_start() == ESP_OK;
103 if (!rc) {
104 LOGE("esp_wifi_start");
105 }
106 return rc;
107 }
108
110 int32_t event_id, void* event_data) {
111 WiFiESP32* self = (WiFiESP32*)arg;
112
113 if (event_base == WIFI_EVENT) {
114 switch (event_id) {
116 LOGI("WIFI_EVENT_STA_START");
118 break;
120 LOGI("WIFI_EVENT_STA_DISCONNECTED");
122 break;
123 }
124 } else if (event_base == IP_EVENT) {
125 switch (event_id) {
126 case IP_EVENT_STA_GOT_IP: {
128 self->ip = event->ip_info.ip;
129 self->is_open = true;
130 LOGI("==> Station connected with IP: " IPSTR ", GW: " IPSTR
131 ", Mask: " IPSTR ".",
132 IP2STR(&event->ip_info.ip), IP2STR(&event->ip_info.gw),
133 IP2STR(&event->ip_info.netmask));
134 break;
135 }
136 }
137 }
138 }
139
140} static IDF_WIFI;
141
142class URLStreamESP32;
144
164 public:
165 URLStreamESP32(const char* ssid, const char* pwd) {
166 setSSID(ssid);
169 }
172 // executes the URL request
173 virtual bool begin(const char* urlStr, const char* acceptMime = "",
174 MethodID action = GET, const char* reqMime = "",
175 const char* reqData = "") {
176 TRACED();
177 total_read = 0;
178 url_str = urlStr;
179 content_length = 0;
180 // start wifi if necessary and possible
181 if (ssid != nullptr) {
182 if (!IDF_WIFI.begin(ssid, password)) {
183 LOGE("Wifi failed");
184 return false;
185 }
186 if (!IDF_WIFI.isConnected()) {
187 // wait for connection
188 Serial.print("Waiting for connection ");
189 while (!IDF_WIFI.isConnected()) {
190 delay(200);
191 Serial.print(".");
192 }
193 Serial.println();
194 }
195 }
196
197 // for headers
199
200 // determine wifi country
202 memset(&cntry, 0, sizeof(wifi_country_t));
204 char text[4] = {0};
205 strncpy(text, cntry.cc, 3);
206 LOGI("wifi country: %s", text);
207
208 // fill http_config
210 memset(&http_config, 0, sizeof(http_config));
211 http_config.url = urlStr;
212 http_config.user_agent = DEFAULT_AGENT;
213 http_config.event_handler = http_event_handler;
214 http_config.buffer_size = buffer_size;
215 http_config.timeout_ms = _timeout;
216 http_config.user_data = this;
217 // for SSL certificate
218 if (pem_cert != nullptr) {
219 http_config.cert_pem = (const char*)pem_cert;
220 http_config.cert_len = pem_cert_len;
221 }
222 // for SSL (use of a bundle for certificate verification)
223 if (crt_bundle_attach != nullptr) {
224 http_config.crt_bundle_attach = crt_bundle_attach;
225 }
226
227 switch (action) {
228 case GET:
230 break;
231 case POST:
233 break;
234 case PUT:
236 break;
237 case DELETE:
239 break;
240 default:
241 LOGE("Unsupported action: %d", action);
242 break;
243 }
244
245 // Init only the first time
246 if (client_handle == nullptr) {
248 }
249
250 // process header parameters
254 for (auto it = lines.begin(); it != lines.end(); ++it) {
255 if ((*it)->active) {
256 esp_http_client_set_header(client_handle, (*it)->key.c_str(),
257 (*it)->value.c_str());
258 }
259 }
260
261 // Open http
263 LOGE("esp_http_client_open");
264 return false;
265 }
266
267 // Determine the result
270 LOGI("status_code: %d / content_length: %d", status_code, content_length);
271
272 // Process post/put data
273 StrView data(reqData);
274 if (!data.isEmpty()) {
275 write((const uint8_t*)reqData, data.length());
276 }
277
278 return status_code == 200;
279 }
280 // ends the request
285
287 int availableForWrite() override { return 1024; }
288
290 virtual void setSSID(const char* ssid) { this->ssid = ssid; }
291
293 virtual void setPassword(const char* password) { this->password = password; }
294
299
300 size_t write(const uint8_t* data, size_t len) override {
301 TRACED();
302 int written = esp_http_client_write(client_handle, (const char*)data, len);
303 if (written < 0) {
304 LOGE("esp_http_client_write: %d", written);
305 return 0;
306 }
307 return written;
308 }
309
310 size_t readBytes(uint8_t* data, size_t len) override {
311 TRACED();
312 int read = esp_http_client_read(client_handle, (char*)data, len);
313 if (read < 0) {
314 LOGE("esp_http_client_read: %d", read);
315 return 0;
316 }
317 total_read += read;
318 return read;
319 }
320
322 void addRequestHeader(const char* key, const char* value) override {
323 TRACED();
324 request.addRequestHeader(key, value);
325 }
327 const char* getReplyHeader(const char* key) override {
328 return request.getReplyHeader(key);
329 }
330
333 void setCACert(const char* cert) override {
334 int len = strlen(cert);
335 setCACert((const uint8_t*)cert, len + 1);
336 }
337
340 void setCACert(esp_err_t (*cb)(void* conf)) { crt_bundle_attach = cb; }
341
343 void setReadBufferSize(int size) { buffer_size = size; }
344
346 HttpRequest& httpRequest() override { return request; }
347
349 void setClient(Client& client) override {}
350
352 const char* urlStr() override { return (char*)url_str; }
353
355 size_t totalRead() override { return total_read; }
356
358 int contentLength() override { return content_length; }
359
361 virtual bool waitForData(int timeout) override{ return false; }
363 void setConnectionClose(bool flag) override {}
364
365 protected:
366 int id = 0;
369 bool is_power_save = false;
370 const char* ssid = nullptr;
371 const char* password = nullptr;
373 const uint8_t* pem_cert = nullptr;
375 esp_err_t (*crt_bundle_attach)(void* conf) = nullptr;
376 size_t total_read = 0;
377 const char* url_str = nullptr;
379
382 void setCACert(const uint8_t* cert, int len) {
383 pem_cert_len = len;
384 pem_cert = cert;
385 // certificate must end with traling null
386 assert(cert[len - 1] == 0);
387 }
388
390 switch (evt->event_id) {
391 case HTTP_EVENT_ERROR:
392 LOGI("HTTP_EVENT_ERROR");
393 break;
395 LOGD("HTTP_EVENT_ON_CONNECTED");
396 break;
398 LOGD("HTTP_EVENT_HEADER_SENT");
399 break;
401 LOGI("HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key,
402 evt->header_value);
403 // store reply headers
405 evt->header_value);
406 break;
408 LOGD("HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
409 break;
411 LOGI("HTTP_EVENT_ON_FINISH");
412 break;
414 LOGI("HTTP_EVENT_DISCONNECTED");
415 break;
416#if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5, 3, 7)
418 LOGI("HTTP_EVENT_REDIRECT");
419 break;
420#endif
421 }
422 return ESP_OK;
423 }
424};
425
428#if defined(USE_CONCURRENCY)
433#endif
434
436#if !defined(ARDUINO)
443#endif
444
445} // namespace audio_tools
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
MethodID
supported http methods
Definition HttpTypes.h:3
@ GET
Definition HttpTypes.h:5
@ POST
Definition HttpTypes.h:7
@ DELETE
Definition HttpTypes.h:9
@ PUT
Definition HttpTypes.h:8
#define DEFAULT_BUFFER_SIZE
Definition avr.h:20
#define assert(T)
Definition avr.h:10
Abstract Base class for all URLStream implementations.
Definition AbstractURLStream.h:17
Definition NoArduino.h:169
List< HttpHeaderLine * > & getHeaderLines()
Provides the http parameter lines.
Definition HttpHeader.h:317
HttpHeader & put(const char *key, const char *value)
Definition HttpHeader.h:86
Simple API to process get, put, post, del http requests I tried to use Arduino HttpClient,...
Definition HttpRequest.h:25
virtual HttpReplyHeader & reply()
Definition HttpRequest.h:165
void addRequestHeader(const char *key, const char *value)
Adds/Updates a request header.
Definition HttpRequest.h:199
virtual HttpRequestHeader & header()
provides access to the request header
Definition HttpRequest.h:168
const char * getReplyHeader(const char *key)
Definition HttpRequest.h:202
Buffered ICYStream with metadata callback support.
Definition ICYStream.h:18
Icecast/Shoutcast audio stream that separates ICY metadata from audio bytes.
Definition ICYStreamT.h:42
Double linked list.
Definition List.h:18
Iterator begin()
Definition List.h:273
Iterator end()
Definition List.h:278
int _timeout
Definition NoArduino.h:139
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:28
virtual int length()
Definition StrView.h:383
virtual bool isEmpty()
checks if the string is empty
Definition StrView.h:386
URLStream implementation for the ESP32 based on a separate FreeRTOS task: the.
Definition URLStreamBufferedT.h:162
URLStream using the ESP32 IDF API.
Definition URLStreamESP32.h:163
size_t totalRead() override
Total amout of data that was consumed so far.
Definition URLStreamESP32.h:355
size_t total_read
Definition URLStreamESP32.h:376
virtual void setPowerSave(bool ps)
Sets the power save mode (default false)!
Definition URLStreamESP32.h:296
URLStreamESP32()
Definition URLStreamESP32.h:170
URLStreamESP32(const char *ssid, const char *pwd)
Definition URLStreamESP32.h:165
HttpRequest request
Definition URLStreamESP32.h:367
~URLStreamESP32()
Definition URLStreamESP32.h:171
void setCACert(esp_err_t(*cb)(void *conf))
Definition URLStreamESP32.h:340
esp_http_client_handle_t client_handle
Definition URLStreamESP32.h:368
size_t readBytes(uint8_t *data, size_t len) override
Definition URLStreamESP32.h:310
virtual void setSSID(const char *ssid)
Sets the ssid that will be used for logging in (when calling begin)
Definition URLStreamESP32.h:290
const char * ssid
Definition URLStreamESP32.h:370
virtual void setPassword(const char *password)
Sets the password that will be used for logging in (when calling begin)
Definition URLStreamESP32.h:293
int pem_cert_len
Definition URLStreamESP32.h:374
esp_err_t(* crt_bundle_attach)(void *conf)
Definition URLStreamESP32.h:375
virtual bool begin(const char *urlStr, const char *acceptMime="", MethodID action=GET, const char *reqMime="", const char *reqData="")
Definition URLStreamESP32.h:173
int content_length
Definition URLStreamESP32.h:378
virtual bool waitForData(int timeout) override
Not used.
Definition URLStreamESP32.h:361
HttpRequest & httpRequest() override
Used for request and reply header parameters.
Definition URLStreamESP32.h:346
size_t write(const uint8_t *data, size_t len) override
Definition URLStreamESP32.h:300
static esp_err_t http_event_handler(esp_http_client_event_t *evt)
Definition URLStreamESP32.h:389
void setCACert(const uint8_t *cert, int len)
Definition URLStreamESP32.h:382
int availableForWrite() override
Writes are not supported.
Definition URLStreamESP32.h:287
const char * url_str
Definition URLStreamESP32.h:377
void addRequestHeader(const char *key, const char *value) override
Adds/Updates a request header.
Definition URLStreamESP32.h:322
const char * password
Definition URLStreamESP32.h:371
const char * urlStr() override
Provides the url as string.
Definition URLStreamESP32.h:352
void setConnectionClose(bool flag) override
Not used.
Definition URLStreamESP32.h:363
void setCACert(const char *cert) override
Definition URLStreamESP32.h:333
int contentLength() override
Provides the reported data size from the http reply.
Definition URLStreamESP32.h:358
void setClient(Client &client) override
Does nothing.
Definition URLStreamESP32.h:349
const uint8_t * pem_cert
Definition URLStreamESP32.h:373
virtual void end() override
Definition URLStreamESP32.h:281
void setReadBufferSize(int size)
Defines the read buffer size.
Definition URLStreamESP32.h:343
int buffer_size
Definition URLStreamESP32.h:372
bool is_power_save
Definition URLStreamESP32.h:369
const char * getReplyHeader(const char *key) override
Provides a header entry.
Definition URLStreamESP32.h:327
Represents the content of a URL as Stream. We use the WiFi.h API. If you run into performance issues,...
Definition URLStream.h:26
Login to Wifi using the ESP32 IDF functionality. This can be accessed with the global object IDF_WIFI...
Definition URLStreamESP32.h:22
esp_ip4_addr_t ip
Definition URLStreamESP32.h:51
bool begin(const char *ssid, const char *password)
Definition URLStreamESP32.h:24
volatile bool is_open
Definition URLStreamESP32.h:50
void setPowerSave(wifi_ps_type_t powerSave)
Definition URLStreamESP32.h:45
bool isConnected()
Definition URLStreamESP32.h:47
static void wifi_sta_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
Definition URLStreamESP32.h:109
bool setupWIFI(const char *ssid, const char *password)
Definition URLStreamESP32.h:54
void end()
Definition URLStreamESP32.h:35
wifi_ps_type_t power_save
Definition URLStreamESP32.h:52
#define URL_CLIENT_TIMEOUT
Definition esp8266.h:23
class audio_tools::WiFiESP32 IDF_WIFI
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
static const char * DEFAULT_AGENT
Definition HttpHeader.h:27
URLStreamBufferedT< URLStreamESP32 > URLStreamBufferedESP32
Buffered URLStream for ESP32 platform.
Definition URLStreamESP32.h:430
void delay(unsigned long ms)
Definition Time.h:23
static HardwareSerial Serial
Definition NoArduino.h:186
static URLStreamESP32 * actualURLStreamESP32
Definition URLStreamESP32.h:143
static const char * CONTENT_TYPE
Definition HttpHeader.h:16
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
URLStreamBufferedT< ICYStreamESP32 > ICYStreamBufferedESP32
Buffered ICYStream for ESP32 platform.
Definition URLStreamESP32.h:432
static const char * ACCEPT
Definition HttpHeader.h:23