Capstone · #18 of 48
Wi‑Fi Transmit Strategy
Batching, Retries, Timeouts, Security
Why it matters
Wi‑Fi is power-hungry and unreliable. Efficient transmission strategy is critical for battery life.
The idea
Power Consumption
Wi‑Fi is expensive:
- Idle: ~20mA
- Connecting: ~80mA
- Turn off when not needed!
Connection Strategy
- Connect once per cycle (not per reading)
- Reuse connection if still valid
- Timeout: Give up after 10 seconds
- Turn off Wi‑Fi immediately after transmit
Transmission Protocol
Options:
- HTTP POST: Simple, works with any server
- MQTT: Efficient, designed for IoT
- HTTPS: Secure but more power (TLS overhead)
Batching
Send multiple readings at once:
- Store readings in RTC memory
- Transmit batch when Wi‑Fi connects
- Reduces connection overhead
Retry Strategy
- Exponential backoff: Wait longer between retries
- Max retries: 3 attempts, then give up
- Error handling: Log failures, continue to sleep
Demo
Wi‑Fi transmission is network communication, not visual. Review this before implementing data transmission.
Key takeaways
- Wi‑Fi is power-hungry — turn off when not needed
- Connect once per cycle, reuse connection if valid
- Use batching to reduce connection overhead
- Implement retry strategy with exponential backoff
Going deeper
For production, use MQTT with QoS level 1 (at least once delivery). For simple projects, HTTP POST to a webhook is sufficient. Always use HTTPS in production (but accept the power cost). Consider using a message queue (like AWS IoT Core) for reliability.
Math details
Power consumption:
Wi‑Fi idle: 20mA × 10s = 200mAs (wasteful!)
Wi‑Fi connect: 80mA × 2s = 160mAs
Wi‑Fi transmit: 170mA × 0.2s = 34mAs
Total: ~194mAs (if Wi‑Fi turned off immediately)
Connection overhead:
TCP handshake: ~100ms
TLS handshake: ~500ms (HTTPS)
HTTP request: ~50ms
Total: ~650ms (HTTP) or ~1150ms (HTTPS)
Batching benefit:
Single reading: 650ms overhead
10 readings: 650ms + (10 × 50ms) = 1150ms
Overhead per reading: 115ms (vs 650ms single)
Implementation
LLM Prompt: Wi‑Fi Transmit with Retry
Write Rust code for ESP32 Wi‑Fi HTTP POST with retry logic.
Include: connect Wi‑Fi (with timeout), HTTP POST request (with retry),
exponential backoff, turn off Wi‑Fi after transmit. Use esp-wifi crate.
Handle errors gracefully — log and continue to sleep.
Lab Exercise
- Set up simple HTTP server (or use webhook service)
- Implement Wi‑Fi connect with timeout (10s)
- Implement HTTP POST with retry (3 attempts)
- Measure power consumption: connect vs transmit
- Test with poor signal — verify retry logic works
Mastery