Smart Irrigation Control System.
โปรเจกต์ Smart Irrigation ที่แยก Measurement, Decision, Actuation และ Feedback ออกจากกัน พร้อม freshness, hysteresis, offline/manual behavior และ fail-closed control guards
Objective
สร้างแกนควบคุม Smart Irrigation ที่ วัด → ตัดสินใจ → สั่งงาน → ตรวจผล ได้แบบ deterministic และอธิบายเหตุผลของทุก decision ได้ โดยไม่ให้ Dashboard, Cloud หรือ Sensor ตัวเดียวกลายเป็น dependency ที่ทำให้ Pump/Valve ทำงานผิดเมื่อข้อมูล stale หรือระบบบางชั้นล้มเหลว
Project นี้เป็นขั้นถัดจากบท Smart Irrigation System และใช้ Foundation จาก Soil Moisture, Pump/Valve, Pressure/Flow และ Offline/Fail-safe มารวมเป็น behavior ที่ทดสอบได้จริง
System boundary
Soil / Tank / Field Measurements
↓
Quality + Freshness Gate
↓
Irrigation Decision Core
↓
Command Owner / Zone State
↓
Pump + Valve Output
↓
Pump / Valve / Flow Feedback
↓
Verify → Continue / Stop / Fault
↓
Telemetry / Dashboard / Alerts
หลักสำคัญคือ Dashboard ไม่ใช่ control loop และ Cloud ไม่ใช่เจ้าของ state หลักของการให้น้ำแบบ local ใน MVP นี้
Deterministic core
Implementation milestone นี้เพิ่ม pure domain logic ที่ src/lib/smart-irrigation/index.ts, controller-cycle/scenario adapter ที่ src/lib/smart-irrigation/simulator.ts และ validation fixtures ที่ scripts/smart-irrigation/validate-smart-irrigation.mjs เพื่อ lock behavior สำคัญก่อนต่อ Hardware จริง
Core ตอนนี้แยก responsibility หลักออกจากกัน:
evaluateIrrigationDecision()— ตัดสินใจว่าจะ Start / Continue / Stop / Hold / BlockevaluateTankSupply()+evaluateIrrigationDecisionWithTank()— แปลง Tank measurement ที่มี quality/freshness เป็น water-supply guard โดยไม่ลดเหลือ boolean ตั้งแต่ต้นทางselectIrrigationCommandOwner()— เลือกเจ้าของ shared Pump แบบ deterministic ระหว่างหลาย Zone และรักษา active owner จนกว่าจะ releaseevaluateIrrigationFeedback()— ตรวจว่าคำสั่งที่ส่งออกไปเกิดผลจริงหรือไม่จาก Pump, Valve และ Flow feedbackevaluateIrrigationZoneRuntime()— คุม stateIDLE / WATERING / FAULT, maximum duration, latched fault และ recoverycreateIrrigationEvent()— contract สำหรับ Decision / Feedback / State Transition / Command Owner event ที่ชั้น logging ใช้ต่อได้
การแยก responsibility เหล่านี้ทำให้ Command ≠ Physical Result, หลาย Zone ไม่แย่ง shared Pump กัน และ fault ไม่ถูกซ่อนไว้หลัง retry loop อัตโนมัติ
Measurement contract
Measurement ทุกตัวที่มีผลต่อ control ต้องมีอย่างน้อย:
value- engineering
unit timestampของเวลาที่วัดจริงsourceIdquality:good,suspect,invalid- freshness ที่คำนวณจาก
timestampเทียบกับ stale policy ของ measurement นั้น
Freshness มีสามสถานะ:
fresh— ใช้ประกอบ decision ได้ตาม policystale— มีค่าล่าสุดแต่เก่าเกินกำหนด ห้ามแสดงเหมือนเป็น current measurementunavailable— ไม่มีค่า, timestamp ใช้ไม่ได้ หรือ quality เป็น invalid
สำหรับ automatic control ใน MVP นี้ Soil Moisture ต้องเป็นทั้ง fresh และ good มิฉะนั้นระบบ fail closed และไม่เริ่ม watering ใหม่
Tank level ใช้ contract เดียวกันแทน boolean waterSupplyAvailable ใน controller ชั้นใหม่ โดย evaluateTankSupply() แยก available, tank-level-low, tank-data-unavailable และ invalid-tank-policy ออกจากกัน ก่อนส่ง availability เข้า decision core จึงยังเก็บ provenance และเหตุผลของ water-supply guard ไว้ได้
Irrigation decision contract
แต่ละ Zone มี threshold แบบ hysteresis:
startBelowPct < stopAtOrAbovePct
ตัวอย่าง:
Start watering เมื่อ Soil Moisture <= 30%
Continue watering ระหว่าง 30–37%
Stop watering เมื่อ Soil Moisture >= 38%
Hysteresis ป้องกัน relay/pump/valve สลับ ON/OFF ถี่เมื่อค่า Sensor แกว่งใกล้ threshold เดียว
ก่อน Automatic Start ต้องผ่าน guard ต่อไปนี้ทั้งหมด:
- System อยู่ใน
automaticmode - Zone เปิดใช้งาน
- Soil measurement fresh + good
- Water supply พร้อม
- Pump พร้อม
- Valve พร้อม
ถ้า guard ใดไม่ผ่าน Decision ต้องบอกเหตุผลที่ machine-readable ได้ เช่น:
soil-data-unavailablewater-supply-unavailablepump-unavailablevalve-unavailablezone-disabledsystem-disabled
Multi-zone command ownership
เมื่อหลาย Zone ใช้ Pump ร่วมกัน Decision ของแต่ละ Zone ยังไม่ควรสั่ง Hardware โดยตรง เพราะอาจเกิดคำสั่งชนกันได้
selectIrrigationCommandOwner() รับ candidate ของแต่ละ Zone แล้วเลือก owner เพียงหนึ่ง Zone ตาม policy ที่ deterministic:
- ถ้า Zone ปัจจุบันยัง
continuewatering ได้ ให้รักษา owner เดิมก่อนเพื่อไม่ preempt กลางรอบ - ถ้า owner เดิม release แล้ว ให้เลือก request ที่
startได้ตาม priority สูงสุด - ถ้า priority เท่ากัน ใช้ request ที่เก่ากว่าก่อน แล้วใช้
zoneIdเป็น deterministic tie-breaker - candidate ที่ timestamp ใช้ไม่ได้หรือ decision ไม่ใช่
startจะไม่ชิง ownership
ดังนั้น shared Pump มี command owner ชัดเจน และ Zone อื่นต้องรอรอบถัดไปแทนการเปิดหลาย Valve/Pump policy พร้อมกันโดยไม่ตั้งใจ
Manual mode
manual mode ใน domain core ไม่แปลว่า bypass protection แล้วบังคับ Pump ON แต่หมายถึง automatic soil-threshold policy หยุดเป็น command owner และรอคำสั่ง operator จาก control layer ที่แยกต่างหาก
Manual command ใน implementation ถัดไปยังต้องผ่าน minimum hard guards เช่น actuator availability, timeout และ electrical/mechanical protection ที่เหมาะกับ hardware จริง
Feedback verification
เมื่อ command active ระบบต้องตรวจ Physical Feedback แยกจาก Command State:
Command active
↓
Pump feedback ON?
↓
Valve feedback OPEN?
↓
Flow fresh + plausible?
↓
Watering verified
MVP feedback states:
watering-verifiedpump-feedback-mismatchvalve-feedback-mismatchno-flowhigh-flowfeedback-unavailableunexpected-flowidle-verified
ถ้ากำลังสั่ง watering แล้ว feedback stale, Pump/Valve state ไม่ตรง หรือ Flow อยู่นอก expected range ระบบ core ส่ง shouldStopCommand = true เพื่อให้ control layer หยุด command และเข้าสู่ fault handling แทนการเดินต่อแบบ blind control
unexpected-flow ขณะ command inactive เป็นคนละกรณีกับ high-flow ระหว่าง watering และควรถูกใช้เป็น signal สำหรับ leak, valve leakage หรือ state mismatch ในระบบจริง
Control state machine
evaluateIrrigationZoneRuntime() ทำ state transition ระดับ Zone แบบ explicit:
IDLE
↓ start request + guards pass
WATERING
↓ target reached
IDLE
WATERING
↓ feedback fault / blocked critical input / maximum duration
FAULT
↓ condition cleared + operator acknowledge
IDLE
ก่อนเริ่ม watering runtime policy และ clock ต้อง valid และเมื่อเข้า WATERING จะจำ wateringStartedAt เพื่อบังคับ maximumWateringSeconds ไม่ให้ Pump เดินต่อแบบไม่จำกัด แม้ Soil target ยังไม่ถึง
Fault จาก no-flow, high-flow, feedback mismatch, stale feedback, critical input หรือ timeout จะ latched อยู่ใน FAULT จนกว่าจะมีทั้ง faultConditionCleared และ faultAcknowledged จึงกลับ IDLE ได้ ไม่มี automatic retry ที่ซ่อนสาเหตุ
ขณะ IDLE ถ้าพบ unexpected-flow ระบบก็เข้า FAULT ได้ แม้ไม่มี active command เพราะอาจหมายถึง valve leakage, pipe state mismatch หรือ flow ที่ไม่ได้เกิดจาก command owner
Event contract
createIrrigationEvent() กำหนด event shape กลางสำหรับ decision, feedback, state-transition และ command-owner โดยทุก event ต้องมี timestamp, zoneId, machine-readable code และ details ที่ serialize ได้
MVP นี้ยังไม่เลือก database หรือ retention policy แต่ lock contract ก่อนเพื่อให้ Simulator/Dashboard และ persistent event store ในอนาคตใช้ความหมายเดียวกัน
Monitoring ≠ Control ≠ Safety
Project นี้รักษา boundary สามชั้นให้ชัด:
- Monitoring — แสดง measurement, freshness, state, events และ alarms
- Control — policy สำหรับ request, command ownership, sequence และ feedback verification
- Safety/Protection — breaker, overload, motor protection, dry-run protection หรือ interlock ที่ hardware/process ต้องมีตามการออกแบบจริง
Software automation ไม่ถูกใช้แทน electrical protection หรือ mechanical safety device
Validation fixtures
Validation ปัจจุบัน lock อย่างน้อยกรณีต่อไปนี้:
- Fresh / stale / invalid / future-dated measurement
- Soil hysteresis: Start / Hold / Continue / Stop
- Manual / Disabled / Zone disabled
- invalid threshold และ invalid runtime policy
- Tank fresh/low/stale/invalid-policy และการ map เข้า water-supply guard
- shared Pump owner selection, priority, oldest-request tie-break และ owner retention
- maximum watering duration และ invalid runtime clock
- explicit
IDLE / WATERING / FAULTtransition - latched fault + acknowledgement/recovery
- Pump/Valve feedback mismatch
- No-flow / High-flow / stale flow feedback
- Unexpected flow ขณะไม่มี watering command
- Event contract validation
Fixture ชุดนี้ตั้งใจให้เป็น contract สำหรับ Simulator/App/Controller layer ถัดไป ไม่ใช่ simulation ของ sensor/hardware รุ่นใดรุ่นหนึ่ง
Simulator / Dashboard MVP
/apps/smart-irrigation-simulator/ ใช้ controller-cycle adapter เดียวกับ Project และเปิด deterministic scenarios ให้ตรวจ behavior ได้โดยไม่เขียน decision rules ซ้ำใน UI ปัจจุบันครอบคลุม:
- multi-zone start และ shared-pump owner selection
- verified watering ที่ตรวจ Pump / Valve / Flow feedback
- low tank ที่ block automatic start
- stale soil ที่ห้ามใช้ค่าครั้งสุดท้ายเป็น current measurement
- no-flow fault และ owner release
- maximum watering timeout
- acknowledged fault recovery โดยไม่ restart Pump อัตโนมัติ
- multi-tick progression ที่ carry state, owner และ latched fault ข้าม cycle
- configurable Soil/Tank/Flow value, age, quality และ Pump/Valve feedback
- operator acknowledge + condition-cleared actions ผ่าน simulator contract ก่อนให้ runtime evaluate recovery ใน tick ถัดไป
- session event/history timeline ต่อเนื่อง และ JSON export/import สำหรับ replay/regression case
- request provenance แยก
soil-policy,schedule-weatherและoperator-manualพร้อม intent/request timestamp ใน Decision/Event/History - Manual Start/Stop contract ที่ข้าม Soil policy ได้แต่ยังห้ามข้าม Tank/Pump/Valve hard guards, maximum duration และ feedback verification
- deterministic owner handoff เมื่อ owner เดิม stop/target reached แล้วมี Zone อื่นรอ shared Pump อยู่
- dynamic Zone controls ที่ render จาก session configuration และรองรับ scenario มากกว่า Zone A/B
- regression cases สำหรับ 3-zone priority, owner handoff, Schedule/Weather request และ stale-during-watering
- field command envelope ที่แยก
desired Pump/Valve stateออกจาก observed feedback และ carry owner/request provenance ไปถึง field adapter boundary - Schedule/Weather signal adapter ที่รับ
sourceId + intent + timestamp + qualityและ normalize เป็น control request เฉพาะเมื่อ signal fresh/good ตาม policy - temporal ingestion guard ที่รับ telemetry/request ใหม่กว่าเท่านั้น และ reject duplicate, conflicting-same-timestamp, invalid timestamp และ out-of-order update
- communication-failure scenarios สำหรับ stale Schedule/Weather และ delayed Flow telemetry โดย fail closed ก่อนสั่ง actuator ต่อ
Measurement timestamp จะไม่ถูก refresh อัตโนมัติเมื่อกด Step ดังนั้นข้อมูลที่ไม่ update จะค่อย ๆ กลายเป็น stale ตาม policy จริง แทน Simulator ที่ทำให้ Sensor ดู fresh ตลอดเวลาโดยไม่ตั้งใจ
Replay schema ปัจจุบันเป็น version 3 เพราะ lastResult รวม field command envelope แล้ว Payload v1/v2 จึง fail closed แทนการเดาว่า contract เก่ายัง compatible
Field I/O boundary
Controller-cycle output มี command envelope version 1 เป็น desired state เท่านั้น:
issuedAtและ deterministiccommandIdownerZoneIddesired.pumpOndesired.openValveZoneId- request provenance: source / intent / requestedAt
Envelope นี้ไม่ใช่หลักฐานว่า Pump หรือ Valve ทำงานจริง การยืนยันฝั่ง process ยังมาจาก observed feedback แยกกัน ได้แก่ Pump feedback, Valve feedback และ Flow measurement พร้อม freshness/quality contract เดิม ดังนั้น Command ON ≠ Pump ON ≠ Water Flow ยังคงเป็น boundary หลักของระบบ
Schedule/Weather ฝั่ง input ก็ไม่ส่ง boolean เข้ากฎควบคุมโดยตรง แต่ผ่าน adapter contract ที่มี sourceId, intent, timestamp, quality และ stale policy ก่อน normalize เป็น schedule-weather request หาก signal stale, suspect/invalid, future/invalid timestamp หรือ policy ผิด ระบบจะไม่สร้าง start request ที่ใช้งานได้
สำหรับ field telemetry และ external request ที่มาถึงหลายครั้ง adapter layer ใช้ timestamp ordering ก่อนเปลี่ยน controller state: newer update เท่านั้นที่ถูก accept; duplicate ถูก ignore แบบ idempotent; same-timestamp conflict และ out-of-order update ถูก reject โดยไม่ย้อนค่าปัจจุบันกลับไปหา packet เก่า และ Schedule/Weather จะไม่แทรกทับ active Operator Manual request
MQTT field adapter contract
Field integration milestone ถัดมาลงรายละเอียด transport ด้วย src/lib/smart-irrigation/mqtt.ts โดยยังไม่เชื่อม Broker จริง เพื่อให้ contract ของ ESP32/Gateway review ได้ก่อน deployment:
- namespace หลัก:
site/{siteId}/irrigation/{controllerId} - command topic แยกจาก state/telemetry/availability/ack
- command ใช้ desired-state envelope เดิม, มี
commandId,issuedAt,expiresAtและ provenance - command ไม่ retain และมี expiry guard เพื่อไม่ให้ reconnect แล้ว execute คำสั่งเก่า
- telemetry/state payload มี schema version, source ID, timestamp และ quality
- retained telemetry ยังต้องผ่าน timestamp freshness; retained ไม่ได้แปลว่า current
- Pump/Valve state, availability และ command acknowledgement เป็นคนละ message class
- ACK lifecycle แยก
pending → accepted → applied/rejectedและมี timeout; ACK ต้อง matchcommandIdเดิมและ ACK ที่ retained จะถูก reject - Last Will ใช้ retained
offlineเพื่อ fail-safe ส่วน retainedonlineต้องยัง fresh; stale offline ยังคง inhibit remote command จนกว่าจะมี fresh online ใหม่ - reconnect policy คือ never replay command; หลัง reconnect ให้ publish current telemetry/state snapshot ใหม่แทนการ replay desired command เก่า
- Availability gate ใช้เฉพาะ remote-command path และไม่เขียนทับ physical Pump availability, local interlock หรือ protection state
- duplicate/out-of-order telemetry ยังต้องผ่าน temporal guards เดิมก่อนเปลี่ยน controller state
Simulator แสดง MQTT publish preview และมี inbound field-node simulation สำหรับ Online/Last Will, Publish Command, ACK Accepted/Applied/Rejected, ACK timeout และ Pump/Valve/Flow feedback โดย message ทุกตัวผ่าน decoder/bridge contract เดียวกับ validator ก่อนอัปเดต canonical simulator state ทำให้ตรวจได้ครบว่า Controller desired command → MQTT transport → ACK → observed state/telemetry → Controller feedback เป็นคนละช่วงของระบบ
Field-node deployment contract
src/lib/smart-irrigation/field-node.ts ปิด boundary ฝั่ง firmware สำหรับ ESP32/PLC/Gateway โดยยังไม่ผูกกับ GPIO/Relay board รุ่นใดรุ่นหนึ่ง:
- boot ต้อง force Pump OFF + close Valve ก่อน network แล้วจึง init dedup cache → sync clock → configure retained offline Last Will → connect MQTT ด้วย TLS/Auth → publish online/current state → subscribe command
- remote command ถูก reject จนกว่า device clock sync แล้ว เพื่อให้
issuedAt/expiresAtมีความหมายจริง - หลังรับ command แล้ว field node ใช้ monotonic command lease จำกัดเวลาทำงานอีกชั้น ต่อให้ wall clock หรือ network มีปัญหา Pump/Valve ก็ไม่ค้าง ON ไม่จำกัดเวลา
- local interlock ยังมีอำนาจ block Start; Safe OFF command ต้องยัง apply ได้แม้ start interlock ไม่พร้อม
commandIdถูกเก็บใน bounded dedup cache; duplicate payload ไม่ apply ซ้ำ และ commandId เดิมที่ payload ขัดกันถูก reject โดยไม่ทำลาย record เดิม- ACK
acceptedหมายถึง command ผ่าน validation/lease แล้ว ส่วน ACKappliedหมายถึง driver command ถูก apply เท่านั้น ไม่ใช่ physical proof; Pump/Valve/Flow feedback ยังแยกเป็น evidence อีกชั้น - MQTT security contract ใช้
mqtts://, per-device client identity และ credential/CA references เท่านั้น ไม่อนุญาต inline secret ใน URL/frontend/source contract - reconnect snapshot publish Availability + current Pump/Valve state + current telemetry ใหม่ แต่ ไม่ replay command และรักษา measurement timestamp เดิมเพื่อไม่ทำ stale data ให้ดู fresh
Simulator เพิ่ม Field Node Contract preview เพื่อให้ review boot order, TLS boundary, command lease และ reconnect policy ได้จากหน้า App เดียวกัน
อ่านหลักการ MQTT ที่รองรับ contract นี้ได้ที่ ESP32 + MQTT + Home Assistant และ MQTT Protocol
ESP32 reference field node
PR75 เริ่ม hardware-specific reference implementation บน ESP32-WROOM-32 DevKit แบบ 2 โซน ผ่าน src/lib/smart-irrigation/esp32/reference.ts โดยจงใจจำกัด scope เป็น low-voltage logic/isolated I/O contract ไม่ใช่วงจรไฟเมนส์หรือ motor drive:
- Pump command ใช้ GPIO25 และ Valve command ใช้ GPIO26/27 เป็น 3.3V logic ไปยัง isolated driver/contactor interface เท่านั้น
- Pump/Valve feedback เป็น low-voltage isolated input แยกจาก command เพื่อยืนยัน actual state
- Flow pulse ใช้ GPIO34, Tank analog ใช้ GPIO35 และ Soil analog ใช้ GPIO36/39; analog measurement อยู่บน ADC1 เพื่อเลี่ยง ADC2/Wi-Fi contention ของ ESP32 classic
- validator block duplicate GPIO, flash/strapping misuse, input-only pin ที่ถูกใช้เป็น output และ analog input ที่หลุดไป ADC2 ขณะ Wi-Fi เปิด
- GPIO34–39 และ command outputs ต้องพึ่ง external bias/conditioning ตามวงจรจริง; firmware ไม่ถือว่า internal pull-up/down คือ fail-safe hardware
- startup phase บังคับ
safe-gpio-init → sensor/feedback init → time sync → MQTT TLS connect → state republish → command subscribe → runtime loop - runtime loop ต้อง feed hardware watchdog, ตรวจ monotonic command lease, force safe outputs เมื่อ lease/interlock fail และ process MQTT โดยไม่ block local safety
- credential/CA เป็น per-device reference จาก secure storage/provisioning path; inline credentials ยังถูกห้าม
Simulator แสดง ESP32 pin map, electrical boundary และ firmware startup/failure policy เพื่อ review implementation ก่อนมี firmware binary หรือ hardware bench จริง
Bench-testable ESP32 harness
PR75 เพิ่ม src/lib/smart-irrigation/esp32/harness.ts เพื่อให้ firmware boundary ถูกทดสอบแบบ deterministic ก่อนเลือก framework/driver จริง:
- fake GPIO driver เก็บ trace ของ Pump/Valve command pins ตั้งแต่ reset
high-z→ safe initialized level → applied command → forced-safe transition - boot-glitch guard บังคับให้ command output ทุกขามี external fail-safe bias; profile ที่ขาด bias จะไม่ถูกยอมรับเป็น harness ที่ปลอดภัย
- local interlock สามารถ de-energize effective GPIO outputs ได้ทันที โดยไม่ปลอมว่า controller/field-node desired state เปลี่ยนแล้ว
- Wi-Fi/MQTT loss ไม่ถูกตีความว่า Pump OFF โดยอัตโนมัติ; output ที่กำลังทำงานอยู่จะถูกจำกัดด้วย monotonic command lease แล้ว watchdog บังคับ safe state เมื่อ lease หมด
- reboot/brownout/watchdog reset กลับเข้าสู่ reset phase พร้อม safe effective outputs และ command pins แบบ high-Z ที่ต้องพึ่ง external hardware bias ตาม contract
- ADC sampling สำหรับ Tank/Soil ใช้ calibration contract แยก raw count ออกจาก engineering unit; sample นอก calibrated span เป็น
suspectและ raw/timestamp/calibration ที่ผิดเป็นinvalid - Flow sampling ใช้ pulse delta + sampling interval + pulses-per-liter; counter rollback หรือ calibration/interval ผิด fail closed และค่าที่เกิน expected range ถูก mark
suspect - acquisition timestamp ถูกเก็บเป็น timestamp ของ measurement จริง ไม่ใช้เวลาที่ publish มาแต่งข้อมูลเก่าให้ดู fresh
Regression fixtures ครอบคลุม boot-glitch, command apply, interlock trip, Wi-Fi/clock fault, lease expiry, brownout safe restart, ADC calibration และ Flow pulse behavior แล้ว Simulator มี Bench Harness preview สำหรับ trace/sampling/fault matrix ชุดเดียวกัน
Framework-facing ESP32 adapter
PR75 เพิ่ม src/lib/smart-irrigation/esp32/framework.ts เป็น port boundary สำหรับ ESP-IDF และ Arduino/PlatformIO โดย framework binding มีหน้าที่ทำ side effect เท่านั้น ไม่ได้ถือ control/safety logic ชุดใหม่:
- ports แยก
GPIO,ADC,pulse counter,monotonic clock,wall clock/SNTP,NVS config store,hardware watchdogและreset reasonเพื่อให้ fake driver กับ framework จริงใช้ contract เดียวกัน - boot อ่าน reset reason แล้ว normalize เป็น
power-on / software-reset / brownout / watchdog-reset; ทุกกรณียังเริ่มจาก externally-biased high-Z → safe GPIO init เหมือนกัน - remote command ยังถูก block จน SNTP/wall clock synchronized; wall-clock jump เกิน tolerance ถูก mark
resync-requiredแทนการปล่อยให้ command expiry เลื่อนเงียบ ๆ - persisted config เก็บ
credentialRef/caCertificateRef+ calibration เท่านั้น; safe config export มีsecretMaterialIncluded: falseและ validator reject inline password/token/private-key material - framework command path เรียก harness/field-node acceptance + command lease เดิม แล้วค่อย flush GPIO trace ไปยัง driver port; framework ไม่มีทาง bypass dedup, expiry, local interlock หรือ watchdog semantics
- sensor path อ่าน ADC1/pulse counter/digital feedback ผ่าน ports แล้วส่งกลับ canonical
IrrigationMeasurementพร้อม acquisition timestamp และ calibration เดิม - fake-driver regression พิสูจน์ครบ
MQTT command → field-node lease → framework GPIO → observed feedback/ADC/Flow → watchdog safe-offรวม SNTP gate, clock-jump guard และ brownout/reset mapping
Simulator เพิ่ม Framework Adapter preview เพื่อ review target frameworks, port surface, SNTP gate และ safe config boundary จากหน้าเดียวกัน
ESP-IDF native binding contract
PR75 ปิด software/reference boundary ด้วย src/lib/smart-irrigation/esp32/idf-binding.ts ซึ่ง map framework ports ไปยัง ESP-IDF 5+ API แบบ explicit:
- GPIO →
gpio_config,gpio_set_level,gpio_get_level - ADC1 →
adc_oneshot_* - Flow pulse → PCNT
pcnt_* - monotonic lease clock →
esp_timer_get_time - wall clock →
gettimeofday - SNTP →
esp_netif_sntp_* - NVS →
nvs_* - task watchdog →
esp_task_wdt_* - reset reason →
esp_reset_reason
Startup contract ล็อกลำดับ external fail-safe → safe GPIO → validated config → reset reason → network → SNTP → MQTT → sensor drivers → watchdog → runtime และ validator block native mapping ที่ย้าย network ก่อน safe GPIO, MQTT ก่อน time sync, runtime ก่อน watchdog หรือเปิดช่องให้ native layer bypass local interlock/replay command/inline secret
Native layer จึงถือเฉพาะ driver calls, raw I/O, clock/config/watchdog/reset reads ส่วน irrigation decision, request provenance, zone arbitration, command expiry/lease policy, interlock semantics, fault recovery และ measurement freshness ยังคงเป็น ownership ของ core/harness เดิม Repo นี้ยัง ไม่ได้ compile ESP-IDF firmware binary เพราะไม่มี ESP-IDF toolchain ใน web workspace; binding นี้เป็น typed source-of-truth + regression guard ก่อนสร้าง firmware project จริง
ESP-IDF firmware scaffold + low-voltage HIL contract
PR76 เริ่ม milestone ใหม่ด้วย firmware/smart-irrigation-esp32-idf/ ซึ่งเป็น ESP-IDF project scaffold จริงใน repo แทนการหยุดอยู่ที่ TypeScript binding manifest:
app_main.cรักษา startup order แบบ fail-closed: external fail-safe → safe command GPIO → local permissive inputs → NVS config/reference → reset reason → network → SNTP → MQTT → sensor drivers → watchdog → runtime- native ports ใช้ GPIO25/26/27 สำหรับ command logic, GPIO32/18/19 สำหรับ isolated feedback, GPIO34 + PCNT สำหรับ Flow, GPIO35/36/39 + ADC1 oneshot สำหรับ Tank/Soil และ GPIO21/22/23/33 สำหรับ active-high Emergency healthy / Water-supply permit / Driver-ready / Process-permit ซึ่งทุกเส้นต้องมี external pull-down fail-safe bias;
esp_timer_get_time()เป็น monotonic lease clock,esp_netif_sntp_init()ใช้ time sync,nvs_*ถือ provisioning data และesp_task_wdt_*/esp_reset_reason()ใช้ watchdog/reset telemetry - NVS init fail closed และไม่ erase config อัตโนมัติ; source/
sdkconfig.defaultsไม่มี Wi-Fi/MQTT secret material - Provisioning แยก non-secret config (
site/controller/source,mqtts://broker URI และnvs:<namespace>refs) ออกจาก Wi-Fi/MQTT credentials + CA PEM จริง; secret material ถูก resolve จาก referenced NVS namespaces ตอน boot และไม่มีค่า secret ฝังใน source/sdkconfig.defaults/evidence - Native Wi-Fi station binding ใช้
esp_netif+ default event loop +esp_wifi_*และรอ IP แบบ bounded/retry ก่อนเข้าสู่ SNTP gate - Native
esp-mqttbinding ใช้ CA verification, retained Last Will/Online availability, auto reconnect, QoS 1 command subscription และ current Pump/Valve state republish หลัง reconnect โดยไม่ replay command เก่า - command decoder ใช้ wire contract
thospaak.smart-irrigation.commandv1 เดิม: ตรวจ schema/version, expiry/lease, owner↔desired consistency, provenance, supported zone และ commandId fingerprint/dedup ก่อน GPIO ทุกครั้ง; retained/fragmented/oversized/conflicting command ถูก reject - safe-OFF command ตอบ
accepted → appliedได้โดยไม่พึ่ง permissive ส่วน Pump-ON/Valve-open ต้องเห็น local permissive ทั้ง 4 HIGH ก่อน apply; เมื่อ apply แล้ว firmware แปลง wall-clock expiry เป็น local deadline จากesp_timer_get_time()และruntime_tickจะ force Pump/Valve command OFF ทันทีเมื่อ permissive ใดตก, monotonic time regression หรือ lease หมด โดย reconnect/duplicate command ไม่ต่ออายุ lease - provisioning workflow เพิ่ม custom
nvspartition 0x6000, committed CSV example ที่ไม่มี secret และprovisioning/local/ที่ถูก gitignore; operator สร้าง NVS image ด้วย ESP-IDF NVS generator แล้วเขียนเฉพาะ partitionnvsผ่านparttool.pyโดยไม่ commit credential/CA material hil/bench-manifest.jsonv3 จำกัด HIL รอบแรกไว้ที่ 3.3V/SELV ≤24V + isolated/dummy I/O เท่านั้น และเพิ่ม physical permissive, energizing dummy-output, interlock-drop, lease-expiry และ reconnect-no-replay caseshil/evidence-template.jsonv3 แยก build + flash evidence (idf.py build, binary SHA-256, serial/log refs) ออกจาก physical cases และเริ่มทุกสถานะเป็นpending; CI ไม่ยอมให้ repo แกล้งประกาศ board/HIL pass ก่อนมี evidence จริง
Web workspace นี้ยังไม่มี idf.py toolchain จึงตรวจได้เฉพาะ source/project/static contract และ Astro regression; ยัง ไม่มี compiled firmware binary, flash evidence หรือ physical HIL pass ใน PR76 ตอนนี้
Next implementation slice
งานถัดไปใน PR76 ถ้ายังอยู่ firmware/HIL domain เดิมคือ นำ branch นี้ไป compile/flash บน ESP-IDF host + ESP32-WROOM-32 จริง และเติม build/flash/HIL evidence v3 ตาม low-voltage runbook เท่านั้น เพราะ physical permissive + local monotonic lease + provisioning workflow อยู่ใน source แล้ว หลัง HIL ผ่านจึงค่อยเลือก actual isolated relay/contactor/feedback/sensor-conditioning hardware และขยับไป process hardware จริง
Worker/Database/device registry ยังไม่ควรสร้างจน deployment จริงบังคับให้ต้องมี remote ingestion/history/notification


