8000 Changes for #140 · shekharsorot/esp32-snippets@6153b32 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6153b32

Browse files
committed
Changes for nkolban#140
1 parent 36d7e22 commit 6153b32

File tree

2 files changed

+57
-79
lines changed

2 files changed

+57
-79
lines changed

cpp_utils/WiFi.cpp

Lines changed: 55 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ WiFi::WiFi()
5353
, m_pWifiEventHandler(nullptr)
5454
{
5555
m_eventLoopStarted = false;
56-
::nvs_flash_init();
57-
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
58-
esp_wifi_init(&config);
59-
::tcpip_adapter_init();
56+
m_initCalled = false;
6057
m_pWifiEventHandler = new WiFiEventHandler();
6158
} // WiFi
6259

@@ -100,6 +97,7 @@ void WiFi::addDNSServer(const char* ip) {
10097

10198
void WiFi::addDNSServer(ip_addr_t ip) {
10299
ESP_LOGD(LOG_TAG, "Setting DNS[%d] to %d.%d.%d.%d", m_dnsCount, ((uint8_t*)(&ip))[0], ((uint8_t*)(&ip))[1], ((uint8_t*)(&ip))[2], ((uint8_t*)(&ip))[3]);
100+
init();
103101
::dns_setserver(m_dnsCount, &ip);
104102
m_dnsCount++;
105103
m_dnsCount %= 2;
@@ -137,6 +135,7 @@ void WiFi::setDNSServer(int numdns, const char* ip) {
137135

138136
void WiFi::setDNSServer(int numdns, ip_addr_t ip) {
139137
ESP_LOGD(LOG_TAG, "Setting DNS[%d] to %d.%d.%d.%d", m_dnsCount, ((uint8_t*)(&ip))[0], ((uint8_t*)(&ip))[1], ((uint8_t*)(&ip))[2], ((uint8_t*)(&ip))[3]);
138+
init();
140139
::dns_setserver(numdns, &ip);
141140
} // setDNSServer
142141

@@ -154,6 +153,8 @@ void WiFi::setDNSServer(int numdns, ip_addr_t ip) {
154153
void WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){
155154
ESP_LOGD(LOG_TAG, ">> connectAP");
156155

156+
init();
157+
157158
if (ip != 0 && gw != 0 && netmask != 0) {
158159
::tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA); // Don't run a DHCP client
159160

@@ -165,27 +166,7 @@ void WiFi::connectAP(const std::string& ssid, const std::string& password, bool
165166
::tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
166167
}
167168

168-
// If the event loop has already started then change the callback else
169-
// start the event loop.
170-
if (m_eventLoopStarted) {
171-
esp_event_loop_set_cb(WiFi::eventHandler, this); // Returns the old handler.
172-
} else {
173-
esp_err_t errRc = ::esp_event_loop_init(WiFi::eventHandler, this); // Initialze the event handler.
174-
if (errRc != ESP_OK) {
175-
ESP_LOGE(LOG_TAG, "esp_event_loop_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
176-
abort();
177-
}
178-
m_eventLoopStarted = true;
179-
}
180-
// Now, one way or another, the event handler is WiFi::eventHandler.
181-
182-
esp_err_t errRc = ::esp_wifi_set_storage(WIFI_STORAGE_RAM);
183-
if (errRc != ESP_OK) {
184-
ESP_LOGE(LOG_TAG, "esp_wifi_set_storage: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
185-
abort();
186-
}
187-
188-
errRc = ::esp_wifi_set_mode(WIFI_MODE_STA);
169+
esp_err_t errRc = ::esp_wifi_set_mode(WIFI_MODE_STA);
189170
if (errRc != ESP_OK) {
190171
ESP_LOGE(LOG_TAG, "esp_wifi_set_mode: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
191172
abort();
@@ -217,6 +198,7 @@ void WiFi::connectAP(const std::string& ssid, const std::string& password, bool
217198
} // connectAP
218199

219200

201+
220202
/**
221203
* @brief Dump diagnostics to the log.
222204
*/
@@ -258,6 +240,7 @@ void WiFi::dump() {
258240
* @return The AP IP Info.
259241
*/
260242
tcpip_adapter_ip_info_t WiFi::getApIpInfo() {
243+
//init();
261244
tcpip_adapter_ip_info_t ipInfo;
262245
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ipInfo);
263246
return ipInfo;
@@ -270,6 +253,7 @@ tcpip_adapter_ip_info_t WiFi::getApIpInfo() {
270253
*/
271254
std::string WiFi::getApMac() {
272255
uint8_t mac[6];
256+
//init();
273257
esp_wifi_get_mac(WIFI_IF_AP, mac);
274258
auto mac_str = (char*) malloc(18);
275259
sprintf(mac_str, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
@@ -283,6 +267,7 @@ std::string WiFi::getApMac() {
283267
*/
284268
std::string WiFi::getApSSID() {
285269
wifi_config_t conf;
270+
//init();
286271
esp_wifi_get_config(WIFI_IF_AP, &conf);
287272
return std::string((char *)conf.sta.ssid);
288273
} // getApSSID
@@ -372,21 +357,9 @@ std::string WiFi::getStaSSID() {
372357

373358

374359
/**
375-
* @brief Perform a WiFi scan looking for access points.
376-
*
377-
* An access point scan is performed and a vector of WiFi access point records
378-
* is built and returned with one record per found scan instance. The scan is
379-
* performed in a blocking fashion and will not return until the set of scanned
380-
* access points has been built.
381-
*
382-
* @return A vector of WiFiAPRecord instances.
360+
* @brief Initialize WiFi.
383361
*/
384-
std::vector<WiFiAPRecord> WiFi::scan() {
385-
ESP_LOGD(LOG_TAG, ">> scan");
386-
std::vector<WiFiAPRecord> apRecords;
387-
388-
::nvs_flash_init();
389-
::tcpip_adapter_init();
362+
/* PRIVATE */ void WiFi::init() {
390363

391364
// If we have already started the event loop, then change the handler otherwise
392365
// start the event loop.
@@ -402,20 +375,44 @@ std::vector<WiFiAPRecord> WiFi::scan() {
402375
}
403376
// Now, one way or another, the event handler is WiFi::eventHandler.
404377

405-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
406-
esp_err_t errRc = ::esp_wifi_init(&cfg);
407-
if (errRc != ESP_OK) {
408-
ESP_LOGE(LOG_TAG, "esp_wifi_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
409-
abort();
410-
}
378+
if (!m_initCalled) {
379+
::nvs_flash_init();
380+
::tcpip_adapter_init();
411381

412-
errRc = ::esp_wifi_set_storage(WIFI_STORAGE_RAM);
413-
if (errRc != ESP_OK) {
414-
ESP_LOGE(LOG_TAG, "esp_wifi_set_storage: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
415-
abort();
382+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
383+
esp_err_t errRc = ::esp_wifi_init(&cfg);
384+
if (errRc != ESP_OK) {
385+
ESP_LOGE(LOG_TAG, "esp_wifi_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
386+
abort();
387+
}
388+
389+
errRc = ::esp_wifi_set_storage(WIFI_STORAGE_RAM);
390+
if (errRc != ESP_OK) {
391+
ESP_LOGE(LOG_TAG, "esp_wifi_set_storage: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
392+
abort();
393+
}
416394
}
395+
m_initCalled = true;
396+
} // init
397+
417398

418-
errRc = ::esp_wifi_set_mode(WIFI_MODE_STA);
399+
/**
400+
* @brief Perform a WiFi scan looking for access points.
401+
*
402+
* An access point scan is performed and a vector of WiFi access point records
403+
* is built and returned with one record per found scan instance. The scan is
404+
* performed in a blocking fashion and will not return until the set of scanned
405+
* access points has been built.
406+
*
407+
* @return A vector of WiFiAPRecord instances.
408+
*/
409+
std::vector<WiFiAPRecord> WiFi::scan() {
410+
ESP_LOGD(LOG_TAG, ">> scan");
411+
std::vector<WiFiAPRecord> apRecords;
412+
413+
init();
414+
415+
esp_err_t errRc = ::esp_wifi_set_mode(WIFI_MODE_STA);
419416
if (errRc != ESP_OK) {
420417
ESP_LOGE(LOG_TAG, "esp_wifi_set_mode: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
421418
abort();
@@ -478,41 +475,16 @@ std::vector<WiFiAPRecord> WiFi::scan() {
478475
*/
479476
void WiFi::startAP(const std::string& ssid, const std::string& password) {
480477
ESP_LOGD(LOG_TAG, ">> startAP: ssid: %s", ssid.c_str());
481-
::nvs_flash_init();
482-
::tcpip_adapter_init();
483-
484-
// If we have already started the event loop, then change the handler otherwise
485-
// start the event loop.
486-
487-
if (m_eventLoopStarted) {
488-
esp_event_loop_set_cb(WiFi::eventHandler, this); // Returns the old handler.
489-
} else {
490-
esp_err_t errRc = ::esp_event_loop_init(WiFi::eventHandler, this); // Initialze the event handler.
491-
if (errRc != ESP_OK) {
492-
ESP_LOGE(LOG_TAG, "esp_event_loop_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
493-
abort();
494-
}
495-
m_eventLoopStarted = true;
496-
}
497-
// Now, one way or another, the event handler is WiFi::eventHandler.
498478

479+
init();
499480

500-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
501-
esp_err_t errRc = ::esp_wifi_init(&cfg);
502-
if (errRc != ESP_OK) {
503-
ESP_LOGE(LOG_TAG, "esp_wifi_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
504-
abort();
505-
}
506-
errRc = ::esp_wifi_set_storage(WIFI_STORAGE_RAM);
507-
if (errRc != ESP_OK) {
508-
ESP_LOGE(LOG_TAG, "esp_wifi_set_storage: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
509-
abort();
510-
}
511-
errRc = ::esp_wifi_set_mode(WIFI_MODE_AP);
481+
esp_err_t errRc = ::esp_wifi_set_mode(WIFI_MODE_AP);
512482
if (errRc != ESP_OK) {
513483
ESP_LOGE(LOG_TAG, "esp_wifi_set_mode: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
514484
abort();
515485
}
486+
487+
// Build the apConfig structure.
516488
wifi_config_t apConfig;
517489
::memset(&apConfig, 0, sizeof(apConfig));
518490
::memcpy(apConfig.ap.ssid, ssid.data(), ssid.size());
@@ -523,11 +495,13 @@ void WiFi::startAP(const std::string& ssid, const std::string& password) {
523495
apConfig.ap.ssid_hidden = 0;
524496
apConfig.ap.max_connection = 4;
525497
apConfig.ap.beacon_interval = 100;
498+
526499
errRc = ::esp_wifi_set_config(WIFI_IF_AP, &apConfig);
527500
if (errRc != ESP_OK) {
528501
ESP_LOGE(LOG_TAG, "esp_wifi_set_config: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
529502
abort();
530503
}
504+
531505
errRc = ::esp_wifi_start();
532506
if (errRc != ESP_OK) {
533507
ESP_LOGE(LOG_TAG, "esp_wifi_start: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
@@ -595,6 +569,8 @@ void WiFi::setIPInfo(const char* ip, const char* gw, const char* netmask) {
595569
* @param [in] netmask Our TCP/IP netmask value.
596570
*/
597571
void WiFi::setIPInfo(uint32_t ip, uint32_t gw, uint32_t netmask) {
572+
init();
573+
598574
this->ip = ip;
599575
this->gw = gw;
600576
this->netmask = netmask;

cpp_utils/WiFi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,14 @@ class WiFiAPRecord {
106106
class WiFi {
107107
private:
108108
static esp_err_t eventHandler(void* ctx, system_event_t* event);
109+
void init();
109110
uint32_t ip;
110111
uint32_t gw;
111112
uint32_t netmask;
112113
WiFiEventHandler* m_pWifiEventHandler;
113114
uint8_t m_dnsCount=0;
114115
bool m_eventLoopStarted;
116+
bool m_initCalled;
115117
FreeRTOS::Semaphore m_gotIpEvt = FreeRTOS::Semaphore("GotIpEvt");
116118

117119
public:

0 commit comments

Comments
 (0)
0