Files
MeshCoreTel-firmware/src/helpers/ESP32Board.cpp
T
Valentin V. Bartenev 47f41847ac Improve OTA behaviour when WiFi is already connected
When startOTAUpdate() was called while the device was already connected
as a STA (web panel or MQTT active), it would bring up an open
"MeshCore-OTA" AP alongside the existing connection and report its IP
(softAPIP, typically 192.168.4.1) in the reply.  The OTA AsyncWebServer
also listens on the STA interface, so the update page was reachable via
the existing network IP, but that address was not reported.

Two problems with the old behaviour:
- Raising an open AP while a STA connection is active is a security
  concern.
- The reported AP address is not useful to a user who is already on the
  same network as the device; they would naturally try the STA IP.

New behaviour:
- If the device is already connected as a STA (WiFi.status() ==
  WL_CONNECTED), skip softAP() entirely and report WiFi.localIP() in
  the reply.  The OTA server is already reachable on that address.
- If there is no STA connection, keep the original behaviour: bring up
  the "MeshCore-OTA" AP and report softAPIP().

In both cases the reply now includes the network name so the user knows
which interface to connect to:
  STA: "Started: http://<ip>/update (WiFi: <ssid>)"
  AP:  "Started: http://<ip>/update (AP: MeshCore-OTA)"

If a user specifically wants to perform OTA via the dedicated AP, they
can disable the current WiFi interface first, after which startOTAUpdate
will fall into the AP path as before.
2026-05-08 03:49:35 +03:00

64 lines
1.7 KiB
C++

#ifdef ESP_PLATFORM
#include "ESP32Board.h"
#if defined(ADMIN_PASSWORD) && !defined(DISABLE_WIFI_OTA) // Repeater or Room Server only
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <SPIFFS.h>
bool ESP32Board::startOTAUpdate(const char* id, char reply[]) {
inhibit_sleep = true; // prevent sleep during OTA
if (WiFi.status() != WL_CONNECTED) {
WiFi.mode(WIFI_AP_STA);
if (!WiFi.softAP("MeshCore-OTA", NULL)) {
strcpy(reply, "Error - OTA AP start failed");
return false;
}
sprintf(reply, "Started: http://%s/update (AP: MeshCore-OTA)",
WiFi.softAPIP().toString().c_str());
} else {
sprintf(reply, "Started: http://%s/update (WiFi: %s)",
WiFi.localIP().toString().c_str(), WiFi.SSID().c_str());
}
MESH_DEBUG_PRINTLN("startOTAUpdate: %s", reply);
static char id_buf[60];
sprintf(id_buf, "%s (%s)", id, getManufacturerName());
static char home_buf[90];
sprintf(home_buf, "<H2>Hi! I am a MeshCore Repeater. ID: %s</H2>", id);
static AsyncWebServer* server = nullptr;
if (server != nullptr) {
delete server;
server = nullptr;
}
server = new AsyncWebServer(80);
server->on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/html", home_buf);
});
server->on("/log", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/packet_log", "text/plain");
});
AsyncElegantOTA.setID(id_buf);
AsyncElegantOTA.begin(server); // Start ElegantOTA
server->begin();
return true;
}
#else
bool ESP32Board::startOTAUpdate(const char* id, char reply[]) {
return false; // not supported
}
#endif
#endif