Skip to content

Commit fdf2ddf

Browse files
committed
Allow to reuse hostname from main app
Ref: mathieucarbou/MycilaESPConnect @ 9.0.1
1 parent 6eac18f commit fdf2ddf

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ lib_deps =
2626
ESPmDNS
2727
Update
2828
WebServer
29-
mathieucarbou/MycilaESPConnect @ 8.1.0
29+
mathieucarbou/MycilaESPConnect @ 9.0.1
3030
mathieucarbou/MycilaLogger @ 3.3.0
3131
lib_ignore =
3232
ArduinoJson

src/main.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ static WebServer webServer(80);
3737
static HTTPUpdateServer httpUpdater;
3838
static Mycila::ESPConnect espConnect;
3939
static Mycila::ESPConnect::Config espConnectConfig;
40-
static String hostname;
4140

4241
static String getChipIDStr() {
4342
uint32_t chipId = 0;
@@ -64,17 +63,15 @@ void setup() {
6463
logger.setLevel(ARDUHAL_LOG_LEVEL_DEBUG);
6564
#endif
6665

67-
// Init hostname
68-
hostname = "SafeBoot-" + getChipIDStr();
6966
LOGI(TAG, "SafeBoot Version: %s", __COMPILED_APP_VERSION__);
7067
LOGI(TAG, "Chip ID: %s", getChipIDStr().c_str());
71-
LOGI(TAG, "Hostname: %s", hostname.c_str());
7268

7369
// Set next boot partition
7470
const esp_partition_t* partition = esp_partition_find_first(esp_partition_type_t::ESP_PARTITION_TYPE_APP, esp_partition_subtype_t::ESP_PARTITION_SUBTYPE_APP_OTA_0, nullptr);
7571
if (partition) {
7672
esp_ota_set_boot_partition(partition);
7773
}
74+
7875
// setup routes
7976
HTTPUpdateServer::setVersion(__COMPILED_APP_VERSION__);
8077
httpUpdater.setup(&webServer, "/");
@@ -88,6 +85,11 @@ void setup() {
8885
espConnect.setBlocking(true);
8986
espConnect.setAutoRestart(false);
9087

88+
// reuse a potentially set hostname from main app, or set a default one
89+
if (!espConnectConfig.hostname.length()) {
90+
espConnectConfig.hostname = "SafeBoot-" + getChipIDStr();
91+
}
92+
9193
// If the passed config is to be in AP mode, or has a SSID that's fine.
9294
// If the passed config is empty, we need to check if the board supports ETH.
9395
// - For boards relying only on Wifi, if a SSID is not set and AP is not set (config empty), then we need to go to AP mode.
@@ -111,28 +113,31 @@ void setup() {
111113
});
112114

113115
// connect...
114-
espConnect.begin(hostname.c_str(), hostname.c_str(), "", espConnectConfig);
116+
espConnect.begin(espConnectConfig.hostname.c_str(), "", espConnectConfig);
115117

116118
LOGI(TAG, "Connected to network!");
117119
LOGI(TAG, "IP Address: %s", espConnect.getIPAddress().toString().c_str());
118120
LOGI(TAG, "Hostname: %s", espConnect.getHostname().c_str());
119121
if (espConnect.getWiFiSSID().length()) {
120122
LOGI(TAG, "WiFi SSID: %s", espConnect.getWiFiSSID().c_str());
121123
}
124+
if (espConnectConfig.apMode) {
125+
LOGI(TAG, "Access Point: %s", espConnect.getAccessPointSSID().c_str());
126+
}
122127

123128
// starte http
124129
LOGI(TAG, "Starting HTTP server...");
125130
webServer.begin();
126131

127132
#ifndef MYCILA_SAFEBOOT_NO_MDNS
128133
// Start mDNS
129-
MDNS.begin(hostname.c_str());
134+
MDNS.begin(espConnectConfig.hostname.c_str());
130135
MDNS.addService("http", "tcp", 80);
131136
#endif
132137

133138
// Start OTA
134139
LOGI(TAG, "Starting OTA server...");
135-
ArduinoOTA.setHostname(hostname.c_str());
140+
ArduinoOTA.setHostname(espConnectConfig.hostname.c_str());
136141
ArduinoOTA.setRebootOnSuccess(true);
137142
ArduinoOTA.setMdnsEnabled(true);
138143
ArduinoOTA.begin();

0 commit comments

Comments
 (0)