@@ -257,14 +257,14 @@ void Mycila::ESPConnect::begin(const char* hostname, const char* apSSID, const c
257
257
_autoSave = true ;
258
258
Config config;
259
259
loadConfiguration (config);
260
- begin (hostname, apSSID, apPassword, config);
260
+ config.hostname = hostname == nullptr ? " " : hostname;
261
+ begin (apSSID, apPassword, config);
261
262
}
262
263
263
- void Mycila::ESPConnect::begin (const char * hostname, const char * apSSID, const char * apPassword, const Mycila::ESPConnect::Config& config) {
264
+ void Mycila::ESPConnect::begin (const char * apSSID, const char * apPassword, const Mycila::ESPConnect::Config& config) {
264
265
if (_state != Mycila::ESPConnect::State::NETWORK_DISABLED)
265
266
return ;
266
267
267
- _hostname = hostname;
268
268
_apSSID = apSSID;
269
269
_apPassword = apPassword;
270
270
_config = config; // copy values
@@ -397,13 +397,16 @@ void Mycila::ESPConnect::loadConfiguration(Mycila::ESPConnect::Config& config) {
397
397
config.ipConfig .gateway .fromString (preferences.getString (" gateway" ));
398
398
if (preferences.isKey (" dns" ))
399
399
config.ipConfig .dns .fromString (preferences.getString (" dns" ));
400
+ if (preferences.isKey (" hostname" ))
401
+ config.hostname = preferences.getString (" hostname" ).c_str ();
400
402
preferences.end ();
401
403
LOGD (TAG, " - AP: %d" , config.apMode );
402
404
LOGD (TAG, " - SSID: %s" , config.wifiSSID .c_str ());
403
405
LOGD (TAG, " - IP: %s" , config.ipConfig .ip .toString ().c_str ());
404
406
LOGD (TAG, " - Subnet: %s" , config.ipConfig .subnet .toString ().c_str ());
405
407
LOGD (TAG, " - Gateway: %s" , config.ipConfig .gateway .toString ().c_str ());
406
408
LOGD (TAG, " - DNS: %s" , config.ipConfig .dns .toString ().c_str ());
409
+ LOGD (TAG, " - Hostname: %s" , config.hostname .c_str ());
407
410
}
408
411
409
412
void Mycila::ESPConnect::saveConfiguration (const Mycila::ESPConnect::Config& config) {
@@ -414,6 +417,7 @@ void Mycila::ESPConnect::saveConfiguration(const Mycila::ESPConnect::Config& con
414
417
LOGD (TAG, " - Subnet: %s" , config.ipConfig .subnet .toString ().c_str ());
415
418
LOGD (TAG, " - Gateway: %s" , config.ipConfig .gateway .toString ().c_str ());
416
419
LOGD (TAG, " - DNS: %s" , config.ipConfig .dns .toString ().c_str ());
420
+ LOGD (TAG, " - Hostname: %s" , config.hostname .c_str ());
417
421
Preferences preferences;
418
422
preferences.begin (" espconnect" , false );
419
423
preferences.putBool (" ap" , config.apMode );
@@ -423,6 +427,7 @@ void Mycila::ESPConnect::saveConfiguration(const Mycila::ESPConnect::Config& con
423
427
preferences.putString (" subnet" , config.ipConfig .subnet .toString ().c_str ());
424
428
preferences.putString (" gateway" , config.ipConfig .gateway .toString ().c_str ());
425
429
preferences.putString (" dns" , config.ipConfig .dns .toString ().c_str ());
430
+ preferences.putString (" hostname" , config.hostname .c_str ());
426
431
preferences.end ();
427
432
}
428
433
@@ -440,6 +445,7 @@ void Mycila::ESPConnect::toJson(const JsonObject& root) const {
440
445
root[" ip_address_ap" ] = getIPAddress (Mycila::ESPConnect::Mode::AP).toString ();
441
446
root[" ip_address_eth" ] = getIPAddress (Mycila::ESPConnect::Mode::ETH).toString ();
442
447
root[" ip_address_sta" ] = getIPAddress (Mycila::ESPConnect::Mode::STA).toString ();
448
+ root[" hostname" ] = _config.hostname .c_str ();
443
449
root[" mac_address" ] = getMACAddress ();
444
450
root[" mac_address_ap" ] = getMACAddress (Mycila::ESPConnect::Mode::AP);
445
451
root[" mac_address_eth" ] = getMACAddress (Mycila::ESPConnect::Mode::ETH);
@@ -526,7 +532,7 @@ void Mycila::ESPConnect::_startSTA() {
526
532
WiFi.setSortMethod (WIFI_CONNECT_AP_BY_SIGNAL);
527
533
#endif
528
534
529
- WiFi.setHostname (_hostname .c_str ());
535
+ WiFi.setHostname (_config. hostname .c_str ());
530
536
WiFi.setSleep (false );
531
537
WiFi.persistent (false );
532
538
WiFi.setAutoReconnect (true );
@@ -559,12 +565,12 @@ void Mycila::ESPConnect::_startAP() {
559
565
LOGI (TAG, " Starting Access Point..." );
560
566
561
567
#ifndef ESP8266
562
- WiFi.softAPsetHostname (_hostname .c_str ());
568
+ WiFi.softAPsetHostname (_config. hostname .c_str ());
563
569
WiFi.setScanMethod (WIFI_ALL_CHANNEL_SCAN);
564
570
WiFi.setSortMethod (WIFI_CONNECT_AP_BY_SIGNAL);
565
571
#endif
566
572
567
- WiFi.setHostname (_hostname .c_str ());
573
+ WiFi.setHostname (_config. hostname .c_str ());
568
574
WiFi.setSleep (false );
569
575
WiFi.persistent (false );
570
576
WiFi.setAutoReconnect (false );
@@ -744,7 +750,7 @@ void Mycila::ESPConnect::_onWiFiEvent(WiFiEvent_t event) {
744
750
case ARDUINO_EVENT_ETH_START:
745
751
if (ETH.linkUp ()) {
746
752
LOGD (TAG, " [%s] WiFiEvent: ARDUINO_EVENT_ETH_START" , getStateName ());
747
- ETH.setHostname (_hostname .c_str ());
753
+ ETH.setHostname (_config. hostname .c_str ());
748
754
}
749
755
break ;
750
756
@@ -756,7 +762,7 @@ void Mycila::ESPConnect::_onWiFiEvent(WiFiEvent_t event) {
756
762
}
757
763
_lastTime = -1 ;
758
764
#ifndef ESPCONNECT_NO_MDNS
759
- MDNS.begin (_hostname .c_str ());
765
+ MDNS.begin (_config. hostname .c_str ());
760
766
#endif
761
767
_setState (Mycila::ESPConnect::State::NETWORK_CONNECTED);
762
768
}
@@ -774,7 +780,7 @@ void Mycila::ESPConnect::_onWiFiEvent(WiFiEvent_t event) {
774
780
LOGD (TAG, " [%s] WiFiEvent: ARDUINO_EVENT_WIFI_STA_GOT_IP" , getStateName ());
775
781
_lastTime = -1 ;
776
782
#ifndef ESPCONNECT_NO_MDNS
777
- MDNS.begin (_hostname .c_str ());
783
+ MDNS.begin (_config. hostname .c_str ());
778
784
#endif
779
785
_setState (Mycila::ESPConnect::State::NETWORK_CONNECTED);
780
786
}
@@ -800,7 +806,7 @@ void Mycila::ESPConnect::_onWiFiEvent(WiFiEvent_t event) {
800
806
801
807
case ARDUINO_EVENT_WIFI_AP_START:
802
808
#ifndef ESPCONNECT_NO_MDNS
803
- MDNS.begin (_hostname .c_str ());
809
+ MDNS.begin (_config. hostname .c_str ());
804
810
#endif
805
811
if (_state == Mycila::ESPConnect::State::AP_STARTING) {
806
812
LOGD (TAG, " [%s] WiFiEvent: ARDUINO_EVENT_WIFI_AP_START" , getStateName ());
0 commit comments