In the field of agriculture and smart gardening, accurate data about the soil's moisture content is pivotal for healthy plant growth. Utilizing Internet of Things (IoT) devices, we can create a soil moisture sensor that can send the data to a cloud platform for analysis and allow for real-time monitoring.
In this blog, we'll discuss how to build a simple IoT soil moisture sensor using Raspberry Pi, Python, and an online IoT platform called Thingspeak.
First, we connect our soil moisture sensor to the Raspberry Pi. The VCC pin goes to pin1 (3.3v) on your Pi, GND to pin6 (GND), and the sensor's DO pin to pin 7 (GPIO4) on the Pi.
Now, let's create the Python script for reading the moisture sensor data and sending it to Thingspeak.
First, we need to install the necessary Python modules:
pip install RPi.GPIO pip install httplib2
Now, let's write a Python script to read the sensor data:
import RPi.GPIO as GPIO import httplib2 import time GPIO.setmode(GPIO.BCM) GPIO.setup(4, GPIO.IN) # the pin where the sensor is connected while True: if GPIO.input(4) == 1: print("Not yet") time.sleep(1) elif GPIO.input(4) == 0: print("Water") time.sleep(15)
This script checks whether the soil moisture level is high or low every 15 seconds and prints out the result.
To send our collected data to the cloud, you first need to create a channel on Thingspeak and get your API-key for sending data to this channel. Update the Python script as follows:
import httplib2 ... # the API-key for your Thingspeak-channel (replace with your key) myAPI = 'xxxxxxxxxxxxxxxx' while True: ... conn = httplib2.HTTPConnection('api.thingspeak.com') conn.request('POST', path) response = conn.getresponse() print(response.read()) conn.close()
Now, when the script detects a low moisture level, it sends an HTTP POST request to Thingspeak which stores the given value in your channel.
With the right tools, and a few lines of Python code, you can develop your own IoT soil moisture detection system. This system can become a vital part of modern agriculture and gardening, enabling real-time improvements and optimized watering schedules.