### Moving Beyond the Arduino loop() Function
Most beginner IoT tutorials place all sensor reading, delay calls, and Wi-Fi requests into the single `void loop()` block. While this works for blinking a single LED on your desk, it completely fails in real-world deployments.
#### The Problem with Blocking Delays
If your Wi-Fi reconnects or experiences a TCP timeout for 3 seconds, your sensor stops sampling, your physical buttons stop responding, and your relay switches might stay stuck in an ON state.
#### The Multithreaded Architecture
With ESP32's dual-core Xtensa processor and FreeRTOS:
1. **Core 0 (Networking Task)**: Dedicated solely to maintaining MQTT/WebSocket connectivity and transmitting outgoing JSON packets.
2. **Core 1 (Sensory & Control Task)**: Strictly samples ADC pins at fixed intervals (e.g. 50ms) using hardware timers and processes emergency shutoff interrupts.
3. **Queue Communication**: Data flows between Core 1 and Core 0 through thread-safe FreeRTOS queues (`xQueueSend` and `xQueueReceive`), eliminating race conditions and ensuring 0 packet drops.
#### Real-World Battery Optimization
By utilizing ESP32's ULP (Ultra Low Power) co-processor and capacitive touch / RTC wakeups, current draw drops from 80mA down to 10µA during idle periods, extending battery lifespan from 2 days to over 4 months on a single 18650 cell.