An intelligent IoT system that uses machine learning to automatically control greenhouse ventilation based on real-time temperature and humidity data
This project implements a complete AI-powered greenhouse fan control system with three main components:
- π§ AI Model - TensorFlow Lite model trained on 17,521 sensor samples
- π§ ESP32 Controller - Hardware simulation with real sensor integration
- π± Flutter Mobile App - Real-time monitoring and AI-powered control
- Intelligent Fan Control: AI decides when to turn fan ON/OFF based on environmental conditions
- Real-time Monitoring: Live temperature and humidity tracking
- Dual Control Modes: AI automatic mode or manual override
- Cloud Integration: Firebase real-time database for seamless communication
- Hardware Simulation: Complete Wokwi simulation for testing without physical hardware
- Production Ready: Robust error handling and offline capabilities
π‘οΈ Temperature: 25.8Β°C | π§ Humidity: 78.5%
π§ AI Prediction: 0.387 (38.7% confidence) β π Fan: OFF
π‘οΈ Temperature: 32.1Β°C | π§ Humidity: 85.2%
π§ AI Prediction: 0.782 (78.2% confidence) β π Fan: ON
π± Flutter App π₯ Firebase π§ ESP32 + Sensors
βββ AI Model βββ Real-time DB βββ DHT22 (Temp/Humidity)
βββ Dashboard UI βββ Authentication βββ Relay Module (Fan)
βββ Manual Controls βββ Data Sync βββ WiFi Connection
βββ Live Monitoring βοΈ Bidirectional βοΈ Sensor Data
-
Development Environment:
- Visual Studio Code with PlatformIO extension
- Flutter SDK (3.0+)
- Firebase CLI (optional)
- Git
-
Hardware (Optional - for physical deployment):
- ESP32 Development Board
- DHT22 Temperature/Humidity Sensor
- 5V Relay Module
- Jumper wires and breadboard
git clone https://github.com/KDsudheera/greenhouse-ai-fancontroller.git
cd greenhouse-ai-fancontroller-
Create Firebase Project:
- Go to Firebase Console
- Click "Create a project"
- Enable Realtime Database (start in test mode)
- Enable Authentication β Email/Password
-
Configure Firebase for Flutter:
cd flutter_app # Install Firebase CLI (if not installed) npm install -g firebase-tools # Login and configure firebase login flutter pub global activate flutterfire_cli flutterfire configure
-
Set up Database Rules:
{ "rules": { ".read": "auth != null", ".write": "auth != null" } }
-
Copy the example config:
cd esp32-controller cp include/config.h.example include/config.h -
Edit
include/config.hwith your credentials:// WiFi Configuration #define WIFI_SSID "your-wifi-name" #define WIFI_PASSWORD "your-wifi-password" // Firebase Configuration #define API_KEY "your-firebase-api-key" #define DATABASE_URL "https://your-project-default-rtdb.firebaseio.com/" #define USER_EMAIL "your-email@example.com" #define USER_PASSWORD "your-password"
-
Using Wokwi (Recommended for testing):
cd esp32-controller # Upload to Wokwi and run simulation # Open diagram.json in Wokwi.com # Click "Start Simulation"
-
Using Physical Hardware:
cd esp32-controller # Compile and upload pio run --target upload # Monitor serial output pio device monitor
-
Install dependencies:
cd flutter_app flutter pub get -
Run on Android device/emulator:
# Check connected devices flutter devices # Run the app flutter run
-
Enable Developer Options (for physical device):
- Settings β About Phone β Tap "Build Number" 7 times
- Settings β Developer Options β Enable "USB Debugging"
-
Verify ESP32 Connection:
- Check serial monitor for "Connected to Firebase"
- Verify sensor readings in console
-
Check Firebase Data:
- Open Firebase Console β Realtime Database
- Look for
/climate_dataupdates every 10 seconds
-
Test Flutter App:
- Open the app and verify real-time data display
- Toggle between AI mode and Manual mode
- Check AI predictions in the console
greenhouse-ai-fancontroller/
βββ README.md # This file
βββ esp32-controller/ # ESP32 microcontroller code
β βββ src/main.cpp # Main ESP32 application
β βββ include/
β β βββ config.h.example # Configuration template
β β βββ README # Include directory info
β βββ data/
β β βββ grenhouse_model.tflite # AI model for ESP32 (backup)
β βββ platformio.ini # PlatformIO configuration
β βββ diagram.json # Wokwi simulation diagram
β βββ wokwi.toml # Wokwi project settings
βββ flutter_app/ # Mobile application
β βββ lib/
β β βββ main.dart # Main Flutter app
β β βββ firebase_options.dart # Firebase configuration
β β βββ [models/screens/services/] # App architecture (future)
β βββ assets/
β β βββ grenhouse_model.tflite # AI model for mobile inference
β βββ android/ # Android-specific configuration
β βββ pubspec.yaml # Flutter dependencies
β βββ firebase.json # Firebase project settings
βββ docs/
β βββ greenhouse.ipynb # AI model training notebook
βββ assets/ # Additional project assets
- Dataset Size: 17,521 samples
- Features: Temperature (20.9Β°C - 90.5Β°C), Humidity (31.69% - 100%)
- Target: Binary classification (Fan ON/OFF)
- Training Method: Google Colab with TensorFlow
model = Sequential([
Dense(16, activation='relu', input_shape=(2,)),
Dense(8, activation='relu'),
Dense(1, activation='sigmoid')
])- Accuracy: 98% on test data
- Model Size: 2KB (optimized for mobile)
- Inference Time: <10ms on mobile device
- Real-world Validation: Excellent uncertainty handling
# Input normalization used in training
temperature_normalized = (temp - 20.9) / (90.5 - 20.9)
humidity_normalized = (humidity - 31.69) / (100.0 - 31.69)- ESP32 DevKit V1
- DHT22 Sensor β GPIO 4
- LED (Fan Indicator) β GPIO 5
- WiFi Connection β Simulated
ESP32 DevKit V1:
βββ DHT22 Sensor
β βββ VCC β 3.3V
β βββ GND β GND
β βββ DATA β GPIO 4
βββ Relay Module (Fan Control)
β βββ VCC β 5V
β βββ GND β GND
β βββ IN β GPIO 5
βββ Power Supply β USB/External 5V
{
"climate_data": {
"temperature_c": 25.8,
"humidity_percent": 78.5,
"timestamp": "1642678800",
"fan_status": false,
"last_updated": "2025-01-15T10:30:00Z"
},
"fan_on": true,
"current_fan_status": false,
"ai_predictions": {
"last_confidence": 0.387,
"last_decision": "OFF",
"prediction_count": 1247
}
}{
"rules": {
".read": "auth != null",
".write": "auth != null",
"climate_data": {
".validate": "newData.hasChildren(['temperature_c', 'humidity_percent'])"
}
}
}- Real-time Sensor Data: Live temperature and humidity display
- AI Prediction Status: Current confidence level and decision
- Fan Control: Manual override toggle
- Connection Status: ESP32 and Firebase connectivity indicators
- On-device Inference: TensorFlow Lite model runs locally
- Uncertainty Handling: Shows confidence levels (0.3-0.7 typical)
- Smart Decisions: Appropriate hesitation in borderline conditions
// Key UI Components
βββ Temperature Card β Real-time display with trend
βββ Humidity Card β Percentage with visual indicator
βββ AI Status Card β Prediction confidence and decision
βββ Fan Control Toggle β Manual override capability
βββ Connection Status β System health indicators-
ESP32 Won't Connect to WiFi:
// Check config.h credentials // Verify WiFi network is 2.4GHz (not 5GHz) // Check serial monitor for connection attempts
-
Firebase Authentication Fails:
# Verify credentials in config.h # Check Firebase Console β Authentication β Users # Ensure email/password auth is enabled
-
Flutter Build Errors:
flutter clean flutter pub get flutter run
-
AI Model Not Loading:
# Verify grenhouse_model.tflite exists in flutter_app/assets/ # Check pubspec.yaml has assets configuration # Confirm file path in main.dart
-
ESP32 Memory Issues:
- Monitor serial output for heap usage
- Reduce Firebase update frequency if needed
- Enable watchdog timer for stability
-
Flutter Performance:
- Use release build for production:
flutter run --release - Monitor AI inference time in debug logs
- Optimize Firebase listeners
- Use release build for production:
- Response Time: < 1 second from sensor to AI decision
- Update Frequency: 10 seconds (configurable)
- Memory Usage:
- ESP32: 84% Flash, 14.8% RAM
- Flutter: ~50MB with TensorFlow Lite
- Power Consumption: Optimized with smart fan control
- Training Accuracy: 98%
- Real-world Confidence Range: 0.3-0.7 (realistic uncertainty)
- Inference Speed: <10ms on mobile
- Model Size: 2KB (efficient for IoT deployment)
- IoT Development: Complete embedded system with cloud integration
- Machine Learning: Real-world AI deployment on mobile devices
- Flutter Development: Professional mobile app with real-time features
- Firebase Integration: Modern backend-as-a-service implementation
- Greenhouse Automation: Actual agricultural use
- Home Automation: Smart ventilation control
- Research Projects: Environmental monitoring system
- Portfolio Projects: Demonstrates full-stack IoT capabilities
- Multi-sensor Support: Add soil moisture, light sensors
- Data Analytics: Historical trends and insights
- Push Notifications: Alert system for critical conditions
- Schedule Control: Time-based automation rules
- Energy Monitoring: Track power consumption
- Multiple Zones: Control multiple greenhouse sections
- Edge AI: Run AI model on ESP32 directly
- Over-the-Air Updates: Remote firmware updates
- Mesh Networking: Multiple ESP32 communication
- Voice Control: Integration with smart assistants
If you use this project in your research or commercial applications, please cite:
Greenhouse AI Fan Controller
https://github.com/KDsudheera/greenhouse-ai-fancontroller
An intelligent IoT system using machine learning for greenhouse automation
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- GitHub: @KDsudheera
- Email: kusaldoranegoda99@gmail.com
- Project Issues: GitHub Issues
This project is licensed under the MIT License - see the LICENSE file for details.
- TensorFlow Team for TensorFlow Lite mobile inference
- Firebase Team for real-time database services
- Flutter Team for cross-platform mobile development
- PlatformIO Team for embedded development platform
- Wokwi Team for excellent hardware simulation
If this project was useful for your learning or development:
- β Star this repository
- π Share with others interested in IoT + AI
- π Report issues to help improve the project
- π€ Contribute improvements or new features
Built with β€οΈ for the IoT and AI community
"Smart agriculture starts with intelligent automation" π±π€