Skip to content

Commit 30139b8

Browse files
committed
Moving hostname inside Config in order to save and reload it
1 parent 1c50bfc commit 30139b8

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ He is making great Arduino libraries.
5858
2 flavors of `begin()` methods:
5959

6060
1. `espConnect.begin("hostname", "ssid", "password")` / `espConnect.begin("hostname", "ssid")`
61-
2. `espConnect.begin("hostname", "ssid", "password", Mycila::ESPConnect::Config)` where config is `{.wifiSSID = ..., .wifiPassword = ..., .apMode = ...}`
61+
2. `espConnect.begin("ssid", "password", Mycila::ESPConnect::Config)` where config is `{.hostname = ..., .wifiSSID = ..., .wifiPassword = ..., .apMode = ...}`
6262

6363
The first flavors will automatically handle the persistance of user choices and reload them at startup.
6464

@@ -147,12 +147,13 @@ espConnect.setIPConfig(ipConfig);
147147

148148
// load config from external system
149149
Mycila::ESPConnect::Config config = {
150+
.hostname = "arduino",
150151
.wifiSSID = ...,
151152
.wifiPassword = ...,
152153
.apMode = ...
153154
};
154155

155-
espConnect.begin("arduino", "Captive Portal SSID", "", config);
156+
espConnect.begin("Captive Portal SSID", "", config);
156157
```
157158
158159
### ESP8266 Specifics

docs/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ He is making great Arduino libraries.
5858
2 flavors of `begin()` methods:
5959

6060
1. `espConnect.begin("hostname", "ssid", "password")` / `espConnect.begin("hostname", "ssid")`
61-
2. `espConnect.begin("hostname", "ssid", "password", Mycila::ESPConnect::Config)` where config is `{.wifiSSID = ..., .wifiPassword = ..., .apMode = ...}`
61+
2. `espConnect.begin("ssid", "password", Mycila::ESPConnect::Config)` where config is `{.hostname = ..., .wifiSSID = ..., .wifiPassword = ..., .apMode = ...}`
6262

6363
The first flavors will automatically handle the persistance of user choices and reload them at startup.
6464

@@ -147,12 +147,13 @@ espConnect.setIPConfig(ipConfig);
147147

148148
// load config from external system
149149
Mycila::ESPConnect::Config config = {
150+
.hostname = "arduino",
150151
.wifiSSID = ...,
151152
.wifiPassword = ...,
152153
.apMode = ...
153154
};
154155

155-
espConnect.begin("arduino", "Captive Portal SSID", "", config);
156+
espConnect.begin("Captive Portal SSID", "", config);
156157
```
157158
158159
### ESP8266 Specifics

examples/AdvancedCaptivePortal/AdvancedCaptivePortal.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void setup() {
7777
Preferences preferences;
7878
preferences.begin("app", true);
7979
Mycila::ESPConnect::Config config = {
80+
.hostname = "arduino",
8081
.wifiSSID = (preferences.isKey("ssid") ? preferences.getString("ssid", emptyString) : emptyString).c_str(),
8182
.wifiPassword = (preferences.isKey("password") ? preferences.getString("password", emptyString) : emptyString).c_str(),
8283
.apMode = preferences.isKey("ap") ? preferences.getBool("ap", false) : false};
@@ -87,7 +88,7 @@ void setup() {
8788
Serial.printf("wifiSSID: %s\n", config.wifiSSID.c_str());
8889
Serial.printf("wifiPassword: %s\n", config.wifiPassword.c_str());
8990

90-
espConnect.begin("arduino", "Captive Portal SSID", "", config);
91+
espConnect.begin("Captive Portal SSID", "", config);
9192

9293
Serial.println("====> setup() completed...");
9394
}

src/MycilaESPConnect.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,14 @@ void Mycila::ESPConnect::begin(const char* hostname, const char* apSSID, const c
257257
_autoSave = true;
258258
Config config;
259259
loadConfiguration(config);
260-
begin(hostname, apSSID, apPassword, config);
260+
config.hostname = hostname == nullptr ? "" : hostname;
261+
begin(apSSID, apPassword, config);
261262
}
262263

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) {
264265
if (_state != Mycila::ESPConnect::State::NETWORK_DISABLED)
265266
return;
266267

267-
_hostname = hostname;
268268
_apSSID = apSSID;
269269
_apPassword = apPassword;
270270
_config = config; // copy values
@@ -397,13 +397,16 @@ void Mycila::ESPConnect::loadConfiguration(Mycila::ESPConnect::Config& config) {
397397
config.ipConfig.gateway.fromString(preferences.getString("gateway"));
398398
if (preferences.isKey("dns"))
399399
config.ipConfig.dns.fromString(preferences.getString("dns"));
400+
if (preferences.isKey("hostname"))
401+
config.hostname = preferences.getString("hostname").c_str();
400402
preferences.end();
401403
LOGD(TAG, " - AP: %d", config.apMode);
402404
LOGD(TAG, " - SSID: %s", config.wifiSSID.c_str());
403405
LOGD(TAG, " - IP: %s", config.ipConfig.ip.toString().c_str());
404406
LOGD(TAG, " - Subnet: %s", config.ipConfig.subnet.toString().c_str());
405407
LOGD(TAG, " - Gateway: %s", config.ipConfig.gateway.toString().c_str());
406408
LOGD(TAG, " - DNS: %s", config.ipConfig.dns.toString().c_str());
409+
LOGD(TAG, " - Hostname: %s", config.hostname.c_str());
407410
}
408411

409412
void Mycila::ESPConnect::saveConfiguration(const Mycila::ESPConnect::Config& config) {
@@ -414,6 +417,7 @@ void Mycila::ESPConnect::saveConfiguration(const Mycila::ESPConnect::Config& con
414417
LOGD(TAG, " - Subnet: %s", config.ipConfig.subnet.toString().c_str());
415418
LOGD(TAG, " - Gateway: %s", config.ipConfig.gateway.toString().c_str());
416419
LOGD(TAG, " - DNS: %s", config.ipConfig.dns.toString().c_str());
420+
LOGD(TAG, " - Hostname: %s", config.hostname.c_str());
417421
Preferences preferences;
418422
preferences.begin("espconnect", false);
419423
preferences.putBool("ap", config.apMode);
@@ -423,6 +427,7 @@ void Mycila::ESPConnect::saveConfiguration(const Mycila::ESPConnect::Config& con
423427
preferences.putString("subnet", config.ipConfig.subnet.toString().c_str());
424428
preferences.putString("gateway", config.ipConfig.gateway.toString().c_str());
425429
preferences.putString("dns", config.ipConfig.dns.toString().c_str());
430+
preferences.putString("hostname", config.hostname.c_str());
426431
preferences.end();
427432
}
428433

@@ -440,6 +445,7 @@ void Mycila::ESPConnect::toJson(const JsonObject& root) const {
440445
root["ip_address_ap"] = getIPAddress(Mycila::ESPConnect::Mode::AP).toString();
441446
root["ip_address_eth"] = getIPAddress(Mycila::ESPConnect::Mode::ETH).toString();
442447
root["ip_address_sta"] = getIPAddress(Mycila::ESPConnect::Mode::STA).toString();
448+
root["hostname"] = _config.hostname.c_str();
443449
root["mac_address"] = getMACAddress();
444450
root["mac_address_ap"] = getMACAddress(Mycila::ESPConnect::Mode::AP);
445451
root["mac_address_eth"] = getMACAddress(Mycila::ESPConnect::Mode::ETH);
@@ -526,7 +532,7 @@ void Mycila::ESPConnect::_startSTA() {
526532
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
527533
#endif
528534

529-
WiFi.setHostname(_hostname.c_str());
535+
WiFi.setHostname(_config.hostname.c_str());
530536
WiFi.setSleep(false);
531537
WiFi.persistent(false);
532538
WiFi.setAutoReconnect(true);
@@ -559,12 +565,12 @@ void Mycila::ESPConnect::_startAP() {
559565
LOGI(TAG, "Starting Access Point...");
560566

561567
#ifndef ESP8266
562-
WiFi.softAPsetHostname(_hostname.c_str());
568+
WiFi.softAPsetHostname(_config.hostname.c_str());
563569
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
564570
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
565571
#endif
566572

567-
WiFi.setHostname(_hostname.c_str());
573+
WiFi.setHostname(_config.hostname.c_str());
568574
WiFi.setSleep(false);
569575
WiFi.persistent(false);
570576
WiFi.setAutoReconnect(false);
@@ -744,7 +750,7 @@ void Mycila::ESPConnect::_onWiFiEvent(WiFiEvent_t event) {
744750
case ARDUINO_EVENT_ETH_START:
745751
if (ETH.linkUp()) {
746752
LOGD(TAG, "[%s] WiFiEvent: ARDUINO_EVENT_ETH_START", getStateName());
747-
ETH.setHostname(_hostname.c_str());
753+
ETH.setHostname(_config.hostname.c_str());
748754
}
749755
break;
750756

@@ -756,7 +762,7 @@ void Mycila::ESPConnect::_onWiFiEvent(WiFiEvent_t event) {
756762
}
757763
_lastTime = -1;
758764
#ifndef ESPCONNECT_NO_MDNS
759-
MDNS.begin(_hostname.c_str());
765+
MDNS.begin(_config.hostname.c_str());
760766
#endif
761767
_setState(Mycila::ESPConnect::State::NETWORK_CONNECTED);
762768
}
@@ -774,7 +780,7 @@ void Mycila::ESPConnect::_onWiFiEvent(WiFiEvent_t event) {
774780
LOGD(TAG, "[%s] WiFiEvent: ARDUINO_EVENT_WIFI_STA_GOT_IP", getStateName());
775781
_lastTime = -1;
776782
#ifndef ESPCONNECT_NO_MDNS
777-
MDNS.begin(_hostname.c_str());
783+
MDNS.begin(_config.hostname.c_str());
778784
#endif
779785
_setState(Mycila::ESPConnect::State::NETWORK_CONNECTED);
780786
}
@@ -800,7 +806,7 @@ void Mycila::ESPConnect::_onWiFiEvent(WiFiEvent_t event) {
800806

801807
case ARDUINO_EVENT_WIFI_AP_START:
802808
#ifndef ESPCONNECT_NO_MDNS
803-
MDNS.begin(_hostname.c_str());
809+
MDNS.begin(_config.hostname.c_str());
804810
#endif
805811
if (_state == Mycila::ESPConnect::State::AP_STARTING) {
806812
LOGD(TAG, "[%s] WiFiEvent: ARDUINO_EVENT_WIFI_AP_START", getStateName());

src/MycilaESPConnect.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ namespace Mycila {
9999
} IPConfig;
100100

101101
typedef struct {
102+
// Hostname of the ESP, loaded from config or set from begin()
103+
ESPCONNECT_STRING hostname;
102104
// SSID name to connect to, loaded from config or set from begin(), or from the captive portal
103105
ESPCONNECT_STRING wifiSSID;
104106
// Password for the WiFi to connect to, loaded from config or set from begin(), or from the captive portal
@@ -134,7 +136,7 @@ namespace Mycila {
134136
// 3. If STA mode fails, or empty WiFi credentials were passed, starts the captive portal
135137
//
136138
// Using this method will NOT auto-load or auto-save any configuration
137-
void begin(const char* hostname, const char* apSSID, const char* apPassword, const Config& config); // NOLINT
139+
void begin(const char* apSSID, const char* apPassword, const Config& config); // NOLINT
138140

139141
// loop() method to be called from main loop()
140142
void loop();
@@ -173,7 +175,7 @@ namespace Mycila {
173175
int8_t getWiFiSignalQuality() const;
174176

175177
// the hostname passed from begin()
176-
const ESPCONNECT_STRING& getHostname() const { return _hostname; }
178+
const ESPCONNECT_STRING& getHostname() const { return _config.hostname; }
177179

178180
// SSID name used for the captive portal or in AP mode
179181
const ESPCONNECT_STRING& getAccessPointSSID() const { return _apSSID; }
@@ -248,7 +250,6 @@ namespace Mycila {
248250
StateCallback _callback = nullptr;
249251
DNSServer* _dnsServer = nullptr;
250252
int64_t _lastTime = -1;
251-
ESPCONNECT_STRING _hostname;
252253
ESPCONNECT_STRING _apSSID;
253254
ESPCONNECT_STRING _apPassword;
254255
uint32_t _connectTimeout = ESPCONNECT_CONNECTION_TIMEOUT;

0 commit comments

Comments
 (0)