SMART FARM · CONTROL · SIMULATOR
Smart Irrigation Simulator
ทดลอง controller cycle ของระบบให้น้ำหลายโซนจาก Measurement → Decision → Command Owner → Runtime → Feedback → Event เพื่อดูว่าเหตุใดระบบจึง Start,…
เริ่มจำลองระบบให้น้ำSystem state
Desired command → transport preview
Preview นี้แปลง command envelope เป็น MQTT message ตาม contract เดียวกับ validator โดยไม่เชื่อมต่อ Broker จริง และ command topic จะไม่ retain
{"schema":"thospaak.smart-irrigation.command","version":1,"commandId":"irrigation:2026-10-15T14:00:00Z:zone-a:2026-10-15T13:55:00Z","issuedAt":"2026-10-15T14:00:00Z","expiresAt":"2026-10-15T14:01:00.000Z","ownerZoneId":"zone-a","desired":{"pumpOn":true,"openValveZoneId":"zone-a"},"provenance":{"source":"soil-policy","intent":"evaluate","requestedAt":"2026-10-15T13:55:00Z"}}MQTT เป็น transport เท่านั้น: message ถูก publish ไม่ได้แปลว่า Pump ทำงานจริง ต้องดู command acknowledgement + Pump/Valve/Flow feedback แยกกัน และ local protection/interlock ยังเป็นคนละชั้น
Boot, clock, watchdog และ reconnect
Deployment preview นี้เป็น firmware-facing contract สำหรับ ESP32/PLC/Gateway: force safe outputs ก่อน network, sync clock ก่อนรับ remote command, ใช้ monotonic lease จำกัดเวลาคำสั่ง และไม่ replay command หลัง reconnect
- 01force-safe-outputs
- 02initialize-command-dedup-cache
- 03synchronize-device-clock
- 04configure-retained-offline-last-will
- 05connect-mqtt-with-tls-auth
- 06publish-retained-online-availability
- 07republish-current-state-and-telemetry
- 08subscribe-command-topic
ACK accepted หมายถึง command ผ่าน validation และถูกจับ lease แล้ว ส่วน applied หมายถึง driver command ถูกเขียนออกไป ไม่ใช่หลักฐานว่า Pump/Valve ทำงานจริง—physical feedback ยังต้องกลับมาผ่าน state/Flow telemetry แยกต่างหาก
Reviewed GPIO + firmware boundary
Reference target นี้ใช้ ESP32-WROOM-32 DevKit สองโซนเป็นตัวอย่างที่ตรวจ pin role ได้จริง: actuator GPIO เป็น 3.3V logic ไปยัง isolated driver/contactor interface เท่านั้น ส่วน analog sensor ใช้ ADC1 เพื่อไม่ชนกับ Wi-Fi
| GPIO | Role | Zone | Direction | Safe / interface note |
|---|---|---|---|---|
| GPIO 25 | pump-command | system | output · active-high | Logic output to an isolated driver/contactor interface only; never drive a pump or mains load directly. |
| GPIO 32 | pump-feedback | system | input | Read isolated auxiliary-contact or low-voltage pump-run feedback. |
| GPIO 34 | flow-pulse | system | input | Input-only GPIO; external pull/bias is required when the flow sensor output needs it. |
| GPIO 35 | tank-analog | system | input | ADC1 input selected so Wi-Fi does not compete with ADC2; external analog conditioning/calibration remains hardware-specific. |
| GPIO 21 | emergency-stop-healthy | system | input | Active-high isolated healthy/permissive input with external pull-down so open wiring or reset fails closed. |
| GPIO 22 | water-supply-permit | system | input | Active-high isolated water-supply/dry-run permit with external pull-down fail-safe bias. |
| GPIO 23 | driver-ready | system | input | Active-high isolated actuator-driver ready feedback; LOW/open blocks energizing commands. |
| GPIO 33 | process-permit | system | input | Active-high local process/manual permit with external pull-down; all four permissives must be healthy. |
| GPIO 26 | valve-command | zone-a | output · active-high | Logic output to an isolated valve-driver interface only; external bias must keep the valve command safe during reset/boot. |
| GPIO 18 | valve-feedback | zone-a | input | Read isolated valve-position or auxiliary-contact feedback when available. |
| GPIO 36 | soil-analog | zone-a | input | ADC1 input; sensor excitation, scaling and calibration are intentionally outside the GPIO contract. |
| GPIO 27 | valve-command | zone-b | output · active-high | Logic output to an isolated valve-driver interface only; external bias must keep the valve command safe during reset/boot. |
| GPIO 19 | valve-feedback | zone-b | input | Read isolated valve-position or auxiliary-contact feedback when available. |
| GPIO 39 | soil-analog | zone-b | input | ADC1 input; sensor excitation, scaling and calibration are intentionally outside the GPIO contract. |
Pin map นี้เป็น reference สำหรับ low-voltage logic interface เท่านั้น ไม่ใช่ wiring diagram สำหรับไฟเมนส์หรือ motor circuit และยังต้องตรวจ schematic ของบอร์ด/driver/contactor/sensor รุ่นจริงก่อน deployment
GPIO trace + sampling + fault injection
Harness แบบ deterministic ใช้ reference profile และ field-node contract เดิมเพื่อพิสูจน์ boot-glitch safety, output transition, ADC/flow conversion และ failure behavior ก่อนมี firmware binary หรือ hardware bench จริง
| Seq | GPIO | Role | Reset / safe level | Effective active | Reason |
|---|---|---|---|---|---|
| 1 | GPIO 25 | pump-command | high-z | NO | power-on-reset-external-bias |
| 2 | GPIO 26 | valve-command · zone-a | high-z | NO | power-on-reset-external-bias |
| 3 | GPIO 27 | valve-command · zone-b | high-z | NO | power-on-reset-external-bias |
| 4 | GPIO 25 | pump-command | low | NO | safe-gpio-init |
| 5 | GPIO 26 | valve-command · zone-a | low | NO | safe-gpio-init |
| 6 | GPIO 27 | valve-command · zone-b | low | NO | safe-gpio-init |
Local interlock ใน harness สามารถ de-energize effective GPIO ได้ทันทีโดยไม่ปลอมว่า controller desired state เปลี่ยนแล้ว ส่วน Wi-Fi/MQTT loss จะไม่สร้าง physical state ปลอม แต่ปล่อยให้ monotonic command lease/watchdog บังคับ safe state ตามเวลา
ESP-IDF / Arduino ports without safety drift
Framework adapter จำกัด side effects ไว้ที่ ports เดียวกันทั้ง ESP-IDF และ Arduino/PlatformIO ส่วน command lease, dedup, readiness, calibration และ fail-safe semantics ยังอยู่ใน core/harness เดิม
Fake-driver regression ใช้ ports ชุดเดียวกันพิสูจน์เส้นทาง MQTT command → field-node acceptance/lease → framework GPIO write → observed GPIO/ADC/pulse feedback → watchdog safe-off โดย framework binding ไม่มีสิทธิ์ bypass local interlock หรือเปลี่ยน command-expiry semantics
Exact API map + safe startup order
Binding plan นี้ล็อก native boundary สำหรับ ESP-IDF 5+: GPIO, ADC oneshot, PCNT, esp_timer, SNTP, NVS, task watchdog และ reset reason ถูก map ไปยัง framework ports แบบตรงตัว โดยไม่มี irrigation decision logic ใน native layer
| Port | ESP-IDF API | Header | Responsibility |
|---|---|---|---|
| gpio | gpio_config · gpio_set_level · gpio_get_level | driver/gpio.h | Configure reviewed GPIO directions and translate high/low reads and writes only. |
| adc | adc_oneshot_new_unit · adc_oneshot_config_channel · adc_oneshot_read | esp_adc/adc_oneshot.h | Read raw ADC1 counts from the reviewed Tank/Soil channels; calibration remains in the framework/core contract. |
| pulseCounter | pcnt_new_unit · pcnt_new_channel · pcnt_unit_enable · pcnt_unit_start · pcnt_unit_get_count | driver/pulse_cnt.h | Expose a monotonically accumulated Flow pulse total; liters-per-minute conversion remains outside the native layer. |
| monotonicClock | esp_timer_get_time | esp_timer.h | Convert ESP-IDF monotonic microseconds to milliseconds for leases, watchdog evaluation, and sampling cadence. |
| wallClock | gettimeofday · gmtime_r | sys/time.h · time.h | Read synchronized wall clock and format it as UTC ISO-8601; never synthesize freshness from publish time. |
| timeSync | esp_netif_sntp_init · esp_netif_sntp_sync_wait · esp_netif_sntp_deinit | esp_netif_sntp.h | Request and observe SNTP synchronization only; command gating remains in the framework/core contract. |
| configStore | nvs_flash_init · nvs_open · nvs_get_str · nvs_get_blob · nvs_close | nvs_flash.h · nvs.h | Load versioned device identity, credential references, CA references, and calibration metadata; no raw secrets are exported. |
| watchdog | esp_task_wdt_add · esp_task_wdt_reset | esp_task_wdt.h | Register/feed the runtime task watchdog; actuator safe-off semantics remain in the harness/field-node contract. |
| resetReason | esp_reset_reason | esp_system.h | Return the raw ESP-IDF reset reason for framework normalization and telemetry. |
- 01external-fail-safe — Hardware pull/down or driver disable keeps Pump/Valve de-energized before firmware runs.
- 02safe-gpio — All reviewed command GPIOs configured to inactive level before network initialization.
- 03config — Versioned config + reference-only credentials must validate before remote control can be enabled.
- 04reset-reason — Reset reason is normalized but never changes safe-start behavior.
- 05network — Connectivity may start only after GPIO safe state is established.
- 06time-sync — Remote command path remains blocked until synchronized wall clock is valid.
- 07mqtt — MQTT command subscription starts only after safe GPIO + validated config + time-sync gate.
- 08sensor-drivers — ADC/PCNT drivers expose raw acquisition only; calibration/freshness stays in the framework/core layer.
- 09watchdog — Runtime task is supervised before command processing loop begins.
- 10runtime — Runtime loop performs port I/O and feeds watchdog while core owns all control semantics.
Repo นี้ยังไม่ได้ compile ESP-IDF binary; checkpoint นี้เป็น typed native-binding contract + regression validator เพื่อกัน API/order/safety drift ก่อนสร้าง firmware project และ Hardware-in-the-Loop milestone จริง
Decision และ runtime ต่อ Zone
Decision เกิดทุก Zone แต่มีเพียง owner ที่ได้ใช้ shared Pump ใน cycle นั้น
| Zone | Request | Soil | Decision | Reason | Owner | Runtime | Fault |
|---|---|---|---|---|---|---|---|
| zone-a | soil-policy · evaluate | 26.0% | start | automatic-start-threshold | OWNER | watering · start-watering | — |
| zone-b | soil-policy · evaluate | 28.0% | start | automatic-start-threshold | QUEUED | idle · idle-no-request | — |
เหตุผลที่ตรวจย้อนหลังได้
Event contract เก็บ decision, feedback, state transition และ command-owner change ต่อเนื่องทุก tick โดยใช้ machine-readable code
{"action":"start","requestSource":"soil-policy","requestIntent":"evaluate","requestedAt":"2026-10-15T13:55:00Z","soilFreshness":"fresh"}{"from":"idle","to":"watering","commandActive":true}{"action":"start","requestSource":"soil-policy","requestIntent":"evaluate","requestedAt":"2026-10-15T13:56:00Z","soilFreshness":"fresh"}{"previousOwner":null,"nextOwner":"zone-a"}State progression หลาย tick
History สรุป owner, feedback และ runtime state ต่อ cycle เพื่อเทียบกับ event timeline และใช้ replay/regression ภายหลัง
| Tick | Time | Owner before | Owner after | Feedback | Tank | Zone states |
|---|---|---|---|---|---|---|
| 1 | 14:00:00 | none | zone-a | unexpected-flow | available | zone-a:watering · zone-b:idle |