Add wifi and ota #1
59
.vscode/settings.json
vendored
Normal file
59
.vscode/settings.json
vendored
Normal 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
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();
|
||||||
|
};
|
||||||
@@ -8,7 +8,19 @@
|
|||||||
; Please visit documentation for the other options and examples
|
; Please visit documentation for the other options and examples
|
||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[env:esp32dev]
|
[env]
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
board = esp32dev
|
board = esp32dev
|
||||||
framework = arduino
|
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
|
||||||
|
|||||||
22
src/main.cpp
22
src/main.cpp
@@ -1,18 +1,22 @@
|
|||||||
#include <Arduino.h>
|
#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() {
|
void setup() {
|
||||||
// put your setup code here, to run once:
|
// put your setup code here, to run once:
|
||||||
int result = myFunction(2, 3);
|
Serial.begin(115200);
|
||||||
|
Serial.println("Starting Smart RGB ESP32...");
|
||||||
|
network = new Network("smart-rgb");
|
||||||
|
otaHandler = new OTAHandler("smart-rgb-ota");
|
||||||
|
network->registerMDNS();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// put your main code here, to run repeatedly:
|
otaHandler->poll(); // Handle OTA updates
|
||||||
}
|
delay(500);
|
||||||
|
|
||||||
// put function definitions here:
|
|
||||||
int myFunction(int x, int y) {
|
|
||||||
return x + y;
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user