@@ -53,10 +53,7 @@ WiFi::WiFi()
53
53
, m_pWifiEventHandler(nullptr )
54
54
{
55
55
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 ;
60
57
m_pWifiEventHandler = new WiFiEventHandler ();
61
58
} // WiFi
62
59
@@ -100,6 +97,7 @@ void WiFi::addDNSServer(const char* ip) {
100
97
101
98
void WiFi::addDNSServer (ip_addr_t ip) {
102
99
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 ();
103
101
::dns_setserver (m_dnsCount, &ip);
104
102
m_dnsCount++;
105
103
m_dnsCount %= 2 ;
@@ -137,6 +135,7 @@ void WiFi::setDNSServer(int numdns, const char* ip) {
137
135
138
136
void WiFi::setDNSServer (int numdns, ip_addr_t ip) {
139
137
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 ();
140
139
::dns_setserver (numdns, &ip);
141
140
} // setDNSServer
142
141
@@ -154,6 +153,8 @@ void WiFi::setDNSServer(int numdns, ip_addr_t ip) {
154
153
void WiFi::connectAP (const std::string& ssid, const std::string& password, bool waitForConnection){
155
154
ESP_LOGD (LOG_TAG, " >> connectAP" );
156
155
156
+ init ();
157
+
157
158
if (ip != 0 && gw != 0 && netmask != 0 ) {
158
159
::tcpip_adapter_dhcpc_stop (TCPIP_ADAPTER_IF_STA); // Don't run a DHCP client
159
160
@@ -165,27 +166,7 @@ void WiFi::connectAP(const std::string& ssid, const std::string& password, bool
165
166
::tcpip_adapter_set_ip_info (TCPIP_ADAPTER_IF_STA, &ipInfo);
166
167
}
167
168
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);
189
170
if (errRc != ESP_OK) {
190
171
ESP_LOGE (LOG_TAG, " esp_wifi_set_mode: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
191
172
abort ();
@@ -217,6 +198,7 @@ void WiFi::connectAP(const std::string& ssid, const std::string& password, bool
217
198
} // connectAP
218
199
219
200
201
+
220
202
/* *
221
203
* @brief Dump diagnostics to the log.
222
204
*/
@@ -258,6 +240,7 @@ void WiFi::dump() {
258
240
* @return The AP IP Info.
259
241
*/
260
242
tcpip_adapter_ip_info_t WiFi::getApIpInfo () {
243
+ // init();
261
244
tcpip_adapter_ip_info_t ipInfo;
262
245
tcpip_adapter_get_ip_info (TCPIP_ADAPTER_IF_AP, &ipInfo);
263
246
return ipInfo;
@@ -270,6 +253,7 @@ tcpip_adapter_ip_info_t WiFi::getApIpInfo() {
270
253
*/
271
254
std::string WiFi::getApMac () {
272
255
uint8_t mac[6 ];
256
+ // init();
273
257
esp_wifi_get_mac (WIFI_IF_AP, mac);
274
258
auto mac_str = (char *) malloc (18 );
275
259
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() {
283
267
*/
284
268
std::string WiFi::getApSSID () {
285
269
wifi_config_t conf;
270
+ // init();
286
271
esp_wifi_get_config (WIFI_IF_AP, &conf);
287
272
return std::string ((char *)conf.sta .ssid );
288
273
} // getApSSID
@@ -372,21 +357,9 @@ std::string WiFi::getStaSSID() {
372
357
373
358
374
359
/* *
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.
383
361
*/
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 () {
390
363
391
364
// If we have already started the event loop, then change the handler otherwise
392
365
// start the event loop.
@@ -402,20 +375,44 @@ std::vector<WiFiAPRecord> WiFi::scan() {
402
375
}
403
376
// Now, one way or another, the event handler is WiFi::eventHandler.
404
377
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 ();
411
381
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
+ }
416
394
}
395
+ m_initCalled = true ;
396
+ } // init
397
+
417
398
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);
419
416
if (errRc != ESP_OK) {
420
417
ESP_LOGE (LOG_TAG, " esp_wifi_set_mode: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
421
418
abort ();
@@ -478,41 +475,16 @@ std::vector<WiFiAPRecord> WiFi::scan() {
478
475
*/
479
476
void WiFi::startAP (const std::string& ssid, const std::string& password) {
480
477
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.
498
478
479
+ init ();
499
480
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);
512
482
if (errRc != ESP_OK) {
513
483
ESP_LOGE (LOG_TAG, " esp_wifi_set_mode: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
514
484
abort ();
515
485
}
486
+
487
+ // Build the apConfig structure.
516
488
wifi_config_t apConfig;
517
489
::memset (&apConfig, 0 , sizeof (apConfig));
518
490
::memcpy (apConfig.ap.ssid, ssid.data(), ssid.size());
@@ -523,11 +495,13 @@ void WiFi::startAP(const std::string& ssid, const std::string& password) {
523
495
apConfig.ap .ssid_hidden = 0 ;
524
496
apConfig.ap .max_connection = 4 ;
525
497
apConfig.ap .beacon_interval = 100 ;
498
+
526
499
errRc = ::esp_wifi_set_config (WIFI_IF_AP, &apConfig);
527
500
if (errRc != ESP_OK) {
528
501
ESP_LOGE (LOG_TAG, " esp_wifi_set_config: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
529
502
abort ();
530
503
}
504
+
531
505
errRc = ::esp_wifi_start ();
532
506
if (errRc != ESP_OK) {
533
507
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) {
595
569
* @param [in] netmask Our TCP/IP netmask value.
596
570
*/
597
571
void WiFi::setIPInfo (uint32_t ip, uint32_t gw, uint32_t netmask) {
572
+ init ();
573
+
598
574
this ->ip = ip;
599
575
this ->gw = gw;
600
576
this ->netmask = netmask;
0 commit comments