Compare commits

2 Commits

Author SHA1 Message Date
8a0c502dae Merge pull request 'Add wifi and ota' (#1) from feature/add_wifi into main
Reviewed-on: #1
2025-07-29 17:41:17 +02:00
d80a58f72c Add wifi and ota 2025-07-29 17:41:21 +02:00
7 changed files with 197 additions and 11 deletions

59
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,59 @@
{
"files.associations": {
"array": "cpp",
"atomic": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
}
}

38
lib/network/network.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include <ESPmDNS.h>
#include "network.h"
WIFIMANAGER Network::WifiManager;
AsyncWebServer Network::webServer(80);
Network::Network(std::string hostname, std::string apSsid) : hostname(hostname), apSsid(apSsid) {
setHostname(hostname);
WifiManager.startBackgroundTask(apSsid.c_str(), "");
WifiManager.fallbackToSoftAp(true);
WifiManager.attachWebServer(&webServer);
WifiManager.attachUI();
webServer.on("/", HTTP_GET, [this](AsyncWebServerRequest *request) {
request->send(200, "text/html", this->defaultHomepage.c_str());
});
webServer.begin();
}
bool Network::isConnected() const {
return WiFi.status() == WL_CONNECTED;
}
std::string Network::getHostname() const {
return WiFi.getHostname();
}
bool Network::setHostname(const std::string &hostname) {
return WiFi.setHostname(hostname.c_str());
}
void Network::registerMDNS() {
if (!MDNS.begin(hostname.c_str())) {
Serial.println("Error setting up MDNS responder!");
} else {
Serial.printf("mDNS responder started with hostname: %s\n", hostname.c_str());
}
}

30
lib/network/network.h Normal file
View File

@@ -0,0 +1,30 @@
#include <Arduino.h>
#include "wifimanager.h"
class Network {
public:
Network(std::string hostname, std::string apSsid = "Smart RGB");
bool isConnected() const;
std::string getHostname() const;
bool setHostname(const std::string &hostname);
void registerMDNS();
private:
static WIFIMANAGER WifiManager;
static AsyncWebServer webServer;
const std::string defaultHomepage = R"html(
<!DOCTYPE html>
<html><head><title>Wifi Manager</title></head>
<body style="font-family: Arial, sans-serif; margin: 40px;">
<h1>Wifi Manager</h1>
<ul>
<li><a href="/wifi">WiFi Configuration Panel</a></li>
<li><a href="/api/wifi/status">WiFi Status (JSON API)</a></li>
<li><a href="/api/wifi/configlist">Saved Networks (JSON API)</a></li>
</ul>
<hr>
<p><small>ESP32 WiFi Manager (c) 2022-2025 by Martin Verges</small></p>
</body></html>
)html";
std::string hostname;
std::string apSsid; // SSID for the fallback AP
};

34
lib/ota/ota.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include <ArduinoOTA.h>
#include "ota.h"
OTAHandler::OTAHandler(std::string hostname) {
ArduinoOTA.setHostname(hostname.c_str());
ArduinoOTA.onStart([]() {
Serial.println("OTA Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("OTA End");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("OTA Progress: %u%%\n", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("OTA Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
}
void OTAHandler::poll() {
ArduinoOTA.handle();
}

9
lib/ota/ota.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include <string>
class OTAHandler {
public:
OTAHandler(std::string hostname);
void poll();
};

View File

@@ -8,7 +8,19 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32dev]
[env]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
martinverges/ESP32 Wifi Manager@^1.5.0
esp32async/ESPAsyncWebServer@^3.7.10
bblanchon/ArduinoJson@^7.4.2
[env:esp32dev-serial]
[env:esp32dev-ota]
upload_protocol = espota
upload_port = smart-rgb.local

View File

@@ -1,18 +1,22 @@
#include <Arduino.h>
#include "network.h"
#include "ota.h"
#include "wifimanager.h"
Network* network = nullptr;
OTAHandler* otaHandler = nullptr;
// put function declarations here:
int myFunction(int, int);
void setup() {
// put your setup code here, to run once:
int result = myFunction(2, 3);
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Starting Smart RGB ESP32...");
network = new Network("smart-rgb");
otaHandler = new OTAHandler("smart-rgb-ota");
network->registerMDNS();
}
void loop() {
// put your main code here, to run repeatedly:
}
// put function definitions here:
int myFunction(int x, int y) {
return x + y;
otaHandler->poll(); // Handle OTA updates
delay(500);
}