This IoT-based agricultural monitoring system uses sensors to track temperature, humidity, soil moisture, rainfall, and gas levels. The data is sent to the ThingSpeak cloud platform for analysis and displayed on an LCD. Artificial Neural Networks (ANN) are utilized for data insights and AI-driven analysis.
This project implements an IoT-based agricultural monitoring system that utilizes various sensors and Artificial Neural Networks (ANN) to monitor environmental conditions and provide valuable insights for farmers. The system collects data on temperature, humidity, soil moisture, rainfall, and gas levels, transmitting this data to the ThingSpeak cloud platform. The data is displayed on an LCD and sent to the ThingSpeak server for artificial intelligence and data analysis. The results are displayed through the ThingSpeak platform.
- ESP32 Development Board: The microcontroller responsible for collecting and transmitting sensor data.
- DHT11 Sensor: For measuring temperature and humidity.
- MQ-2 Gas Sensor: For detecting combustible gases (e.g., smoke).
- Rain Sensor: To detect rainfall.
- Soil Moisture Sensor: Measures the moisture level in the soil.
- PIR Motion Sensor: Detects movement.
- LCD (16x2) with I2C Interface: For local display of sensor readings.
- Connecting Wires: To connect the sensors to the ESP32.
- Arduino IDE: For programming the ESP32 microcontroller.
- ThingSpeak Account: For cloud-based data storage, visualization, and analysis. (Sign up here).
- Arduino Libraries:
DHT sensor libraryLiquidCrystal_I2CWiFiHTTPClientWireSPINeuralNetwork
Download and install the Arduino IDE from the official website:
Arduino IDE Download
- Open Arduino IDE and go to File > Preferences.
- Add the following URL to "Additional Boards Manager URLs":
https://dl.espressif.com/dl/package_esp32_index.json - Go to Tools > Board > Boards Manager.
- Search for "ESP32" and install the ESP32 by Espressif Systems board.
- Select your ESP32 board (e.g., "ESP32 Dev Module") from Tools > Board.
- Go to Sketch > Include Library > Manage Libraries.
- Search for and install the following libraries:
DHT sensor library by AdafruitLiquidCrystal_I2C by Frank de BrabanderNeuralNetwork by BrainyThing
- Sign up for a free account at ThingSpeak.
- Create a new channel to store the sensor data.
- Configure the channel fields according to the data you want to send (e.g., Temperature, Humidity, Soil Moisture, Gas Level).
- Note down the API Key for your ThingSpeak channel.
- DHT11: Data pin to ESP32 pin 23
- MQ-2:
- Analog output (AOUT) to ESP32 pin 39
- Digital output (DOUT) to ESP32 pin 27
- Rain Sensor: Digital output to ESP32 pin 35
- Soil Moisture Sensor: Analog output to ESP32 pin 36
- PIR Sensor: Digital output to ESP32 pin 12
- LCD:
- SDA to ESP32 SDA pin (usually 21)
- SCL to ESP32 SCL pin (usually 22)
Connect VCC and GND pins of all the sensors to the appropriate 5V and GND pins on the ESP32.
- Open the Arduino sketch (
.inofile) in the Arduino IDE. - WiFi Configuration:
- Replace
xxxxwith your WiFi network SSID (name). - Replace
xxxxxwith your WiFi password. Make sure they are in lowercase.
- Replace
- ThingSpeak API Key:
- Replace
xxxxxxxxxxxxxxinString serverName = "https://api.thingspeak.com/update?api_key=xxxxxxxxxxxxxx";with your ThingSpeak API key. - Replace
xxxxxxxxxxxxxxinString serverNameAI = "https://api.thingspeak.com/update?api_key=xxxxxxxxxxxxxx";with your ThingSpeak API key.
- Replace
- Connect the ESP32 to your computer via USB.
- In the Arduino IDE, select the correct port under Tools > Port.
- Click the Upload button to compile and upload the code to the ESP32.
- After uploading the code, open the Serial Monitor in the Arduino IDE (Tools > Serial Monitor).
- Set the baud rate to 9600.
- You should see the ESP32 connecting to your WiFi network and sensor readings being printed.
- Log in to your ThingSpeak account and go to the channel you created.
- You should see the sensor data being updated in real-time on the channel's charts.
- The LCD should display the temperature, humidity, and other sensor readings.
The code includes necessary libraries for DHT11 sensor, LCD, WiFi, HTTP communication, and more.
- Pin assignments for sensors, WiFi credentials, and ThingSpeak API key.
- Initializes serial communication, sensor pins, LCD, and WiFi connection.
- Connects to the specified WiFi network.
- Initializes the DHT sensor and LCD.
- Reads sensor values (temperature, humidity, gas level, rain, soil moisture).
- Displays the sensor readings on the LCD.
- Sends the sensor data to the ThingSpeak channel using HTTP GET requests.
- Delays for a specified interval before the next reading.
- Constructs the ThingSpeak API URL with sensor data.
- Sends an HTTP GET request to ThingSpeak to update the channel with the new data.
The neural network is designed with three layers:
- An input layer with 2 neurons
- A hidden layer with 4 neurons
- An output layer with 1 neuron
The NeuralNetwork class handles the feedforward and backpropagation processes. The network is trained using sample inputs (inputs) and expected outputs (expectedOutput) to minimize the mean squared error (MSE). After training, the network can predict outputs based on new inputs.
- More Sensors: Add more sensors to monitor additional environmental parameters (e.g., wind speed, light intensity).
- Data Analysis: Implement more advanced data analysis techniques to generate insights and predictions.
- Mobile App: Develop a mobile app for farmers to easily access sensor data and receive alerts.
- Alert System: Implement an alert system that sends SMS or email notifications to farmers when certain thresholds are reached (e.g., low soil moisture, high gas level).
- Solar Power: Integrate a solar panel and battery to make the system self-sufficient.
- Double-check the WiFi SSID and password in the code.
- Ensure your ESP32 board is compatible with your WiFi network (2.4GHz).
- Verify your ThingSpeak API key in the code.
- Check your ThingSpeak channel settings (field names, data format).
- Make sure your internet connection is stable.
- Check the sensor connections to the ESP32.
- Make sure you have installed the correct sensor libraries.
- Verify the sensor readings with a known reliable source.
- Ensure you have connected the LCD to ESP32 properly.
- Check the I2C address on the serial monitor (usually
0x27or0x39) and declare it accordingly.
Feel free to contribute to this project by submitting bug reports, feature requests, or pull requests.
This project is open-source and available under the MIT license.