Compare commits

...

4 Commits

Author SHA1 Message Date
a46604d970 Updated cmds 2025-07-22 16:28:21 -06:00
1cd35970c1 Added mqtt docs 2025-07-20 12:05:58 -06:00
d4fb078a40 Updated after completion of scheduler 2025-07-20 12:05:16 -06:00
9ca88df1cb Added project plan 2025-07-20 12:04:32 -06:00
3 changed files with 725 additions and 0 deletions

413
MQTT_README.md Normal file
View File

@ -0,0 +1,413 @@
# MQTT Topics Reference
Complete reference for all MQTT topics used by the ESP32-S3 Plant Watering System.
## Topic Structure Overview
```
plant_watering/
├── status # System online/offline status
├── pump/ # Pump control and monitoring
│ ├── 1/ # Pump 1
│ │ ├── set # Control commands
│ │ ├── state # Current state
│ │ ├── speed # Speed control
│ │ ├── stats # Runtime statistics
│ │ ├── runtime # Current runtime (when running)
│ │ └── cooldown # Cooldown status
│ └── 2/ # Pump 2 (same structure)
├── moisture/ # Moisture sensor readings
│ ├── 1 # Sensor 1 percentage
│ └── 2 # Sensor 2 percentage
├── schedule/ # Scheduling system
│ ├── status # Global scheduler status
│ ├── summary # Schedule summary (on demand)
│ ├── time/set # Manual time setting
│ ├── 1/ # Pump 1 schedules
│ │ ├── trigger # Manual trigger all schedules
│ │ ├── get # Get all schedules for pump
│ │ ├── executed # Execution notification
│ │ └── 0-3/ # Schedule slots
│ │ ├── config # Schedule configuration
│ │ ├── status # Schedule status (periodic)
│ │ └── current # Schedule details (on demand)
│ └── 2/ # Pump 2 (same structure)
├── system/ # System information
│ ├── time # Time response (on demand)
│ └── current_time # Current time (periodic)
├── commands/ # System commands
│ ├── emergency_stop # Stop all pumps immediately
│ ├── test_mode # Enable/disable test mode
│ ├── holiday_mode # Enable/disable all schedules
│ ├── get_time # Request current time
│ ├── get_schedules # Request all schedules
│ └── test_pump/ # Test pump operation
│ ├── 1 # Test pump 1
│ └── 2 # Test pump 2
├── settings/ # Runtime configuration
│ └── pump/
│ └── 1-2/
│ ├── max_runtime # Maximum runtime per cycle
│ ├── min_interval # Minimum time between runs
│ ├── min_speed # Minimum speed percentage
│ └── max_speed # Maximum speed percentage
└── alerts/ # System alerts
├── pump_error/1-2 # Pump errors
└── schedule_error/1-2 # Schedule execution errors
```
## Pump Control Topics
### Basic Pump Control
**Topic**: `plant_watering/pump/[1-2]/set`
**Direction**: Subscribe
**Payload**:
- `on` - Start pump at default speed (80%)
- `off` - Stop pump
- `pulse` - Run pump for 5 seconds
**Example**:
```bash
# Turn pump 1 on
mosquitto_pub -h <broker> -t "plant_watering/pump/1/set" -m "on"
# Turn pump 1 off
mosquitto_pub -h <broker> -t "plant_watering/pump/1/set" -m "off"
# Pulse pump 2
mosquitto_pub -h <broker> -t "plant_watering/pump/2/set" -m "pulse"
```
### Pump Speed Control
**Topic**: `plant_watering/pump/[1-2]/speed`
**Direction**: Subscribe
**Payload**: `0-100` (percentage)
**Note**: Only works when pump is running
**Example**:
```bash
# Set pump 1 to 50% speed
mosquitto_pub -h <broker> -t "plant_watering/pump/1/speed" -m "50"
```
### Pump State
**Topic**: `plant_watering/pump/[1-2]/state`
**Direction**: Publish
**Payload**: `on` or `off`
**Retained**: Yes
### Pump Statistics
**Topic**: `plant_watering/pump/[1-2]/stats`
**Direction**: Publish
**Payload**: JSON
```json
{
"total_runtime": 123456,
"run_count": 42,
"last_duration": 5000
}
```
### Pump Runtime (When Running)
**Topic**: `plant_watering/pump/[1-2]/runtime`
**Direction**: Publish
**Payload**: Current runtime in milliseconds
**Note**: Published every minute while pump is running
### Pump Cooldown Status
**Topic**: `plant_watering/pump/[1-2]/cooldown`
**Direction**: Publish
**Payload**: `true`
**Note**: Published when pump is in cooldown period
## Moisture Sensor Topics
### Moisture Readings
**Topic**: `plant_watering/moisture/[1-2]`
**Direction**: Publish
**Payload**: `0-100` (percentage)
**Frequency**: Every 10 seconds
**Note**: Currently simulated values
## Scheduling Topics
### Schedule Configuration
**Topic**: `plant_watering/schedule/[1-2]/[0-3]/config`
**Direction**: Subscribe
**Payload**: JSON configuration
**Schedule Types**:
1. **Interval Schedule**:
```json
{
"type": "interval",
"enabled": true,
"interval_minutes": 120,
"duration_ms": 15000,
"speed_percent": 70
}
```
2. **Time of Day Schedule**:
```json
{
"type": "time_of_day",
"enabled": true,
"hour": 6,
"minute": 30,
"duration_ms": 20000,
"speed_percent": 80
}
```
3. **Days and Time Schedule**:
```json
{
"type": "days_time",
"enabled": true,
"hour": 18,
"minute": 0,
"days_mask": 127,
"duration_ms": 25000,
"speed_percent": 75
}
```
4. **Disable Schedule**:
```json
{
"type": "disabled"
}
```
### Schedule Status
**Topic**: `plant_watering/schedule/[1-2]/[0-3]/status`
**Direction**: Publish
**Frequency**: Every minute (if enabled)
**Retained**: Yes
**Payload**: JSON with full schedule details including next run time
### Schedule Execution
**Topic**: `plant_watering/schedule/[1-2]/executed`
**Direction**: Publish
**Payload**: JSON
```json
{
"schedule_id": 0,
"duration_ms": 20000,
"speed": 80
}
```
### Global Schedule Status
**Topic**: `plant_watering/schedule/status`
**Direction**: Publish
**Frequency**: Every minute
**Retained**: Yes
**Payload**: JSON
```json
{
"holiday_mode": false,
"time_sync": true,
"active_schedules": 3,
"time": 1706436000
}
```
### Manual Schedule Trigger
**Topic**: `plant_watering/schedule/[1-2]/trigger`
**Direction**: Subscribe
**Payload**: Any value
**Action**: Triggers all enabled schedules for the specified pump
### Get Schedules
**Topic**: `plant_watering/schedule/[1-2]/get`
**Direction**: Subscribe
**Payload**: Any value
**Response**: Publishes all schedules for pump to `current` topics
### Manual Time Setting
**Topic**: `plant_watering/schedule/time/set`
**Direction**: Subscribe
**Payload**: Unix timestamp as string
**Example**: `"1706436000"`
## System Topics
### System Status
**Topic**: `plant_watering/status`
**Direction**: Publish
**Payload**: `online` or `offline`
**Retained**: Yes
**Note**: Uses MQTT Last Will and Testament
### Current Time (Periodic)
**Topic**: `plant_watering/system/current_time`
**Direction**: Publish
**Frequency**: Every minute
**Payload**: JSON
```json
{
"timestamp": 1706436000,
"datetime": "2024-01-28 14:32:00 MST",
"day_of_week": 0,
"hour": 14,
"minute": 32
}
```
### Time Response (On Demand)
**Topic**: `plant_watering/system/time`
**Direction**: Publish
**Trigger**: Send any message to `plant_watering/commands/get_time`
**Payload**: JSON
```json
{
"timestamp": 1706436000,
"datetime": "2024-01-28 14:32:45 MST",
"timezone": "MST7MDT,M3.2.0,M11.1.0",
"synced": true
}
```
## Command Topics
### Emergency Stop
**Topic**: `plant_watering/commands/emergency_stop`
**Direction**: Subscribe
**Payload**: Any value
**Action**: Immediately stops all pumps
### Test Mode
**Topic**: `plant_watering/commands/test_mode`
**Direction**: Subscribe
**Payload**: `on` or `off`
**Action**: Enables/disables test mode with shorter intervals
Test mode settings:
- Max runtime: 30 seconds
- Min interval: 5 seconds
### Holiday Mode
**Topic**: `plant_watering/commands/holiday_mode`
**Direction**: Subscribe
**Payload**: `on` or `off`
**Action**: Pauses all schedules without deleting them
### Get Time
**Topic**: `plant_watering/commands/get_time`
**Direction**: Subscribe
**Payload**: Any value
**Response**: Publishes to `plant_watering/system/time`
### Get All Schedules
**Topic**: `plant_watering/commands/get_schedules`
**Direction**: Subscribe
**Payload**: Any value
**Response**: Publishes all schedules and summary
### Test Pump
**Topic**: `plant_watering/commands/test_pump/[1-2]`
**Direction**: Subscribe
**Payload**: Duration in milliseconds (max 30000)
**Example**: `"15000"` for 15 seconds
## Settings Topics
### Pump Settings
**Base Topic**: `plant_watering/settings/pump/[1-2]/`
#### Maximum Runtime
**Topic**: `plant_watering/settings/pump/[1-2]/max_runtime`
**Payload**: Milliseconds (e.g., `"60000"` for 60 seconds)
#### Minimum Interval
**Topic**: `plant_watering/settings/pump/[1-2]/min_interval`
**Payload**: Milliseconds (e.g., `"300000"` for 5 minutes)
#### Speed Limits
**Topic**: `plant_watering/settings/pump/[1-2]/min_speed`
**Topic**: `plant_watering/settings/pump/[1-2]/max_speed`
**Payload**: Percentage 0-100
## Alert Topics
### Pump Errors
**Topic**: `plant_watering/alerts/pump_error/[1-2]`
**Direction**: Publish
**Payload**: Error description string
**Examples**:
- `"Maximum runtime exceeded"`
- `"Cooldown period not elapsed"`
### Schedule Errors
**Topic**: `plant_watering/alerts/schedule_error/[1-2]`
**Direction**: Publish
**Payload**: Error description
**Example**: `"Schedule 0 failed: ESP_ERR_INVALID_STATE"`
## Monitoring Examples
### Subscribe to All Topics
```bash
# Monitor everything
mosquitto_sub -h <broker> -t "plant_watering/#" -v
# Monitor pump activity only
mosquitto_sub -h <broker> -t "plant_watering/pump/#" -v
# Monitor schedules only
mosquitto_sub -h <broker> -t "plant_watering/schedule/#" -v
# Monitor alerts only
mosquitto_sub -h <broker> -t "plant_watering/alerts/#" -v
```
### Common Operations
#### Daily Morning Watering
```bash
mosquitto_pub -h <broker> -t "plant_watering/schedule/1/0/config" -m '{
"type": "time_of_day",
"enabled": true,
"hour": 6,
"minute": 0,
"duration_ms": 20000,
"speed_percent": 80
}'
```
#### Check System Status
```bash
# Get current time
mosquitto_pub -h <broker> -t "plant_watering/commands/get_time" -m "1"
# Get all schedules
mosquitto_pub -h <broker> -t "plant_watering/commands/get_schedules" -m "1"
```
#### Manual Watering
```bash
# Run pump 1 for 15 seconds
mosquitto_pub -h <broker> -t "plant_watering/commands/test_pump/1" -m "15000"
```
#### Going on Vacation
```bash
# Enable holiday mode
mosquitto_pub -h <broker> -t "plant_watering/commands/holiday_mode" -m "on"
# When back, disable holiday mode
mosquitto_pub -h <broker> -t "plant_watering/commands/holiday_mode" -m "off"
```
## Notes
- All timestamps are Unix timestamps (seconds since epoch)
- All durations are in milliseconds
- Speed values are percentages (0-100)
- Day of week: 0=Sunday, 1=Monday, ..., 6=Saturday
- Days mask is a bitmask where bit 0=Sunday
- Retained messages persist across broker restarts
- JSON payloads use unquoted booleans (`true`/`false`)

View File

@ -1,2 +1,8 @@
docker run -it --rm --network mqtt-broker_mqtt-network eclipse-mosquitto:2.0.22 mosquitto_pub -h mosquitto -u home-server -P '123QWeaSDZXC!@#' -t "home/plants/pump1/command" -m "OFF" -r docker run -it --rm --network mqtt-broker_mqtt-network eclipse-mosquitto:2.0.22 mosquitto_pub -h mosquitto -u home-server -P '123QWeaSDZXC!@#' -t "home/plants/pump1/command" -m "OFF" -r
docker run -it --rm --network mqtt-broker_mqtt-network eclipse-mosquitto:2.0.22 mosquitto_sub -h mosquitto -u monitor -P ThisIsNotATest123monitor -t "home/plants/#" -v docker run -it --rm --network mqtt-broker_mqtt-network eclipse-mosquitto:2.0.22 mosquitto_sub -h mosquitto -u monitor -P ThisIsNotATest123monitor -t "home/plants/#" -v
docker run --user $(id -u):$(id -g) --rm -v $PWD:/project -w /project -it espressif/idf:latest idf.py build
docker run --privileged --rm -v $PWD:/project -w /project --device=/dev/ttyACM0 -it espressif/idf:latest idf.py monitor -p /dev/ttyACM0
(If ota does not work)
docker run --privileged --rm -v $PWD:/project -w /project --device=/dev/ttyACM0 -it espressif/idf:latest idf.py flash -p /dev/ttyACM0

306
project_plan_v2.md Normal file
View File

@ -0,0 +1,306 @@
# ESP32-S3 Plant Watering System - Project Plan v2.0
*Updated: January 2025*
## Project Overview
**Goal**: Automated plant watering system with remote monitoring/control
**Hardware**: ESP32-S3-MINI-1, TB6612FNG motor driver, 2 pumps, 2 soil moisture sensors
**Software**: ESP-IDF v6, MQTT communication, OTA updates, NTP time sync, Scheduling
**Infrastructure**: Docker-based Mosquitto MQTT broker, local network deployment
## Project Status Summary
- **Phase 1**: Core Infrastructure ✅ COMPLETED
- **Phase 2**: Hardware Integration 🚧 75% COMPLETE (Motors done, Sensors pending)
- **Phase 3**: Automation Logic ✅ COMPLETED (via Scheduler)
- **Phase 4**: Enhanced Features 🚧 50% COMPLETE
- **Phase 5**: Production Polish 📋 TODO
---
## Phase 1: Core Infrastructure ✅ COMPLETED
### 1.1 Development Environment ✅
- ESP-IDF v6 in Docker container
- Build system configured
- Project structure created
### 1.2 WiFi Manager ✅
- Auto-connect to configured network
- Credential storage in NVS
- Auto-reconnection on disconnect
- Event callbacks for state changes
### 1.3 OTA Server ✅
- HTTP server on port 80
- Web interface for firmware upload
- Progress tracking
- Version management
- Rollback capability
### 1.4 MQTT Client ✅
- Connection with authentication
- Auto-reconnect with exponential backoff
- Last Will and Testament
- NVS credential storage
- Comprehensive publish/subscribe implementation
- Topics implemented:
- `plant_watering/status`
- `plant_watering/moisture/[1-2]`
- `plant_watering/pump/[1-2]/set`
- `plant_watering/pump/[1-2]/state`
- `plant_watering/pump/[1-2]/speed`
- `plant_watering/schedule/*`
- `plant_watering/system/*`
- `plant_watering/commands/*`
---
## Phase 2: Hardware Integration 🚧 75% COMPLETE
### 2.1 Motor Control Module ✅ COMPLETED
**Files**: `motor_control.c/h`
- TB6612FNG driver implementation ✅
- PWM control for pump speed ✅
- Direction control ✅
- Safety features:
- Maximum runtime limit ✅
- Minimum interval between runs ✅
- Soft start (500ms ramp) ✅
- Emergency stop ✅
- Manual override via MQTT ✅
- State tracking and reporting ✅
- Runtime statistics with NVS persistence ✅
- Tested and working with hardware ✅
### 2.2 Moisture Sensor Module ⏸️ ON HOLD
**File**: `moisture_sensor.c/h`
- [ ] ADC configuration for 2 sensors
- [ ] Calibration system
- [ ] Reading stabilization
- [ ] Percentage conversion
- [ ] Sensor fault detection
**Note**: Awaiting hardware delivery
### 2.3 Hardware Integration Testing 🚧 PARTIAL
- [x] Pump operation verified
- [x] PWM speed control tested
- [x] Safety features validated
- [ ] Moisture sensor integration (pending hardware)
- [ ] Full system integration test
---
## Phase 3: Automation Logic ✅ COMPLETED (via Scheduler)
### 3.1 Time-Based Scheduling ✅ COMPLETED
**Files**: `scheduler.c/h`
- Multiple schedule types:
- Interval-based (every X minutes) ✅
- Time of day (daily at specific time) ✅
- Day-specific (specific days at time) ✅
- Per-pump scheduling (4 schedules each) ✅
- NVS persistence ✅
- MQTT configuration ✅
- Holiday mode ✅
- Manual trigger for testing ✅
### 3.2 Time Synchronization ✅ COMPLETED
- NTP client implementation ✅
- Multiple NTP servers ✅
- Manual time setting fallback ✅
- Timezone support (MST default) ✅
- Time status reporting ✅
### 3.3 Schedule Management ✅ COMPLETED
- Create/update schedules via MQTT ✅
- View all schedules on demand ✅
- Enable/disable without deletion ✅
- Execution notifications ✅
- Error reporting ✅
### 3.4 Moisture-Based Automation ⏸️ PENDING
- Awaiting sensor hardware
- Will integrate with scheduler when available
---
## Phase 4: Enhanced MQTT & Monitoring 🚧 50% COMPLETE
### 4.1 Expanded MQTT Topics ✅ COMPLETED
Comprehensive topic structure implemented:
```
plant_watering/
├── status/esp32/*
├── pump/[1-2]/*
├── schedule/*
├── system/*
├── settings/*
├── alerts/*
└── commands/*
```
### 4.2 JSON Payloads ✅ COMPLETED
- Structured data for schedules ✅
- Status messages ✅
- Configuration updates ✅
- Built-in JSON parsing (no external deps) ✅
### 4.3 Alert System 🚧 PARTIAL
- [x] Pump malfunction alerts
- [x] Schedule execution errors
- [ ] Low moisture alerts (pending sensors)
- [ ] Water tank monitoring (future)
### 4.4 Remote Configuration ✅ COMPLETED
- MQTT-based settings updates ✅
- Validation and bounds checking ✅
- Persistent storage in NVS ✅
- Runtime parameter changes ✅
---
## Phase 5: Production Features 📋 TODO
### 5.1 Web Dashboard 📋 TODO
- [ ] Real-time status display
- [ ] Historical graphs
- [ ] Manual control interface
- [ ] Schedule configuration UI
- [ ] Mobile-responsive design
### 5.2 Home Assistant Integration 📋 TODO
- [ ] MQTT Discovery implementation
- [ ] Device registry
- [ ] Entity configuration
- [ ] Automation examples
### 5.3 Advanced Features 📋 TODO
- [ ] Multi-zone support (>2 zones)
- [ ] Flow sensor integration
- [ ] Weather API integration
- [ ] Predictive watering
- [ ] Water usage tracking
### 5.4 Production Hardening 🚧 PARTIAL
- [x] Watchdog timer (system level)
- [ ] Enhanced error recovery
- [ ] Factory reset mechanism
- [ ] Diagnostic mode
- [x] Memory monitoring
---
## Current Capabilities
### Working Features
1. **Remote Control**: Full MQTT control of pumps
2. **Scheduling**: Time-based watering with multiple schedule types
3. **Safety**: Runtime limits, cooldown periods, emergency stop
4. **Monitoring**: Real-time status, statistics, time sync status
5. **OTA Updates**: Web-based firmware updates
6. **Persistence**: Settings and schedules survive reboots
7. **Time Management**: NTP sync with manual fallback
### Tested and Verified
- Motor control with PWM speed adjustment
- Schedule execution (time_of_day and days_time modes)
- MQTT command processing
- Safety features (max runtime, min interval)
- OTA firmware updates
- NVS persistence
- Time synchronization
---
## Hardware Connections (Reference)
### ESP32-S3 to TB6612FNG ✅ VERIFIED
```
ESP32-S3 TB6612FNG Status
GPIO4 -> AIN1 ✅ Working
GPIO5 -> AIN2 ✅ Working
GPIO6 -> BIN1 ✅ Working
GPIO7 -> BIN2 ✅ Working
GPIO8 -> PWMA ✅ Working
GPIO9 -> PWMB ✅ Working
GPIO10 -> STBY ✅ Working
GND -> GND ✅ Connected
3.3V -> VCC ✅ Connected
```
### Moisture Sensors ⏸️ PENDING
```
Sensor 1 -> GPIO1 (ADC1_CHANNEL_0) - Awaiting hardware
Sensor 2 -> GPIO2 (ADC1_CHANNEL_1) - Awaiting hardware
```
---
## Next Steps
### Immediate (This Week)
1.~~Test interval scheduling mode~~
2. ⏸️ Install moisture sensors when they arrive
3.~~Document all MQTT topics comprehensively~~
4. ⏸️ Create basic web dashboard mockup
### Short Term (Next Month)
1. Implement moisture sensor module
2. Integrate moisture readings with scheduler
3. Add moisture-based automation rules
4. Implement Home Assistant discovery
### Long Term
1. Multi-zone expansion (>2 pumps)
2. Weather API integration
3. Water usage analytics
4. Mobile app development
---
## Success Metrics
- ✅ Reliable WiFi/MQTT connectivity (achieved)
- ✅ Accurate time-based scheduling (achieved)
- ✅ Precise watering control (achieved)
- ✅ OTA updates without service interruption (achieved)
- ✅ Response time < 1 second for commands (achieved)
- Accurate moisture readings 5%) - pending hardware
- 30-day uptime without intervention - in progress
- < 100mA average power consumption - to be measured
---
## Known Issues
1. None currently - system stable
## Risk Mitigation
- Implemented watchdog timers
- Added redundant safety checks
- Local schedule storage (survives network loss)
- Comprehensive error logging
- Sensor corrosion prevention (pending hardware)
---
## Version History
- **v2.2.0-scheduler**: Full scheduling system with NTP
- **v2.1.0-motor**: Motor control implementation
- **v2.0.0-mqtt**: MQTT integration
- **v1.0.1**: OTA-enabled base
- **v1.0.0**: Initial template
---
## Documentation Status
- README.md - Comprehensive project overview
- MOTOR_CONTROL_README.md - Motor control details
- SCHEDULER_README.md - Scheduling system guide
- MQTT_TOPICS.md - To be created
- API_REFERENCE.md - To be created
---
## Notes
- Scheduler tested with days_time mode - working correctly
- Time sync typically completes within 30 seconds of boot
- System handles DST transitions automatically
- All safety features have been validated with hardware