Makers, Raspberry Pi, Ubidots

Water your plants with a DIY Smart Sprinkler using PiFace and Ubidots

One of the reasons I mostly have cactuses at home is because I often forget to water my plants. This is why I build this project to control an electro-valve remotely to irrigate my plants from any place just using my phone.

At the end of this tutorial, you should be able to do something like this:

Materials

  • Raspberry Pi model  B:raspberry_pi-1
  • Piface digital

piface-digital

  • Plastic water valve – 3/4″ – 12V nominal

valve

  • Flexible wire for 1A

wire_coils

  • DC barrel jack

dcjack

  • 12 VDC 1000mA Regulated switching power adapter

12vcharger

  • 3/4 ” pvc threaded coupling

coupling

  • Teflon tape

teflon

  • Hose

hose

  • Sprinkler

sprinkler

Connections

The electrical connections are quite easy:

  • First of all connect your piface to the Raspberry Pi without pluging any power adapter.
  • Follow the diagram below.The white wire is GND and its connected to the common terminal in the relay, the red one is connected to the NO (normally open).

sch

As for the hydraulic connections, make sure to use teflon tape on every union to prevent the water from leaking.

Setup your Ubidots account, variables and widgets

If you’re new to Ubidots, create an account here.

  • Navigate to the “Sources” tab and add a new source.

Step1source

  • Select Raspberry Pi as your new data source and fill the form.

Step2source

  • Now click on the new source “My Raspberry Pi”

Step1Variable

  • Add two new variables called “valve” and “relay_state” and don’t forget to complete the fields name and unit.

Step2variable

  • Take note of your variable’s id:

Step3variable

  • Take note of your API Key found in “My Profile –> API Key“.
  • Now, let’s create a new widget to control the valve remotely:

Step1indicator

  • Choose the “Switch” widget and bind it to the variable “valve”. This widget will write “1” or “0” to the “valve” variable, which we’ll poll later from our Raspberry Pi.

Step1switch

  • Add another widget called “indicator” and choose the variable Valve_State:

Step2indicator

  • This is how your dashboard looks now:

dashboard

Coding

Before we can start coding you should’ve already configured your Raspberry Pi with Internet. If not, check this blog post about setting up WiFiNow we can move on, login through a terminal into your Raspberry Pi and set up the SPI module to communicate with the PiFace Digital:

sudo nano /etc/modprobe.d/raspi-blacklist.conf

Add a “#” character before the line spi-bcm2708, then press CTRL-X, type Y and Enter. This enables SPI from boot. Now let’s install and setup the PiFace Digital library:

sudo apt-get update sudo apt-get install python3-pifacedigitalio python-pifacedigitalio

Restart your Pi:

sudo reboot

Great!  we are ready to start coding our project. Create a new file called “valve.py” and paste the following code into it:

sudo nano valve.py

import pifacedigitalio #Library for pifacedigitalio #Library for delays from ubidots import ApiClient #Library for Ubidots pifacedigital = pifacedigitalio.PiFaceDigital() #Declare piface object ##Connect to Ubidots try: api = ApiClient("1fc7a56bf4b539725ace7a3f4aa623e9e9620612") #Don't forget to put your own Apikey valve = api.get_variable('53cd4cb07625425b70f5c21e') #Put here your valve ID's valveState = api.get_variable("53ce95547625420403d81468") #Put here your realy state ID's except: print("cant connect") #Check your Apikey, variable's ID and internet connection while(True): lastValue = valve.get_values(1) #Get the last value of valve from Ubidots rele = pifacedigital.relays[0].value #Save relay state valveState.save_value({'value':rele}) #Send relay state to Ubidots for a in lastValue: print a['value'] if(a['value']): #Turn on or off the relay pifacedigital.output_pins[0].turn_on() else: pifacedigital.output_pins[0].turn_off()

Run your program:

$ sudo python valve.py

Wrapping up

In this example, we were able to control an electrovalve with Piface Digital. We turned on/off an actuator from the Ubidots dashboard, which should enable you to control almost any physical process as long as it can be controlled by a relay.

You can also use Ubidots to measure time-series data such as signal levels, make alerts and control other devices. You might want to check out these other examples:


Do you have more project ideas? Create a Ubidots account and make them happen.