#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoJson.h> // ArduinoJson v7
// ===================== CAU HINH CAN SUA =====================
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASS = "";
// Dán URL Cloudflare Quick Tunnel, ví dụ https://abc.trycloudflare.com
// Không thêm /api/ingest ở cuối.
const char* API_BASE_URL =
"https://operated-speeches-paper-queries.trycloudflare.com";
// Phải GIỐNG HỆT API_TOKEN trong file .env của website (>= 16 ký tự).
const char* API_TOKEN = "WvhNlXwRec71Rslg335lrtcx26k3Qtgp";
const char* DEVICE_ID = "ESP32-WOKWI-AOCA"; // chỉ dùng A-Z a-z 0-9 _ . -
// ===================== CHAN =====================
#define ONE_WIRE_BUS 4
#define TRIG_PIN 25
#define ECHO_PIN 18
#define DHTPIN 26
#define DHTTYPE DHT22
#define LDR_PIN 34
#define BUZZER_PIN 19
#define RELAY_PUMP 13
#define RELAY_OXY 16 // doi tu GPIO14 (GPIO14 phat xung ngan luc khoi dong tren ESP32 that)
#define RELAY_LIGHT 17
#define RELAY_ON HIGH
#define RELAY_OFF LOW
// ===================== NGUONG =====================
const float TEMP_WATER_MIN = 22.0;
const float TEMP_WATER_MAX = 32.0;
const float TEMP_HYST = 0.5;
const float WATER_LOW_DIST = 15.0;
const float WATER_FULL_DIST = 5.0;
// Module LDR cua Wokwi: AO CAO khi TOI, THAP khi SANG (theo tai lieu Wokwi).
// Nhieu module that (LM393) thi nguoc lai -> doi LDR_DARK_IS_HIGH thanh false.
const bool LDR_DARK_IS_HIGH = true;
const int LIGHT_THRESHOLD = 2500;
const int LIGHT_HYST = 200;
// ===================== AN TOAN MAY BOM =====================
// Bom chay lien tuc qua thoi gian nay se bi ngat va khoa mot luc (de thu nhanh: giam con 30000UL).
const uint32_t PUMP_MAX_RUN_MS = 10UL * 60UL * 1000UL;
const uint32_t PUMP_LOCK_MS = 2UL * 60UL * 1000UL;
// ===================== CHU KY =====================
const uint32_t CYCLE_MS = 2000;
const uint32_t DS_CONV_MS = 800;
const uint32_t LCD_MS = 500;
const uint32_t WIFI_TIMEOUT_MS = 20000;
const uint32_t HTTP_TIMEOUT_MS = 15000;
const uint32_t WIFI_RETRY_MS = 10000;
WebServer server(80);
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// ---- Trang thai (chi loop() / loopTask doc-ghi) ----
float waterTemp = NAN, distanceCm = NAN, airTemp = NAN, airHum = NAN;
int lightVal = 0;
bool waterValid = false, distValid = false, airValid = false;
bool autoMode = true;
bool pumpState = false, oxyState = false, lightState = false;
bool alertTemp = false, alertWater = false, sensorFault = true, pumpTimeout = false, alertState = false;
bool dsPending = false, wifiOk = false;
uint32_t pumpOnSince = 0, pumpLockUntil = 0;
int32_t lastCmdId = 0; // lenh cuoi cung da ap dung (gui kem moi goi tin de backend xac nhan)
uint32_t dsStart = 0, lastCycle = 0, lastLcd = 0, lastWifiRetry = 0;
// ---- Chia se voi task mang (bao ve bang netMutex) ----
struct Cmd { int32_t id; uint8_t type; uint8_t target; bool value; }; // type: 0=mode 1=set; target: 0=pump 1=oxy 2=light
const int MAX_RX = 8;
SemaphoreHandle_t netMutex;
String pendingBody;
bool hasPending = false;
Cmd rxQueue[MAX_RX];
int rxCount = 0;
int32_t serverMaxCmdId = -1;
String ingestUrl;
bool netConfigOk = false;
WiFiClientSecure secureClient; // chi task mang su dung
HTTPClient http;
String numberOrNull(bool valid, float value, int decimals) {
if (!valid || isnan(value)) return "null";
return String(value, decimals);
}
String boolJson(bool value) { return value ? "true" : "false"; }
float round2(float v) { return roundf(v * 100.0f) / 100.0f; }
void applyOutputs() {
digitalWrite(RELAY_PUMP, pumpState ? RELAY_ON : RELAY_OFF);
digitalWrite(RELAY_OXY, oxyState ? RELAY_ON : RELAY_OFF);
digitalWrite(RELAY_LIGHT, lightState ? RELAY_ON : RELAY_OFF);
digitalWrite(BUZZER_PIN, alertState ? HIGH : LOW);
}
// ===================== CAM BIEN =====================
void readFastSensors() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long us = pulseIn(ECHO_PIN, HIGH, 30000UL);
if (us == 0) {
distValid = false;
distanceCm = NAN;
} else {
distanceCm = us * 0.0343f / 2.0f;
distValid = distanceCm > 0 && distanceCm < 500;
if (!distValid) distanceCm = NAN;
}
float t = dht.readTemperature();
float h = dht.readHumidity();
airValid = !(isnan(t) || isnan(h));
if (airValid) { airTemp = t; airHum = h; }
else { airTemp = NAN; airHum = NAN; }
lightVal = analogRead(LDR_PIN);
}
void readWaterTemp() {
float t = sensors.getTempCByIndex(0);
// 85.0 la gia tri DS18B20 tra ve khi vua cap nguon / chua doi xong -> coi la khong hop le.
waterValid = (t != DEVICE_DISCONNECTED_C) && !isnan(t) && t > -55.0f && t < 125.0f && t != 85.0f;
waterTemp = waterValid ? t : NAN;
}
// ===================== DIEU KHIEN =====================
// Dam bao an toan may bom, ap dung cho ca AUTO va MANUAL.
void enforcePumpSafety(uint32_t now) {
if (pumpTimeout && (int32_t)(now - pumpLockUntil) >= 0) pumpTimeout = false;
if (pumpTimeout) pumpState = false;
if (pumpState) {
if (pumpOnSince == 0) pumpOnSince = now ? now : 1;
if (now - pumpOnSince >= PUMP_MAX_RUN_MS) {
pumpState = false;
pumpTimeout = true;
pumpLockUntil = now + PUMP_LOCK_MS;
pumpOnSince = 0;
Serial.println("[SAFE] Bom chay qua thoi gian cho phep -> ngat va khoa");
}
} else {
pumpOnSince = 0;
}
}
void runControl(uint32_t now) {
alertTemp = waterValid && (waterTemp < TEMP_WATER_MIN || waterTemp > TEMP_WATER_MAX);
alertWater = distValid && (distanceCm > WATER_LOW_DIST);
sensorFault = !waterValid || !distValid || !airValid;
if (autoMode) {
// Quat oxy: bat khi nhiet do bat thuong; mat cam bien nhiet -> bat (trang thai an toan).
if (!waterValid) oxyState = true;
else if (alertTemp) oxyState = true;
else if (waterTemp >= TEMP_WATER_MIN + TEMP_HYST &&
waterTemp <= TEMP_WATER_MAX - TEMP_HYST) oxyState = false;
// May bom: mat cam bien muc nuoc -> TAT (khong bom mu).
if (!distValid) pumpState = false;
else if (alertWater && !pumpTimeout) pumpState = true;
else if (distanceCm < WATER_FULL_DIST) pumpState = false;
if (LDR_DARK_IS_HIGH) {
if (lightVal > LIGHT_THRESHOLD) lightState = true;
else if (lightVal < LIGHT_THRESHOLD - LIGHT_HYST) lightState = false;
} else {
if (lightVal < LIGHT_THRESHOLD) lightState = true;
else if (lightVal > LIGHT_THRESHOLD + LIGHT_HYST) lightState = false;
}
}
enforcePumpSafety(now);
alertState = alertTemp || alertWater || sensorFault || pumpTimeout;
}
// Dung chung cho lenh tu web va endpoint noi bo. Tra ve nullptr neu OK, nguoc lai la ly do tu choi.
const char* setManualDevice(uint8_t target, bool value) {
if (autoMode) return "AUTO MODE";
if (target == 0) {
if (value && pumpTimeout) return "PUMP LOCKED";
pumpState = value;
} else if (target == 1) {
oxyState = value;
} else if (target == 2) {
lightState = value;
} else {
return "BAD TARGET";
}
enforcePumpSafety(millis());
applyOutputs();
return nullptr;
}
void applyRemoteCommands() {
Cmd local[MAX_RX];
int n = 0;
int32_t smax = -1;
xSemaphoreTake(netMutex, portMAX_DELAY);
n = rxCount;
for (int i = 0; i < n; i++) local[i] = rxQueue[i];
rxCount = 0;
smax = serverMaxCmdId;
serverMaxCmdId = -1;
xSemaphoreGive(netMutex);
// Backend bi xoa DB (id quay ve nho) -> dong bo lai de khong bo lo lenh moi.
if (smax >= 0 && lastCmdId > smax) {
Serial.printf("[CMD] Backend max id %d < lastCmdId %d -> reset\n", (int)smax, (int)lastCmdId);
lastCmdId = smax;
}
for (int i = 0; i < n; i++) {
if (local[i].id <= lastCmdId) continue; // da ap dung roi (goi tin phan hoi bi gui lai)
if (local[i].type == 0) {
autoMode = local[i].value;
Serial.printf("[CMD #%d] Mode -> %s\n", (int)local[i].id, autoMode ? "AUTO" : "MANUAL");
} else {
const char* err = setManualDevice(local[i].target, local[i].value);
Serial.printf("[CMD #%d] Set target=%d value=%d -> %s\n", (int)local[i].id,
local[i].target, local[i].value, err ? err : "OK");
}
lastCmdId = local[i].id; // luon xac nhan de backend khong gui lai
}
}
// ===================== LCD =====================
void updateLcd() {
char row0[17], row1[17];
char wt[7], ds[7], at[5], ah[5];
if (waterValid) snprintf(wt, sizeof(wt), "%.1f", waterTemp); else strcpy(wt, "--");
if (distValid) snprintf(ds, sizeof(ds), "%.0f", distanceCm); else strcpy(ds, "--");
if (airValid) {
snprintf(at, sizeof(at), "%.0f", airTemp);
snprintf(ah, sizeof(ah), "%.0f", airHum);
} else { strcpy(at, "--"); strcpy(ah, "--"); }
snprintf(row0, sizeof(row0), "TN:%s MN:%s", wt, ds);
snprintf(row1, sizeof(row1), "KK:%sC %s%% %c%s", at, ah, autoMode ? 'A' : 'M', alertState ? "!" : "");
lcd.setCursor(0, 0); lcd.print(" ");
lcd.setCursor(0, 0); lcd.print(row0);
lcd.setCursor(0, 1); lcd.print(" ");
lcd.setCursor(0, 1); lcd.print(row1);
}
// ===================== MANG =====================
String makePayload() {
JsonDocument doc;
doc["device_id"] = DEVICE_ID;
if (waterValid) doc["water_temp"] = round2(waterTemp); else doc["water_temp"] = nullptr;
if (distValid) doc["distance_cm"] = round2(distanceCm); else doc["distance_cm"] = nullptr;
if (airValid) {
doc["air_temp"] = round2(airTemp);
doc["air_humidity"] = round2(airHum);
} else {
doc["air_temp"] = nullptr;
doc["air_humidity"] = nullptr;
}
doc["light_adc"] = lightVal;
doc["auto_mode"] = autoMode;
doc["pump"] = pumpState;
doc["oxy"] = oxyState;
doc["light_relay"] = lightState;
doc["alert_temp"] = alertTemp;
doc["alert_water"] = alertWater;
doc["sensor_fault"] = sensorFault;
doc["pump_timeout"] = pumpTimeout;
doc["alert_state"] = alertState;
doc["last_cmd_id"] = lastCmdId;
// Firmware la nguon su that duy nhat cua cac nguong; website chi hien thi lai.
JsonObject th = doc["thresholds"].to<JsonObject>();
th["temp_min"] = TEMP_WATER_MIN;
th["temp_max"] = TEMP_WATER_MAX;
th["water_low"] = WATER_LOW_DIST;
th["water_full"] = WATER_FULL_DIST;
th["light_threshold"] = LIGHT_THRESHOLD;
th["light_dark_is_high"] = LDR_DARK_IS_HIGH;
String out;
serializeJson(doc, out);
return out;
}
void queueCommand(const Cmd& c) {
xSemaphoreTake(netMutex, portMAX_DELAY);
if (rxCount < MAX_RX) rxQueue[rxCount++] = c;
xSemaphoreGive(netMutex);
}
void postBody(const String& body) {
secureClient.setInsecure(); // Chi dung demo; production phai nap root CA va xac minh chung chi.
http.setReuse(true);
http.setTimeout(HTTP_TIMEOUT_MS);
http.setConnectTimeout(15000);
if (!http.begin(secureClient, ingestUrl)) {
Serial.println("[API] http.begin failed");
return;
}
http.addHeader("Content-Type", "application/json");
http.addHeader("X-API-Token", API_TOKEN);
int code = http.POST(body);
if (code == 200) {
String response = http.getString();
JsonDocument doc;
DeserializationError err = deserializeJson(doc, response);
if (err) {
Serial.printf("[API] 200 nhung JSON loi: %s\n", err.c_str());
} else {
int32_t maxId = doc["max_cmd_id"] | -1;
xSemaphoreTake(netMutex, portMAX_DELAY);
serverMaxCmdId = maxId;
xSemaphoreGive(netMutex);
for (JsonObject c : doc["commands"].as<JsonArray>()) {
Cmd cmd = {c["id"] | 0, 0, 0, false};
const char* type = c["type"] | "";
if (strcmp(type, "mode") == 0) {
cmd.type = 0;
cmd.value = strcmp(c["value"] | "", "auto") == 0;
} else if (strcmp(type, "set") == 0) {
cmd.type = 1;
const char* tg = c["target"] | "";
if (strcmp(tg, "pump") == 0) cmd.target = 0;
else if (strcmp(tg, "oxy") == 0) cmd.target = 1;
else if (strcmp(tg, "light") == 0) cmd.target = 2;
else continue;
cmd.value = c["value"].as<bool>();
} else {
continue;
}
if (cmd.id > 0) queueCommand(cmd);
}
}
Serial.println("[API] HTTP 200");
} else {
String msg = code > 0 ? http.getString() : http.errorToString(code);
Serial.printf("[API] HTTP %d | %s\n", code, msg.c_str());
if (code <= 0) {
char tlsErr[100] = "";
int tlsCode = secureClient.lastError(tlsErr, sizeof(tlsErr));
String h = String(API_BASE_URL);
h.replace("https://", "");
IPAddress ip;
bool dnsOk = WiFi.hostByName(h.c_str(), ip);
Serial.printf("[DBG] URL=%s\n", ingestUrl.c_str());
Serial.printf("[DBG] DNS=%s %s | TLS code=%d (%s) | RSSI=%d\n",
dnsOk ? "OK" : "FAIL", dnsOk ? ip.toString().c_str() : "-", tlsCode, tlsErr, WiFi.RSSI());
secureClient.stop(); // ket noi hong -> lan sau bat tay lai
}
}
http.end();
}
// Chay tren core 0 de HTTPS (bat tay TLS 1-3 giay) khong lam dung LCD / cam bien / dieu khien.
void netTask(void*) {
for (;;) {
String body;
bool have = false;
xSemaphoreTake(netMutex, portMAX_DELAY);
if (hasPending) { body = pendingBody; hasPending = false; have = true; }
xSemaphoreGive(netMutex);
if (have) {
if (!netConfigOk) {
// da in canh bao trong setup()
} else if (WiFi.status() != WL_CONNECTED) {
Serial.println("[API] WiFi disconnected, skip sending");
} else {
postBody(body);
}
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
// ===================== WEB SERVER NOI BO =====================
bool localAuthOk() {
String token = server.header("X-API-Token");
size_t n = strlen(API_TOKEN);
if (token.length() != n) return false;
uint8_t diff = 0;
for (size_t i = 0; i < n; i++) diff |= (uint8_t)(token[i] ^ API_TOKEN[i]);
return diff == 0;
}
void handleRoot() {
String html = "<html><meta charset='utf-8'><h2>ESP32 AO CA IoT</h2>";
html += "<p>Mode: "; html += autoMode ? "AUTO" : "MANUAL"; html += "</p>";
html += "<p>Nhiet do nuoc: " + numberOrNull(waterValid, waterTemp, 1) + " C</p>";
html += "<p>Khoang cach: " + numberOrNull(distValid, distanceCm, 1) + " cm</p>";
html += "<p>Nhiet do KK: " + numberOrNull(airValid, airTemp, 1) + " C</p>";
html += "<p>Do am KK: " + numberOrNull(airValid, airHum, 1) + " %</p>";
html += "<p>LDR: " + String(lightVal) + "</p></html>";
server.send(200, "text/html; charset=utf-8", html);
}
void handleReadings() {
String j = "{";
j += "\"water_temp\":" + numberOrNull(waterValid, waterTemp, 1) + ",";
j += "\"distance_cm\":" + numberOrNull(distValid, distanceCm, 1) + ",";
j += "\"air_temp\":" + numberOrNull(airValid, airTemp, 1) + ",";
j += "\"air_humidity\":" + numberOrNull(airValid, airHum, 1) + ",";
j += "\"light_adc\":" + String(lightVal) + ",";
j += "\"auto_mode\":" + boolJson(autoMode) + ",";
j += "\"pump\":" + boolJson(pumpState) + ",";
j += "\"oxy\":" + boolJson(oxyState) + ",";
j += "\"light_relay\":" + boolJson(lightState) + ",";
j += "\"pump_timeout\":" + boolJson(pumpTimeout) + ",";
j += "\"alert_state\":" + boolJson(alertState) + "}";
server.send(200, "application/json", j);
}
// Cac endpoint dieu khien noi bo yeu cau header X-API-Token (cung token voi backend).
void handleMode() {
if (!localAuthOk()) { server.send(401, "text/plain", "UNAUTHORIZED"); return; }
String value = server.arg("value");
if (value == "auto") autoMode = true;
else if (value == "manual") autoMode = false;
else { server.send(400, "text/plain", "BAD VALUE"); return; }
server.send(200, "text/plain", "OK");
}
void handleToggle() {
if (!localAuthOk()) { server.send(401, "text/plain", "UNAUTHORIZED"); return; }
String target = server.arg("target");
uint8_t idx;
bool next;
if (target == "pump") { idx = 0; next = !pumpState; }
else if (target == "oxy") { idx = 1; next = !oxyState; }
else if (target == "light") { idx = 2; next = !lightState; }
else { server.send(400, "text/plain", "BAD TARGET"); return; }
const char* err = setManualDevice(idx, next);
if (err) { server.send(409, "text/plain", err); return; }
server.send(200, "text/plain", "OK");
}
// ===================== SETUP / LOOP =====================
void setup() {
Serial.begin(115200);
// Dat muc OFF truoc khi chuyen sang OUTPUT de relay khong nhay luc khoi dong.
digitalWrite(RELAY_PUMP, RELAY_OFF); digitalWrite(RELAY_OXY, RELAY_OFF);
digitalWrite(RELAY_LIGHT, RELAY_OFF); digitalWrite(BUZZER_PIN, LOW);
pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RELAY_PUMP, OUTPUT); pinMode(RELAY_OXY, OUTPUT); pinMode(RELAY_LIGHT, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
applyOutputs();
netMutex = xSemaphoreCreateMutex();
Wire.begin(21, 22);
lcd.init(); lcd.backlight();
dht.begin();
sensors.begin();
sensors.setWaitForConversion(false);
// Chuan hoa URL backend va kiem tra cau hinh.
String baseUrl = String(API_BASE_URL);
while (baseUrl.endsWith("/")) baseUrl.remove(baseUrl.length() - 1);
ingestUrl = baseUrl + "/api/ingest";
netConfigOk = true;
if (baseUrl.length() == 0 || baseUrl.indexOf("YOUR-TRY-CLOUDFLARE-URL") >= 0) {
Serial.println("[CFG] Chua cau hinh API_BASE_URL -> khong gui du lieu len website");
netConfigOk = false;
}
if (strncmp(API_TOKEN, "DOI_", 4) == 0 || strlen(API_TOKEN) < 16) {
Serial.println("[CFG] API_TOKEN con la mau/qua ngan -> khong gui du lieu len website");
netConfigOk = false;
}
lcd.setCursor(0, 0); lcd.print("Connecting WiFi");
WiFi.mode(WIFI_STA);
WiFi.setAutoReconnect(true);
WiFi.begin(WIFI_SSID, WIFI_PASS, 6);
uint32_t start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < WIFI_TIMEOUT_MS) {
delay(250);
Serial.print(".");
}
wifiOk = WiFi.status() == WL_CONNECTED;
Serial.println();
if (wifiOk) {
Serial.print("WiFi OK. IP: "); Serial.println(WiFi.localIP());
} else {
Serial.println("WiFi failed; local functions continue.");
}
if (wifiOk) {
String host = String(API_BASE_URL);
host.replace("https://", "");
while (host.endsWith("/")) host.remove(host.length() - 1);
IPAddress ip;
Serial.print("[DBG] DNS "); Serial.print(host); Serial.print(" -> ");
if (WiFi.hostByName(host.c_str(), ip)) Serial.println(ip);
else Serial.println("THAT BAI");
WiFiClientSecure t;
t.setInsecure();
t.setTimeout(15);
Serial.print("[DBG] Ket noi TLS: ");
if (t.connect(host.c_str(), 443)) { Serial.println("OK"); t.stop(); }
else { char e[100]; t.lastError(e, sizeof(e)); Serial.print("THAT BAI: "); Serial.println(e); }
}
const char* headers[] = {"X-API-Token"};
server.collectHeaders(headers, 1);
server.on("/", HTTP_GET, handleRoot);
server.on("/api/readings", HTTP_GET, handleReadings);
server.on("/api/mode", HTTP_POST, handleMode);
server.on("/api/toggle", HTTP_POST, handleToggle);
server.begin();
xTaskCreatePinnedToCore(netTask, "net", 12288, nullptr, 1, nullptr, 0);
lcd.clear();
lcd.setCursor(0, 0); lcd.print("ESP32 AO CA IoT");
lcd.setCursor(0, 1); lcd.print(wifiOk ? "WiFi connected" : "WiFi offline");
delay(1200);
lcd.clear();
lastCycle = millis() - CYCLE_MS;
}
void loop() {
server.handleClient();
uint32_t now = millis();
// Tu dong thu ket noi lai neu Wi-Fi bi mat sau khi khoi dong.
if (WiFi.status() != WL_CONNECTED && now - lastWifiRetry >= WIFI_RETRY_MS) {
lastWifiRetry = now;
Serial.println("[WiFi] Mat ket noi, dang thu ket noi lai...");
WiFi.disconnect();
WiFi.begin(WIFI_SSID, WIFI_PASS, 6);
}
if (!dsPending && now - lastCycle >= CYCLE_MS) {
lastCycle = now;
sensors.requestTemperatures();
dsStart = now;
dsPending = true;
readFastSensors();
}
if (dsPending && now - dsStart >= DS_CONV_MS) {
dsPending = false;
readWaterTemp();
applyRemoteCommands(); // ap dung lenh tu website truoc khi chay logic tu dong
runControl(now);
applyOutputs();
Serial.printf("Water=%.1fC Distance=%.1fcm Air=%.1fC/%.1f%% LDR=%d | Pump=%d Oxy=%d Light=%d | %s | %s%s\n",
waterTemp, distanceCm, airTemp, airHum, lightVal,
pumpState, oxyState, lightState,
autoMode ? "AUTO" : "MANUAL", alertState ? "ALARM" : "SAFE",
pumpTimeout ? " | PUMP-LOCK" : "");
String body = makePayload();
xSemaphoreTake(netMutex, portMAX_DELAY);
pendingBody = body; // ghi de mau chua kip gui (luon gui mau moi nhat)
hasPending = true;
xSemaphoreGive(netMutex);
}
if (now - lastLcd >= LCD_MS) {
lastLcd = now;
updateLcd();
}
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4