ESP8266无睡眠轮询版 + 服务端推送记录
- 固件: 去掉 deepSleep(D0->RST 跳线依赖), 上电常驻每60秒轮询抓取上报; WiFi 掉线自动重连(30s超时), 失败也POST占位body让服务器记录fail - server: /api/raw 成功/失败均记 ReportRec(时间/来源/结果/客户端数/HTTP码), 持久化 reports.csv, /api/site 返回最近50条 - 看板: 新增"最近推送记录"面板(成功/失败徽标), 移动端兼容 - secrets.h.example 同步轮询版配置说明
This commit is contained in:
1 parent
da141e4c79
commit
e76d6984e0
4 files changed
+196
-60
No files matched your search
@@ -1,7 +1,10 @@
|
||||
// ESP8266 低功耗 DHCP 抓取固件
|
||||
// ESP8266 DHCP 抓取固件 —— 无睡眠轮询版
|
||||
// 功能: 登录水星 MW305R 路由器 → GET /userRpm/AssignedIpAddrListRpm.htm
|
||||
// 拿到原始页面 HTML → POST 给 dhcp-mon server 的 /api/raw
|
||||
// → 深度睡眠 SLEEP_SECONDS 秒。
|
||||
// → 常驻轮询,每 SLEEP_SECONDS 秒循环一轮。
|
||||
//
|
||||
// 与深睡版的区别: 不调用 ESP.deepSleep,不需要 D0→RST 跳线,
|
||||
// 上电后一直运行,靠 loop() 里的 delay 控制采集间隔(简单可靠)。
|
||||
//
|
||||
// 解析和上下线判定全部在服务器端(与 README 的 /api/raw 协议一致),
|
||||
// 本固件只做「登录 → 抓页面 → 原样转发」,逻辑极简。
|
||||
@@ -123,6 +126,7 @@ static int reportPage(WiFiClient &wifi, const String &page) {
|
||||
http.addHeader("Content-Type", "text/html; charset=GBK");
|
||||
http.addHeader("X-Token", X_TOKEN);
|
||||
http.addHeader("X-Site", X_SITE);
|
||||
http.addHeader("X-Source", SOURCE_ID);
|
||||
int code = http.POST(page);
|
||||
String resp = http.getString();
|
||||
http.end();
|
||||
@@ -133,52 +137,74 @@ static int reportPage(WiFiClient &wifi, const String &page) {
|
||||
return code;
|
||||
}
|
||||
|
||||
// 确保 WiFi 已连接;未连接则重连(最多等 30s)
|
||||
static bool ensureWiFi() {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
return true;
|
||||
}
|
||||
Serial.printf("[wifi] 连接 %s ...\n", WIFI_SSID);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
delay(100);
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||
uint32_t t0 = millis();
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
if (millis() - t0 > 30000) {
|
||||
Serial.println("[wifi] 连接超时(30s),本轮跳过,下轮重试");
|
||||
return false;
|
||||
}
|
||||
delay(500);
|
||||
}
|
||||
Serial.printf("[wifi] 已连接,IP=%s\n", WiFi.localIP().toString().c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
Serial.println("=== ESP8266 DHCP 采集固件 ===");
|
||||
Serial.println("=== ESP8266 DHCP 采集固件(无睡眠轮询版) ===");
|
||||
|
||||
// 短暂点亮板载 LED 提示本次唤醒
|
||||
// 启动信息: 复位原因 + 上电/复位计数(RTC memory,带 magic 校验)
|
||||
Serial.printf("[boot] 复位原因: %s\n", ESP.getResetReason().c_str());
|
||||
static const uint32_t RTC_MAGIC = 0xDEADBEEF;
|
||||
struct RTCState { uint32_t magic; uint32_t count; } st;
|
||||
ESP.rtcUserMemoryRead(0, (uint32_t *)&st, sizeof(st));
|
||||
if (st.magic != RTC_MAGIC) {
|
||||
st.magic = RTC_MAGIC;
|
||||
st.count = 0;
|
||||
}
|
||||
st.count++;
|
||||
ESP.rtcUserMemoryWrite(0, (uint32_t *)&st, sizeof(st));
|
||||
Serial.printf("[boot] 复位次数: %u\n", st.count);
|
||||
|
||||
// 短暂点亮板载 LED 提示本次上电
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
delay(150);
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
|
||||
// 连接 WiFi(ESP8266 仅支持 2.4GHz)
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||
Serial.printf("[wifi] 连接 %s ...\n", WIFI_SSID);
|
||||
uint32_t t0 = millis();
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
if (millis() - t0 > 20000) {
|
||||
Serial.println("[wifi] 连接超时,直接进入深度睡眠");
|
||||
ESP.deepSleep(SLEEP_SECONDS * 1000000ULL, WAKE_RF_DEFAULT);
|
||||
return; // 不会到达
|
||||
}
|
||||
delay(500);
|
||||
}
|
||||
Serial.printf("[wifi] 已连接,IP=%s\n", WiFi.localIP().toString().c_str());
|
||||
// 首次连接 WiFi;失败不阻塞,loop 里每轮会重试
|
||||
ensureWiFi();
|
||||
|
||||
WiFiClient wifi;
|
||||
// 登录路由器 + 抓页面
|
||||
String page = fetchRouterPage(wifi);
|
||||
if (page.length() > 0) {
|
||||
// 原样转发原始 HTML(服务器端解析 DHCPDynList + 上下线判定)
|
||||
reportPage(wifi, page);
|
||||
} else {
|
||||
Serial.println("[report] 无页面可上报,跳过本轮");
|
||||
}
|
||||
|
||||
// 深度睡眠到下一轮(固定间隔,低功耗)
|
||||
// 注: 自动唤醒需 GPIO16(D0) 短接到 RST;无跳线则按 RST 唤醒。
|
||||
// 单次深睡上限约 3.5 小时,间隔超过请拆成多段循环。
|
||||
Serial.printf("[sleep] 深度睡眠 %u 秒\n", SLEEP_SECONDS);
|
||||
Serial.flush();
|
||||
ESP.deepSleep(SLEEP_SECONDS * 1000000ULL, WAKE_RF_DEFAULT);
|
||||
Serial.printf("[setup] 完成,进入轮询模式,每 %u 秒采集一轮\n", SLEEP_SECONDS);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// setup() 里已深睡,不会到这里;安全兜底再睡一次
|
||||
ESP.deepSleep(SLEEP_SECONDS * 1000000ULL, WAKE_RF_DEFAULT);
|
||||
delay(1000);
|
||||
// 每轮: 确保 WiFi → 抓路由页面 → 上报服务器
|
||||
if (ensureWiFi()) {
|
||||
WiFiClient wifi;
|
||||
String page = fetchRouterPage(wifi);
|
||||
if (page.length() > 0) {
|
||||
// 原样转发原始 HTML(服务器端解析 DHCPDynList + 上下线判定)
|
||||
reportPage(wifi, page);
|
||||
} else {
|
||||
// 抓页面失败也上报一个占位 body,服务器端会记一条“失败”推送记录
|
||||
Serial.println("[report] 抓页面失败,上报错误标记");
|
||||
reportPage(wifi, "<html>device_fetch_failed</html>");
|
||||
}
|
||||
}
|
||||
|
||||
// 等待到下一轮(固定间隔)。delay 期间 ESP8266 后台保持 WiFi 连接。
|
||||
Serial.printf("[loop] 本轮完成, %u 秒后下一轮\n", SLEEP_SECONDS);
|
||||
delay(SLEEP_SECONDS * 1000UL);
|
||||
}
|
||||
@@ -17,11 +17,13 @@ static const char *SERVER_URL = "http://your-vps-ip:8080/api/raw";
|
||||
// ---- 认证与站点(需与 server 的 -stoken 一致)----
|
||||
static const char *X_TOKEN = "与server的-stoken一致";
|
||||
static const char *X_SITE = "home"; // 站点名,如 home/office/shop
|
||||
// 上报来源标识(出现在看板"最近推送记录"),改成便于区分的名字
|
||||
static const char *SOURCE_ID = "esp8266-home";
|
||||
|
||||
// ---- 路由器(Mercury 水星 MW305R)----
|
||||
static const char *ROUTER_HOST = "192.168.1.1";
|
||||
static const char *ROUTER_ADMIN = "admin";
|
||||
static const char *ROUTER_PASS = "123456";
|
||||
|
||||
// ---- 采集间隔(秒): 深度睡眠时长,默认 15 分钟 ----
|
||||
// ---- 采集间隔(秒): 轮询间隔(无睡眠,常驻运行),默认 15 分钟 ----
|
||||
static const uint32_t SLEEP_SECONDS = 900;
|
||||
Reference in new issue
Block a user