Hostname, Webserver, MDNS added
This commit is contained in:
10
.vscode/extensions.json
vendored
Normal file
10
.vscode/extensions.json
vendored
Normal file
@@ -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"
|
||||
]
|
||||
}
|
||||
16
platformio.ini
Normal file
16
platformio.ini
Normal file
@@ -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
|
||||
162
src/main.cpp
162
src/main.cpp
@@ -1,23 +1,173 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFiManager.h>
|
||||
#include <ESPmDNS.h>
|
||||
#include <LittleFS.h>
|
||||
#include <WebServer.h>
|
||||
|
||||
#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"(
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset=utf-8>
|
||||
<meta name=viewport content='width=device-width,initial-scale=1'>
|
||||
<title>M1730</title>
|
||||
<style>
|
||||
*, *::before, *::after { box-sizing:border-box; margin:0; padding:0 }
|
||||
body {
|
||||
font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen,Ubuntu,sans-serif;
|
||||
background:#f2f4f8; color:#1a1a2e; display:flex; justify-content:center;
|
||||
align-items:center; min-height:100vh; padding:16px;
|
||||
}
|
||||
.card {
|
||||
background:#fff; border-radius:12px; box-shadow:0 4px 24px rgba(0,0,0,.08);
|
||||
padding:32px; width:100%; max-width:400px;
|
||||
}
|
||||
h1 { font-size:24px; margin-bottom:4px }
|
||||
.sub { color:#666; font-size:14px; margin-bottom:24px }
|
||||
.info { background:#f8f9fc; border-radius:8px; padding:12px 16px; margin-bottom:20px; font-size:14px }
|
||||
.info div { display:flex; justify-content:space-between; padding:4px 0 }
|
||||
.info div span:first-child { color:#666 }
|
||||
label { display:block; font-size:14px; font-weight:600; margin-bottom:6px }
|
||||
input {
|
||||
width:100%; padding:10px 14px; border:1px solid #d1d5db; border-radius:8px;
|
||||
font-size:15px; outline:none; transition:border-color .2s;
|
||||
}
|
||||
input:focus { border-color:#4361ee; box-shadow:0 0 0 3px rgba(67,97,238,.15) }
|
||||
button {
|
||||
width:100%; margin-top:16px; padding:12px; background:#4361ee; color:#fff;
|
||||
border:none; border-radius:8px; font-size:15px; font-weight:600; cursor:pointer;
|
||||
transition:background .2s;
|
||||
}
|
||||
button:hover { background:#3651d4 }
|
||||
.mac { font-size:12px; color:#999; text-align:center; margin-top:20px }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class=card>
|
||||
<h1>M1730</h1>
|
||||
<div class=sub>Device Configuration</div>
|
||||
<div class=info>
|
||||
<div><span>Hostname</span><span>)" + String(hostname) + R"(</span></div>
|
||||
<div><span>IP Address</span><span>)" + WiFi.localIP().toString() + R"(</span></div>
|
||||
</div>
|
||||
<form action=/update method=post>
|
||||
<label for=h>Change hostname</label>
|
||||
<input id=h name=hostname value=')" + String(hostname) + R"('>
|
||||
<button type=submit>Save & Reboot</button>
|
||||
</form>
|
||||
<div class=mac>)" + WiFi.macAddress() + R"(</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
)";
|
||||
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"(
|
||||
<!DOCTYPE html>
|
||||
<html><head><meta charset=utf-8>
|
||||
<meta name=viewport content='width=device-width,initial-scale=1'>
|
||||
<title>M1730</title>
|
||||
<style>
|
||||
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
|
||||
body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen,Ubuntu,sans-serif;background:#f2f4f8;color:#1a1a2e;display:flex;justify-content:center;align-items:center;min-height:100vh;padding:16px}
|
||||
.card{background:#fff;border-radius:12px;box-shadow:0 4px 24px rgba(0,0,0,.08);padding:32px;width:100%;max-width:400px;text-align:center}
|
||||
h1{font-size:24px;margin-bottom:8px}
|
||||
p{color:#666;font-size:14px}
|
||||
</style></head><body>
|
||||
<div class=card>
|
||||
<h1>Saved!</h1>
|
||||
<p>Rebooting with new hostname: <strong>)" + String(hostname) + R"(</strong></p>
|
||||
</div>
|
||||
</body></html>
|
||||
)";
|
||||
server.send(200, "text/html", html);
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
return;
|
||||
}
|
||||
}
|
||||
server.send(400, "text/html", "<html><body><h1>Bad Request</h1></body></html>");
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user