Mushroom Automation with Smart Hydro-environment using IoT and AI for Sustainable Harvest
A Raspberry Pi 3 Model B-based IoT controller for automated mushroom cultivation chamber management.
This project implements the IoT device component of the M.A.S.H. thesis project, providing:
- WiFi Provisioning via Hotspot - Easy setup without keyboard/monitor
- Real-time sensor monitoring - CO₂, Temperature, Humidity via Arduino/SCD41
- Automated actuator control - Fans, Humidifier, LED Lights via GPIO relays
- Rule-based automation - Mathematical threshold control (no AI/ML training needed)
- Local REST API - Direct control from mobile app on same network
- Cloud connectivity - Remote control via backend when on internet
- Offline-first data storage - SQLite with backend synchronization
- Auto-boot service - Runs automatically on power-up
- mDNS discovery - Device findable on local network
Note: The automation system uses rule-based mathematical control, not AI or machine learning. No training datasets or model training required - the system works immediately with scientifically-derived thresholds. See RULE_BASED_AUTOMATION_GUIDE.md for details.
- SETUP_INSTRUCTIONS.md - Step-by-step setup guide
- QUICKSTART.md - Fast 5-minute deployment for presentations
- DEPLOYMENT_GUIDE.md - Complete technical documentation
- IMPLEMENTATION_SUMMARY.md - What was built and how
- SCHEMA_USAGE.md - Backend database integration details
- RULE_BASED_AUTOMATION_GUIDE.md - Automated control system guide
- CONTROL_SYSTEM_OVERVIEW.md - Quick reference for presentations
- MIGRATION_TO_RULE_BASED.md - AI to rule-based migration notes
/home/mash/MASH-IoT-Device/
├── main.py # Main application entry point
├── install.sh # One-command installation script
├── requirements.txt # Python dependencies
├── .env # Configuration (create from .env.example)
├── config/
│ ├── device_config.yaml # Default configuration
│ └── mash-device.service # Systemd service for auto-boot
├── src/
│ ├── actuators/ # GPIO actuator control
│ ├── api/ # REST API server (Flask)
│ ├── sensors/ # Sensor data collection
│ ├── storage/ # SQLite local database
│ ├── utils/ # Network, hotspot, config
│ └── backend_client.py # Cloud API communication
├── raspberry-pi/
│ └── actuator_controller.py # Standalone actuator script
├── arduino-uno/ # Arduino sensor code
├── logs/ # Auto-generated logs
├── data/ # Local database files
└── documents/ # Schema and documentation
- Raspberry Pi 3 Model B (or Zero 2W)
- SCD41 CO₂, Temperature, and Humidity Sensor
- I2C LCD Display (20x4 characters)
- Actuators (Fans, Humidifiers, etc.)
- Power supply and wiring components
- Python 3.9+
- Raspberry Pi OS (Lite or Desktop)
- Required Python packages (see requirements.txt)
# 1. SSH into your Raspberry Pi
ssh mash@MASH-CHAMBER
# 2. Navigate to project directory
cd ~/MASH-IoT-Device
# 3. Run installation script
chmod +x install.sh
./install.sh
# 4. Create environment file
cp .env.example .env
nano .env
# 5. Reboot to start service
sudo rebootThe device will automatically:
- Start the MASH service
- Check for WiFi connection
- Create hotspot
MASH-Chamber-XXXXXXif not connected - Wait for mobile app to configure WiFi
# Start service
sudo systemctl start mash-device
# Check status
sudo systemctl status mash-device
# View logs
sudo journalctl -u mash-device -f
# Test in mock mode (no hardware needed)
python3 main.py --mockWhen hotspot is active:
- Provisioning Info:
http://192.168.4.1:5000/api/v1/provisioning/info - WiFi Scan:
http://192.168.4.1:5000/api/v1/wifi/scan - WiFi Config:
POST http://192.168.4.1:5000/api/v1/wifi/config - Device Status:
http://192.168.4.1:5000/api/v1/status - Actuator Control:
POST http://192.168.4.1:5000/api/v1/commands/actuator_control
Key configuration variables:
# Device Identity
DEVICE_ID=mash_device_001
DEVICE_NAME="MASH Chamber #1"
# Backend API
BACKEND_API_URL=https://mash-backend.onrender.com/api
BACKEND_API_KEY=your_api_key
# MQTT Configuration
MQTT_BROKER_URL=mqtt://your_broker:1883
MQTT_USERNAME=your_username
MQTT_PASSWORD=your_password
# Sensor Settings
SENSOR_READ_INTERVAL=60
MOCK_MODE=false# Temperature Control
TEMP_MIN=25.0
TEMP_MAX=28.0
TEMP_CRITICAL_HIGH=32.0
# Humidity Control
HUMIDITY_MIN=80.0
HUMIDITY_MAX=90.0
# CO₂ Management
CO2_OPTIMAL_MIN=10000
CO2_OPTIMAL_MAX=15000
CO2_CRITICAL_HIGH=20000# Start device
python main.py
# Run in mock mode (for testing)
python main.py --mock
# Enable debug logging
python main.py --debug
# Check device status
python main.py --status# Run with mock sensors and debug output
python main.py --mock --debug
# Test sensor reading
python -c "
from src.sensors import SensorManager
sm = SensorManager(mock_mode=True)
sm.start()
time.sleep(5)
print(sm.get_latest_reading())
sm.stop()
"MASH-IoT-Device/
├── src/
│ ├── sensors/ # Sensor drivers and interfaces
│ │ ├── __init__.py
│ │ ├── scd41_sensor.py
│ │ ├── sensor_reading.py
│ │ └── sensor_manager.py
│ ├── actuators/ # Actuator control (mocked initially)
│ ├── display/ # LCD display (mocked initially)
│ ├── mqtt/ # MQTT client and communication
│ ├── storage/ # SQLite database operations
│ │ ├── __init__.py
│ │ ├── database_manager.py
│ │ ├── schema.py
│ │ └── sync_manager.py
│ └── utils/ # Helper functions and configs
│ ├── __init__.py
│ ├── config.py
│ └── logger.py
├── tests/ # Unit and integration tests
├── config/ # Configuration files
├── data/ # Local data storage (SQLite)
├── logs/ # Application logs
├── main.py # Main application entry point
├── requirements.txt # Python dependencies
├── env.example # Environment variables template
└── README.md # This file
The local SQLite database is aligned with the backend PostgreSQL schema:
- device_info - Device information and configuration
- sensor_readings - Sensor data with sync status
- device_commands - Commands from backend or local
- alerts - Generated alerts and notifications
- actuator_states - Current actuator states
- sync_queue - Data pending upload to backend
- system_logs - Application logs
- config_cache - Configuration values
- Device Registration:
POST /api/devices - Sensor Data Sync:
POST /api/sensors/data - Alert Sync:
POST /api/alerts - Command Retrieval:
GET /api/devices/:id/commands
- Sensor Data:
mash/device/{device_id}/sensors - Device Status:
mash/device/{device_id}/status - Commands:
mash/device/{device_id}/commands - Alerts:
mash/device/{device_id}/alerts
-
Windows Development:
- Install Python 3.9+
- Install VSCode with Python extensions
- Use mock mode for testing
-
Raspberry Pi Deployment:
- Enable SSH on RPi
- Deploy code via Git or SCP
- Test with real hardware
-
Remote Development:
# SSH to Raspberry Pi ssh pi@<rpi-ip> # Clone and run git clone <repository> cd MASH-IoT-Device python main.py
# Run all tests
python -m pytest tests/
# Run specific test
python -m pytest tests/test_sensors.py
# Run with coverage
python -m pytest --cov=src tests/# Test sensor reading
python -c "
from src.sensors import SensorManager
sm = SensorManager(mock_mode=True)
sm.start()
time.sleep(10)
print('Latest reading:', sm.get_latest_reading())
sm.stop()
"
# Test database operations
python -c "
from src.storage import DatabaseManager
db = DatabaseManager('./test.db')
print('Database stats:', db.get_database_stats())
"-
Sensor Not Detected:
- Check I2C is enabled:
sudo raspi-config - Verify wiring connections
- Use mock mode for testing
- Check I2C is enabled:
-
Database Errors:
- Check file permissions
- Ensure data directory exists
- Verify SQLite installation
-
MQTT Connection Failed:
- Check network connectivity
- Verify broker credentials
- Test with local broker first
# Enable debug logging
python main.py --debug
# Check logs
tail -f logs/mash_device.log
# Monitor system resources
htopSCD41 → Raspberry Pi
VCC → 3.3V (Pin 1)
GND → GND (Pin 6)
SCL → GPIO 3 (Pin 5)
SDA → GPIO 2 (Pin 3)
LCD → Raspberry Pi
VCC → 5V (Pin 2)
GND → GND (Pin 6)
SCL → GPIO 3 (Pin 5)
SDA → GPIO 2 (Pin 3)
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is part of the M.A.S.H. thesis research project.
For issues and questions:
- Check the troubleshooting section
- Review the logs in
logs/mash_device.log - Create an issue in the repository
Version: 1.0
Created: October 2025
Target Completion: October 31, 2025