Add wifi and ota
This commit is contained in:
38
lib/network/network.cpp
Normal file
38
lib/network/network.cpp
Normal 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
30
lib/network/network.h
Normal 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
34
lib/ota/ota.cpp
Normal 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
9
lib/ota/ota.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
class OTAHandler {
|
||||
public:
|
||||
OTAHandler(std::string hostname);
|
||||
|
||||
void poll();
|
||||
};
|
||||
Reference in New Issue
Block a user