IoT Project for Smart Agriculture: Setup and Example
An IoT project for smart agriculture uses
sensors to collect data like soil moisture and temperature, sends it via MQTT protocol to a Raspberry Pi or cloud server for processing, and triggers actions like irrigation automatically. This setup helps farmers monitor crops remotely and optimize water use efficiently.Syntax
This project uses the MQTT protocol to send sensor data. The main parts are:
- Sensor reading: Code to get data from soil moisture or temperature sensors.
- MQTT client setup: Connects to an MQTT broker to publish sensor data.
- Data publishing: Sends sensor values to a topic for subscribers to receive.
- Action trigger: Subscriber code listens to data and controls devices like water pumps.
python
import paho.mqtt.client as mqtt import time # MQTT broker details broker = 'test.mosquitto.org' port = 1883 topic = 'smart_agriculture/soil_moisture' # Simulated sensor reading function def read_soil_moisture(): # Replace with actual sensor code return 45 # example moisture percentage client = mqtt.Client() client.connect(broker, port) while True: moisture = read_soil_moisture() client.publish(topic, moisture) print(f'Published soil moisture: {moisture}%') time.sleep(10)
Example
This example shows a simple Python script that reads a simulated soil moisture value and publishes it to an MQTT broker every 10 seconds. A subscriber can listen to this topic to automate irrigation.
python
import paho.mqtt.client as mqtt import time import random broker = 'test.mosquitto.org' port = 1883 topic = 'smart_agriculture/soil_moisture' client = mqtt.Client() client.connect(broker, port) def read_soil_moisture(): # Simulate sensor data between 30% and 70% return random.randint(30, 70) while True: moisture = read_soil_moisture() client.publish(topic, moisture) print(f'Published soil moisture: {moisture}%') time.sleep(10)
Output
Published soil moisture: 52%
Published soil moisture: 38%
Published soil moisture: 65%
Published soil moisture: 47%
Common Pitfalls
Common mistakes include:
- Not connecting to the MQTT broker before publishing, causing errors.
- Using incorrect topic names, so subscribers don't receive data.
- Ignoring sensor calibration, leading to wrong readings.
- Not handling network failures, which stops data flow.
Always check connection status and use consistent topic naming.
python
import paho.mqtt.client as mqtt client = mqtt.Client() # Missing connect call causes publish failure # client.connect('test.mosquitto.org', 1883) try: client.publish('smart_agriculture/soil_moisture', 50) except Exception as e: print(f'Error publishing: {e}') # Correct way: client.connect('test.mosquitto.org', 1883) client.publish('smart_agriculture/soil_moisture', 50) print('Published after connecting')
Output
Error publishing: MQTT client is not connected
Published after connecting
Quick Reference
Tips for building IoT smart agriculture projects:
- Use MQTT for lightweight, real-time messaging.
- Choose sensors like soil moisture, temperature, and humidity.
- Use a Raspberry Pi or microcontroller as the MQTT client.
- Implement error handling for network issues.
- Automate irrigation based on sensor thresholds.
Key Takeaways
Use MQTT protocol to send sensor data efficiently in smart agriculture projects.
Always connect to the MQTT broker before publishing messages to avoid errors.
Simulate or calibrate sensors to ensure accurate data collection.
Automate irrigation by subscribing to sensor data and triggering devices.
Handle network failures gracefully to maintain continuous monitoring.