Skip to content

Commit

Permalink
Wifi manager: avoid empty board name (#26)
Browse files Browse the repository at this point in the history
A device was failing to start AP mode when the board name was set to be the empty string.

- Avoid letting board name be empty (defaults to "og3board").
- If board name is empty, default AP mode essid is `"og3board"`, not empty-string.
- Properly get IP of board in AP mode.
- Version -> 0.2.2
  • Loading branch information
chl33 authored Oct 19, 2024
1 parent 69938de commit 69c15cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "og3",
"version": "0.2.1",
"version": "0.2.2",
"description": "A library for esp projects",
"keywords": "esp32, esp8266, modules, tasks, mqtt",
"authors": [
Expand Down
16 changes: 11 additions & 5 deletions src/wifi_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ WifiManager::WifiManager(const char* default_board_name, Tasks* tasks,
#endif
if (m_config) {
m_config->read_config(m_vg);
if (m_board.value().length() == 0) {
m_board = "og3board";
}
}
#ifdef ARDUINO_ARCH_ESP32
m_wifiEventIdConnected = WiFi.onEvent(
Expand Down Expand Up @@ -128,7 +131,7 @@ WifiManager::WifiManager(const char* default_board_name, Tasks* tasks,
void WifiManager::onConnect() {
m_start_connect_msec = 0;
#ifndef NATIVE
const IPAddress ip = WiFi.localIP();
const IPAddress ip = m_ap_mode ? WiFi.softAPIP() : WiFi.localIP();
m_ip_addr = ip.toString().c_str();
log()->logf("Connected to Wi-Fi. IP address: %s", m_ip_addr.value().c_str());
for (const auto& callback : m_connectCallbacks) {
Expand Down Expand Up @@ -177,11 +180,12 @@ void WifiManager::trySetup() {

// This is a function to call to startup the board in AP mode.
auto start_ap = [this]() {
log()->logf("Wifi: starting in AP mode (%s).", board().c_str());
const char* essid = (board().length() > 0) ? board().c_str() : "og3board";
log()->logf("Wifi: starting in AP mode (%s).", essid);
WiFi.mode(WIFI_AP);
m_ap_mode = true;
if (WiFi.softAPConfig(apSoftIP, apSoftIP, IPAddress(255, 255, 255, 0)) &&
WiFi.softAP(board().c_str())) {
if (WiFi.softAPConfig(apSoftIP, apSoftIP, IPAddress(255, 255, 255, 0)) && WiFi.softAP(essid)) {
log()->debugf("Started AP mode as %s", essid);
if (!m_dns_server) {
m_dns_server.reset(new DNSServer());
}
Expand All @@ -191,8 +195,10 @@ void WifiManager::trySetup() {
callback();
}
// Allow time to setup maybe??
m_scheduler.runIn(2 * kMsecInSec, [this]() { onConnect(); });
m_scheduler.runIn(100, [this]() { onConnect(); });
m_start_connect_msec = millis();
} else {
log()->log("Failed to setup SoftAP mode.");
}
};

Expand Down

0 comments on commit 69c15cb

Please sign in to comment.