From d80a58f72c5ff894af291639a6b24f62588ebe75 Mon Sep 17 00:00:00 2001 From: Tianyu Liu Date: Tue, 29 Jul 2025 17:41:21 +0200 Subject: [PATCH] Add wifi and ota --- .vscode/settings.json | 59 +++++++++++++++++++++++++++++++++++++++++ lib/network/network.cpp | 38 ++++++++++++++++++++++++++ lib/network/network.h | 30 +++++++++++++++++++++ lib/ota/ota.cpp | 34 ++++++++++++++++++++++++ lib/ota/ota.h | 9 +++++++ platformio.ini | 14 +++++++++- src/main.cpp | 24 ++++++++++------- 7 files changed, 197 insertions(+), 11 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 lib/network/network.cpp create mode 100644 lib/network/network.h create mode 100644 lib/ota/ota.cpp create mode 100644 lib/ota/ota.h diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..025eb3b --- /dev/null +++ b/.vscode/settings.json @@ -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" + } +} \ No newline at end of file diff --git a/lib/network/network.cpp b/lib/network/network.cpp new file mode 100644 index 0000000..a32d5be --- /dev/null +++ b/lib/network/network.cpp @@ -0,0 +1,38 @@ + +#include +#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()); + } +} \ No newline at end of file diff --git a/lib/network/network.h b/lib/network/network.h new file mode 100644 index 0000000..cf226e9 --- /dev/null +++ b/lib/network/network.h @@ -0,0 +1,30 @@ +#include +#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( + +Wifi Manager + +

Wifi Manager

+ +
+

ESP32 WiFi Manager (c) 2022-2025 by Martin Verges

+ + )html"; + std::string hostname; + std::string apSsid; // SSID for the fallback AP +}; \ No newline at end of file diff --git a/lib/ota/ota.cpp b/lib/ota/ota.cpp new file mode 100644 index 0000000..ad2b093 --- /dev/null +++ b/lib/ota/ota.cpp @@ -0,0 +1,34 @@ +#include +#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(); +} \ No newline at end of file diff --git a/lib/ota/ota.h b/lib/ota/ota.h new file mode 100644 index 0000000..9ba41e2 --- /dev/null +++ b/lib/ota/ota.h @@ -0,0 +1,9 @@ +#pragma once +#include + +class OTAHandler { +public: + OTAHandler(std::string hostname); + + void poll(); +}; \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 0b8befc..482e731 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/main.cpp b/src/main.cpp index b33a2d8..9de6d7a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,22 @@ #include +#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); } \ No newline at end of file