arduino-audio-tools
Loading...
Searching...
No Matches
WiFiESP32.h
Go to the documentation of this file.
1#pragma once
2
3#include "esp_http_client.h"
4#include "esp_idf_version.h"
5#include "esp_system.h"
6#include "esp_wifi.h"
7#include "nvs_flash.h"
8
9namespace audio_tools {
10
18class WiFiESP32 {
19 public:
20 bool begin(const char* ssid, const char* password) {
21 TRACEI();
22 if (is_open) return true;
23 if (!setupWIFI(ssid, password)) {
24 LOGE("setupWIFI failed");
25 return false;
26 }
27
28 return true;
29 }
30
31 void end() {
32 TRACED();
33 if (is_open) {
34 TRACEI();
37 }
38 is_open = false;
39 }
40
42
43 bool isConnected() { return is_open; }
44
45 protected:
46 volatile bool is_open = false;
49
50 bool setupWIFI(const char* ssid, const char* password) {
51 assert(ssid != nullptr);
52 assert(password != nullptr);
53 LOGI("setupWIFI: %s", ssid);
55 if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
58 ret = nvs_flash_init();
59 }
60
61 if (esp_netif_init() != ESP_OK) {
62 LOGE("esp_netif_init");
63 return false;
64 };
65
67 LOGE("esp_event_loop_create_default");
68 return false;
69 };
70
72 if (itf == nullptr) {
73 LOGE("esp_netif_create_default_wifi_sta");
74 return false;
75 };
76
82 if (esp_wifi_init(&cfg) != ESP_OK) {
83 LOGE("esp_wifi_init");
84 return false;
85 }
86
89
91 memset(&sta_config, 0, sizeof(wifi_config_t));
92 strncpy((char*)sta_config.sta.ssid, ssid, 32);
93 strncpy((char*)sta_config.sta.password, password, 32);
94 sta_config.sta.threshold.authmode = WIFI_AUTH_WPA_WPA2_PSK;
96
97 // start wifi
98 bool rc = esp_wifi_start() == ESP_OK;
99 if (!rc) {
100 LOGE("esp_wifi_start");
101 }
102 return rc;
103 }
104
106 int32_t event_id, void* event_data) {
107 WiFiESP32* self = (WiFiESP32*)arg;
108
109 if (event_base == WIFI_EVENT) {
110 switch (event_id) {
112 LOGI("WIFI_EVENT_STA_START");
114 break;
116 LOGI("WIFI_EVENT_STA_DISCONNECTED");
118 break;
119 }
120 } else if (event_base == IP_EVENT) {
121 switch (event_id) {
122 case IP_EVENT_STA_GOT_IP: {
124 self->ip = event->ip_info.ip;
125 self->is_open = true;
126 LOGI("==> Station connected with IP: " IPSTR ", GW: " IPSTR
127 ", Mask: " IPSTR ".",
128 IP2STR(&event->ip_info.ip), IP2STR(&event->ip_info.gw),
129 IP2STR(&event->ip_info.netmask));
130 break;
131 }
132 }
133 }
134 }
135
136} static IDF_WIFI;
137
138} // namespace audio_tools
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define assert(T)
Definition avr.h:10
Login to Wifi using the ESP32 IDF functionality. This can be accessed with the global object IDF_WIFI...
Definition WiFiESP32.h:18
esp_ip4_addr_t ip
Definition WiFiESP32.h:47
bool begin(const char *ssid, const char *password)
Definition WiFiESP32.h:20
volatile bool is_open
Definition WiFiESP32.h:46
void setPowerSave(wifi_ps_type_t powerSave)
Definition WiFiESP32.h:41
bool isConnected()
Definition WiFiESP32.h:43
static void wifi_sta_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
Definition WiFiESP32.h:105
bool setupWIFI(const char *ssid, const char *password)
Definition WiFiESP32.h:50
void end()
Definition WiFiESP32.h:31
wifi_ps_type_t power_save
Definition WiFiESP32.h:48
class audio_tools::WiFiESP32 IDF_WIFI
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508