main.ino
Arduino (C++) · 210 lines
#include <WiFi.h>
#include <WebServer.h>
#include <DNSServer.h>
// -------------------- Wi-Fi Captive Portal settings --------------------
static const char* AP_SSID = "mmWave-Portal";
static const char* AP_PASS = ""; // empty = open network (no login)
static const byte DNS_PORT = 53;
DNSServer dnsServer;
WebServer webServer(80);
// Captive portal IP (ESP32 AP default gateway)
IPAddress apIP(192, 168, 4, 1);
IPAddress netMsk(255, 255, 255, 0);
// -------------------- UART sensor settings --------------------
// Choose pins that are convenient for your ESP32 board.
// Common defaults: RX2=16, TX2=17
static const int SENSOR_RX_PIN = 16; // ESP32 RX (connect to sensor TX)
static const int SENSOR_TX_PIN = 17; // ESP32 TX (connect to sensor RX)
static const uint32_t SENSOR_BAUD = 115200; // adjust if your sensor uses a different baud rate
HardwareSerial SensorSerial(2);
// Last data received from sensor (for web page)
String lastRawLine = "No data yet";
unsigned long lastLineMillis = 0;
// Simple "presence" guess (adjust to your sensor protocol!)
bool presence = false;
// Small helper: escape HTML
String htmlEscape(const String& s) {
String out;
out.reserve(s.length() + 16);
for (size_t i = 0; i < s.length(); i++) {
char c = s[i];
switch (c) {
case '&': out += "&"; break;
case '<': out += "<"; break;
case '>': out += ">"; break;
case '"': out += """;break;
case '\'':out += "'"; break;
default: out += c; break;
}
}
return out;
}
// Try to infer presence from raw line (very basic example).
// Replace this with real parsing once you know the frame format.
void updatePresenceFromLine(const String& line) {
String u = line;
u.toUpperCase();
// Examples of patterns you might see (depends on firmware):
// "PRESENCE:1", "ON", "DETECTED", "HAS_TARGET", etc.
if (u.indexOf("PRESENCE") >= 0 && u.indexOf("1") >= 0) presence = true;
else if (u.indexOf("DETECT") >= 0) presence = true;
else if (u.indexOf("ON") >= 0) presence = true;
else if (u.indexOf("PRESENCE") >= 0 && u.indexOf("0") >= 0) presence = false;
else if (u.indexOf("OFF") >= 0) presence = false;
}
void handleRoot() {
// Main captive portal page
String page =
"<!doctype html><html><head>"
"<meta charset='utf-8'/>"
"<meta name='viewport' content='width=device-width,initial-scale=1'/>"
"<title>mmWave Sensor Portal</title>"
"<style>"
"body{font-family:system-ui,Arial,sans-serif;margin:16px;}"
".card{max-width:720px;padding:16px;border:1px solid #ddd;border-radius:12px;}"
".row{display:flex;gap:12px;flex-wrap:wrap;}"
".pill{display:inline-block;padding:6px 10px;border-radius:999px;border:1px solid #ccc;}"
"pre{white-space:pre-wrap;word-break:break-word;background:#f6f6f6;padding:12px;border-radius:10px;}"
"</style>"
"</head><body>"
"<div class='card'>"
"<h2>mmWave Sensor Captive Portal</h2>"
"<div class='row'>"
"<div class='pill'>AP SSID: <b>" + String(AP_SSID) + "</b></div>"
"<div class='pill'>Device IP: <b>" + apIP.toString() + "</b></div>"
"</div>"
"<p>Live data refreshes automatically.</p>"
"<h3>Status</h3>"
"<div id='status'>Loading...</div>"
"<h3>Last raw UART line</h3>"
"<pre id='raw'>Loading...</pre>"
"</div>"
"<script>"
"async function refresh(){"
" try{"
" const r = await fetch('/data');"
" const j = await r.json();"
" document.getElementById('status').innerHTML = "
" 'Presence: <b>' + (j.presence ? 'YES' : 'NO') + '</b><br>' +"
" 'Last update: <b>' + j.age_ms + ' ms</b>';"
" document.getElementById('raw').textContent = j.last_raw;"
" }catch(e){"
" document.getElementById('status').textContent = 'Error: ' + e;"
" }"
"}"
"setInterval(refresh, 1000);"
"refresh();"
"</script>"
"</body></html>";
webServer.send(200, "text/html", page);
}
void handleData() {
// JSON endpoint for the page
unsigned long age = (lastLineMillis == 0) ? 0 : (millis() - lastLineMillis);
String json = "{";
json += "\"presence\":" + String(presence ? "true" : "false") + ",";
json += "\"age_ms\":" + String(age) + ",";
json += "\"last_raw\":\"" + htmlEscape(lastRawLine) + "\"";
json += "}";
webServer.send(200, "application/json", json);
}
void handleNotFound() {
// Captive portal behavior: redirect any unknown path to "/"
webServer.sendHeader("Location", String("http://") + apIP.toString() + "/", true);
webServer.send(302, "text/plain", "");
}
void setupCaptivePortal() {
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, netMsk);
if (strlen(AP_PASS) == 0) {
WiFi.softAP(AP_SSID); // open
} else {
WiFi.softAP(AP_SSID, AP_PASS);
}
// DNS: respond to ALL domains with our AP IP
dnsServer.start(DNS_PORT, "*", apIP);
// Web server routes
webServer.on("/", handleRoot);
webServer.on("/data", handleData);
webServer.onNotFound(handleNotFound);
webServer.begin();
}
String readLineFromSensor() {
// Read one line (ending with '\n') from UART, non-blocking style
static String buffer;
while (SensorSerial.available()) {
char c = (char)SensorSerial.read();
if (c == '\r') continue;
if (c == '\n') {
String line = buffer;
buffer = "";
line.trim();
return line;
} else {
// Keep buffer from growing forever if sensor sends binary/no newline
if (buffer.length() < 256) buffer += c;
else buffer = ""; // reset on overflow
}
}
return ""; // no complete line yet
}
void setup() {
Serial.begin(115200);
delay(200);
// Start UART for sensor
SensorSerial.begin(SENSOR_BAUD, SERIAL_8N1, SENSOR_RX_PIN, SENSOR_TX_PIN);
setupCaptivePortal();
Serial.println("Captive portal started.");
Serial.print("Connect to AP: ");
Serial.println(AP_SSID);
Serial.print("Open: http://");
Serial.println(apIP);
}
void loop() {
// Captive portal services
dnsServer.processNextRequest();
webServer.handleClient();
// Read sensor data
String line = readLineFromSensor();
if (line.length() > 0) {
lastRawLine = line;
lastLineMillis = millis();
updatePresenceFromLine(line);
// Debug to Serial Monitor
Serial.print("UART: ");
Serial.println(line);
}
}