Skip to Main Content






Smart Farm

Install libaries

Install library for using Grove Pi

Open a terminal and use the below commands to install needed library for Grove Pi.

$ curl -sL https://github.com/Seeed-Studio/grove.py/raw/master/install.sh | sudo bash -s -
$ sudo reboot

Install the library for DHT11 sensor

An additional package needs to be installed in order to use the temperature and humidity sensor (DHT11). To do this, open a terminal window and run the below command.

pip3 install seeed-python-dht

Python program

After you successfully installed the required libraries, you can write your Python script in order to read the sensors data and connect it to your IoT interface:

Importing libraries

First, we start by adding the needed libraries.

import time
#library for using the relay
from grove.grove_relay import GroveRelay
import sys
#library for grove pi
import grovepi
from grove.adc import ADC
import math
#library for temperature and humidity sensor
import Adafruit_DHT
#library for connecting to your IoT interface
from Adafruit_IO import Client, Feed

Connect to IoT interface

In the IoT interface section you made an user interface and you learnt how to get your key and username. In this part of the code we will connect to the interface.

#insert your key and username
ADAFRUIT_IO_KEY = '#insert_your_key'
ADAFRUIT_IO_USERNAME = 'insert_your_username'
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

Set up your IO feeds

We are going to use the feeds that you made on your interface in the previous section and we will receive and send data to them.

#set up IO feeds, naming should be the same in your program and IoT interface
temperature_feed = aio.feeds('temperature')
humidity_feed = aio.feeds('humidity')
moisture_feed=aio.feeds('moisture')
water_feed=aio.feeds('water')

Initialize variables

Here we defined some variables which we will use in the program later and we give them initial values. E.g. I want the initial value for my soil moisture to be 0 when I first start the program.

# DHT sensors come in two colours blue and white
#our temperature sensor is blue and the code for blue is 0 if yours is white you can change it to 1
blue=0
#initial value for soil_sensor_value
soil_Sensor_Value=0
# we turn off the relay at first, my relay is on D4 pin so relay=4 if your is on 5 then relay=5
relay = 4
grovepi.digitalWrite(relay,0)

Sending and receiving data

We want a loop in our program to keep reading and sending data from sensors to the interface and vice versa. We read the temperature, humidity and soil moisture data from the sensors and send them to our interface. We read our water feed data from the interface and send it to our relay which means that if the user turns on the watering system on the interface, we send this data to the program and the program turns on the relay.

while True:
    try:
        #5 is the number of pin that I connect my sensor if you e.g. connect in to D16 it will be 16 instead of 5
        [temperature, humidity] = grovepi.dht(5,blue)
        #send the temperature data to temperature feed
        aio.send(temperature_feed.key, str(temperature))
        #send the humidity data to humidity feed
        aio.send(humidity_feed.key, str(humidity))
        # read the soil moisture data from A0 if you connect the sensor to another  analog pin you can change 0 to that number
        moisture=grovepi.analogRead(0)
        #send the moisture data to moisture feed
        aio.send(moisture_feed.key, str(moisture))
        #sleep for 2 seconds, to reduce the number of messages send to the interface
        time.sleep(2)
        # receive water feed from interface to see whether it is on or off
        water = aio.receive(water_feed.key)
        #print soil moisture values, temp and humidity on your terminal for checking purposes
        print("moisture = %.02f"%(soil_Sensor_Value))
        print("temp = %.02f C humidity =%.02f%%"%(temperature, humidity))
        # print if water feed is 1 (on) we will turn on relay otherwise we will turn it off
        if int(water.value) == 1:
            print('received watering <- ON\n')
        elif int(water.value) == 0:
            print('received watering<- OFF\n')
         #write the water feed value to relay, turn it on if it is 1, turnn off it is 0
        grovepi.digitalWrite(relay,int(water.value))
    except IOError:
        print ("Error")

You can download the python file here.