Skip to content

Pisethz/dht11

Repository files navigation

IoT Agriculture Monitoring System

A modern web-based monitoring and control system for IoT agriculture sensors, built with Node.js, Express, and SQLite database. This system replaces PHP-based solutions with a more maintainable and scalable architecture.

Features

  • 🌑️ Real-time Sensor Monitoring: Temperature, humidity, and soil moisture tracking
  • πŸ’‘ Remote LED Control: Turn devices on/off from the web interface
  • πŸ“Š Data Visualization: Interactive charts showing sensor data history
  • πŸ”„ Real-time Updates: Live data updates using WebSocket connections
  • πŸ“± Responsive Design: Works on desktop, tablet, and mobile devices
  • πŸ—„οΈ Data Storage: SQLite database for reliable data persistence
  • πŸš€ Modern Architecture: Node.js backend with RESTful API

Hardware Requirements

  • ESP8266 (NodeMCU or similar)
  • DHT11 Temperature & Humidity Sensor
  • Soil Moisture Sensor (analog)
  • LED for control demonstration
  • Jumper wires and breadboard

Software Requirements

  • Node.js (v14 or higher)
  • npm (Node Package Manager)
  • Arduino IDE
  • ESP8266 board support package

Installation & Setup

1. Backend Setup

  1. Install Node.js dependencies:

    npm install
  2. Start the server:

    npm start

    For development with auto-restart:

    npm run dev
  3. Access the web interface: Open your browser and navigate to http://localhost:3000

2. Arduino Setup

  1. Install required libraries:

    • ESP8266WiFi
    • ESP8266HTTPClient
    • Arduino_JSON
    • DHT sensor library
  2. Update the Arduino code:

    • Open arduino_updated.ino in Arduino IDE
    • Update the WiFi credentials:
      const char* ssid = "YOUR_WIFI_NAME";
      const char* password = "YOUR_WIFI_PASSWORD";
    • Update the server IP address:
      const char* serverIP = "YOUR_SERVER_IP"; // e.g., "192.168.1.100"
  3. Upload the code to your ESP8266

3. Hardware Connections

ESP8266 Pin Component Notes
D5 DHT11 Data Temperature & Humidity Sensor
A0 Soil Moisture Sensor Analog input
D0 LED Control demonstration
3.3V DHT11 VCC Power supply
3.3V Soil Moisture VCC Power supply
GND Common Ground All components

API Endpoints

Device Communication (for Arduino)

  • POST /getdata - Get pending commands for the device
  • POST /updateDHT11data_and_recordtable - Send sensor data to server

Web Interface API

  • GET /api/devices - Get all registered devices
  • GET /api/sensor-data/:deviceId - Get latest sensor data
  • GET /api/sensor-data/:deviceId/history - Get sensor data history
  • POST /api/command - Send command to device

Web Interface Features

Dashboard

  • Real-time Sensor Display: Shows current temperature, humidity, and soil moisture
  • Status Indicators: Visual indicators for sensor health and device connection
  • LED Control: Remote control for connected devices

Data Visualization

  • Interactive Charts: Line charts showing sensor data over time
  • Historical Data: View up to 50 recent data points
  • Real-time Updates: Charts update automatically with new data

Device Management

  • Connection Status: Monitor device connectivity
  • Last Update Time: Track when data was last received
  • Sensor Status: Monitor sensor health and functionality

Configuration

Server Configuration

The server runs on port 3000 by default. To change this:

  1. Set the PORT environment variable:

    export PORT=8080
    npm start
  2. Or modify the PORT variable in server.js:

    const PORT = process.env.PORT || 8080;

Database

The system uses SQLite database (agriculture.db) which is created automatically. The database includes:

  • devices: Device information and status
  • sensor_data: Historical sensor readings
  • device_commands: Pending commands for devices

Troubleshooting

Common Issues

  1. ESP8266 not connecting to WiFi

    • Check WiFi credentials in Arduino code
    • Ensure WiFi network is accessible
    • Check signal strength
  2. Server not receiving data

    • Verify server IP address in Arduino code
    • Check if server is running on correct port
    • Ensure firewall allows connections
  3. Web interface not loading

    • Check if Node.js server is running
    • Verify port 3000 is accessible
    • Check browser console for errors
  4. Database errors

    • Ensure write permissions in project directory
    • Check if SQLite is properly installed
    • Verify database file is not corrupted

Debug Mode

Enable debug logging by setting the environment variable:

DEBUG=* npm start

Development

Project Structure

β”œβ”€β”€ server.js              # Main server file
β”œβ”€β”€ package.json           # Dependencies and scripts
β”œβ”€β”€ agriculture.db         # SQLite database (auto-created)
β”œβ”€β”€ public/                # Web interface files
β”‚   β”œβ”€β”€ index.html         # Main web page
β”‚   └── script.js          # Frontend JavaScript
β”œβ”€β”€ arduino_updated.ino    # Updated Arduino code
└── README.md              # This file

Adding New Features

  1. New Sensor Types: Add to database schema and API endpoints
  2. Additional Controls: Extend command system in Arduino code
  3. Custom Visualizations: Modify Chart.js configuration
  4. Mobile App: Use existing API endpoints for mobile development

Security Considerations

  • Change default device IDs
  • Implement authentication for production use
  • Use HTTPS in production environments
  • Secure database access
  • Validate all input data

Performance Optimization

  • Database indexing for large datasets
  • Connection pooling for multiple devices
  • Caching for frequently accessed data
  • Compression for API responses

License

MIT License - feel free to use and modify for your projects.

Support

For issues and questions:

  1. Check the troubleshooting section
  2. Review the code comments
  3. Check the browser console for errors
  4. Verify all connections and configurations

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

Happy Farming! 🌱

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors