diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..576e39a --- /dev/null +++ b/platformio.ini @@ -0,0 +1,16 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32-s3-devkitc-1] +platform = espressif32 +board = esp32-s3-devkitc-1 +framework = arduino +monitor_speed = 115200 +lib_deps = tzapu/WiFiManager@^2.0.17 diff --git a/src/main.cpp b/src/main.cpp index 87bf7ce..67c674e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,23 +1,173 @@ #include #include +#include +#include +#include + +#define HOSTNAME_DEFAULT "m1730" + +static char hostname[32] = HOSTNAME_DEFAULT; +static WiFiManagerParameter hostnameParam("hostname", "Device hostname", hostname, 32); +static WebServer server(80); + +static void saveParams() { + strlcpy(hostname, hostnameParam.getValue(), sizeof(hostname)); + + File f = LittleFS.open("/hostname.txt", "w"); + if (f) { + f.print(hostname); + f.close(); + } +} + +static void loadParams() { + if (!LittleFS.exists("/hostname.txt")) return; + + File f = LittleFS.open("/hostname.txt", "r"); + if (!f) return; + + String val = f.readString(); + val.trim(); + if (val.length() > 0) { + val.toCharArray(hostname, sizeof(hostname)); + } + f.close(); +} + +static void startMDNS() { + if (MDNS.begin(hostname)) { + Serial.printf("mDNS: %s.local\n", hostname); + MDNS.addService("http", "tcp", 80); + } else { + Serial.println("mDNS start failed"); + } +} + +static void handleRoot() { + String html = R"( + + + + + +M1730 + + + +
+

M1730

+
Device Configuration
+
+
Hostname)" + String(hostname) + R"(
+
IP Address)" + WiFi.localIP().toString() + R"(
+
+
+ + + +
+
)" + WiFi.macAddress() + R"(
+
+ + +)"; + server.send(200, "text/html", html); +} + +static void handleUpdate() { + if (server.hasArg("hostname")) { + String val = server.arg("hostname"); + val.trim(); + if (val.length() > 0 && val.length() < sizeof(hostname)) { + val.toCharArray(hostname, sizeof(hostname)); + saveParams(); + String html = R"( + + + +M1730 + +
+

Saved!

+

Rebooting with new hostname: )" + String(hostname) + R"(

+
+ +)"; + server.send(200, "text/html", html); + delay(1000); + ESP.restart(); + return; + } + } + server.send(400, "text/html", "

Bad Request

"); +} + +static void startServer() { + server.on("/", handleRoot); + server.on("/update", HTTP_POST, handleUpdate); + server.begin(); + Serial.println("HTTP server started"); +} void setup() { Serial.begin(115200); + LittleFS.begin(true); + + loadParams(); WiFiManager wm; wm.setTitle("M1730"); - bool res = wm.autoConnect("M1730"); + wm.addParameter(&hostnameParam); + wm.setSaveParamsCallback(saveParams); - if (!res) { - Serial.println("Failed to connect to WiFi"); + if (!wm.autoConnect("M1730")) { + Serial.println("WiFi failed, restarting"); ESP.restart(); } - Serial.println("WiFi connected"); - Serial.print("IP address: "); - Serial.println(WiFi.localIP()); + saveParams(); + startServer(); + startMDNS(); + + Serial.printf("Board address: http://%s.local or http://%s\n", hostname, WiFi.localIP().toString().c_str()); } void loop() { + server.handleClient(); }