A comprehensive web-based environmental monitoring and control system using Raspberry Pi with MongoDB-only architecture for reliable offline operation.
- Temperature & Humidity: DHT22 sensor for precise readings
- CO2 Levels: MQ-135 or similar sensor for air quality monitoring
- Light Intensity: Photoresistor for grow light management
- Real-time Dashboard: Live updates via WebSocket
- Fogger System: Automatic misting based on humidity levels
- Exhaust Fan: Air circulation control
- LED Grow Lights: Automated lighting schedule
- Manual Override: Web-based control interface
- MongoDB-Only Architecture: Local MongoDB with Atlas cloud sync
- Offline Operation: Continues working when internet is down
- Automatic Sync: Data syncs to Atlas when internet is restored
- Historical Data: Charts and tables for trend analysis
- Data Export: Easy access to sensor readings
- Real-time Monitoring: Live sensor data and control status
- Automated Controls: Smart environmental adjustments
- Manual Override: Web-based control interface
- Status Monitoring: Visual indicators for system health
- Raspberry Pi 4 (recommended) or Pi 3B+
- MicroSD card (32GB+)
- Power supply (5V 3A)
- DHT22: Temperature and humidity sensor
- MCP3008: 8-channel ADC for analog sensors
- Photoresistor: Light intensity measurement
- MQ-135: CO2/air quality sensor (or similar)
- Relay Module: For fogger, fan, and light control
- 12V Fogger/Mister: Ultrasonic or similar
- Exhaust Fan: 12V computer fan or similar
- LED Grow Lights: Full spectrum recommended
- Status LEDs: Visual system status indicators
GPIO_CONFIG = {
# Sensors
'DHT22_PIN': 4, # Temperature & Humidity
'LIGHT_SENSOR_PIN': 0, # ADC Channel 0
'CO2_SENSOR_PIN': 1, # ADC Channel 1
# Controls
'FOGGER_PIN': 18, # Fogger relay
'FAN_PIN': 19, # Fan control
'LED_LIGHTS_PIN': 21, # Grow lights
# Status LEDs
'STATUS_LED_GREEN': 26, # System OK
'STATUS_LED_RED': 16, # Error
'STATUS_LED_BLUE': 13, # WiFi Connected
}
# Update Raspberry Pi OS
sudo apt update && sudo apt upgrade -y
# Install Python dependencies
sudo apt install python3-pip python3-venv git -y
# Install system libraries for GPIO
sudo apt install python3-dev python3-gpiozero -y
git clone <your-repo-url>
cd environmental-control-system
# Run the MongoDB setup script
./setup_mongodb.sh
This will install and configure MongoDB locally on your Raspberry Pi.
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install Python packages
pip install -r requirements.txt
# Create environment file
cp .env.example .env
# Edit configuration
nano .env
Update .env
with your settings:
SECRET_KEY
: Your secret keyDASHBOARD_PASSWORD
: Dashboard login passwordMONGODB_URI
: MongoDB Atlas connection string (optional)
Connect sensors and control devices according to the GPIO configuration in config.py
.
# Activate virtual environment
source venv/bin/activate
# Run the application
python app.py
The system will:
- Initialize GPIO pins and sensors
- Connect to local MongoDB database
- Attempt to connect to MongoDB Atlas (if configured)
- Start the web server on port 5000
- Automatically open browser to
http://localhost:5000
- Login: Default password is
admin123
(change in .env) - Dashboard: Real-time sensor readings and controls
- Database Status: Shows local MongoDB and Atlas connection status
- Controls: Manual override for fogger, fan, and lights
- Historical Data: Charts and tables of past readings
To access from other devices on your network:
# Find your Pi's IP address
hostname -I
# Access via browser at: http://YOUR_PI_IP:5000
The system automatically manages environmental conditions:
- Temperature: 18-24°C
- Humidity: 80-95%
- CO2: 800-1200 ppm
- Light: 200-800 lux
- Water Level: 20-100%
The system automatically manages:
- Fogger: Activates when humidity drops below optimal range
- Fan: Activates when humidity exceeds optimal range
- Duration: Configurable misting cycles
- Schedule: 6 AM - 6 PM daily
- Intensity: Full spectrum LED grow lights
- Automatic: Based on time schedule
- Monitoring: Continuous temperature tracking
- Alerts: Visual warnings for out-of-range conditions
- Future: Heating element control (configurable)
- Local storage on Raspberry Pi
- No internet required
- Fast local access
- Automatic data persistence
- Cloud-based backup and remote access
- Automatic sync when internet available
- Global accessibility
- Optional - system works offline
GET /api/current
- Current sensor readingsGET /api/latest
- Recent readings (last 10)GET /api/history
- Historical dataGET /api/status
- System status
POST /api/control/fogger
- Control foggerPOST /api/control/fan
- Control fan speedPOST /api/control/lights
- Control grow lights
# Check GPIO permissions
sudo usermod -a -G gpio $USER
# Restart after group change
sudo reboot
- Verify wiring connections
- Check sensor power (3.3V/5V)
- Review GPIO pin assignments
- Monitor system logs
- Verify MongoDB is running:
sudo systemctl status mongod
- Check local MongoDB connection:
mongo --eval "db.runCommand('ping')"
- Verify Atlas URI in .env (optional)
- Check internet connectivity for Atlas sync
- Monitor connection status in dashboard
- Ensure port 5000 is open
- Check firewall settings
- Verify Pi's IP address
- Try localhost:5000 directly on Pi
If GPIO libraries aren't available, the system runs in simulation mode with realistic sensor data.
Extend the SensorService
class in app.py
to add new sensors.
Add new control functions in GPIOControlService
class.
- Electrical Safety: Use proper relays for high-voltage devices
- Moisture Protection: Protect electronics from humidity
- Ventilation: Ensure adequate air circulation
- Fire Safety: Monitor heating elements and electrical connections
- Food Safety: Use food-grade materials for growing containers
MIT License - see LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
For issues, questions, or contributions:
- Create an issue on GitHub
- Check the troubleshooting section
- Review system logs for error details