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 electrovalve 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:
1. Materials
Raspberry Pi model B:
Piface digital
Plastic water valve - 3/4" - 12V nominal
Flexible wire for 1A
DC barrel jack
12 VDC 1000mA Regulated switching power adapter
3/4 " pvc threaded coupling
Teflon tape
Hose
Sprinkler
2. 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).
As for the hydraulic connections, make sure to use teflon tape on every union to prevent the water from leaking.
3. 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.
Select Raspberry Pi as your new data source and fill the form.
Now click on the new source "My Raspberry Pi"
Add two new variables called "valve" and "relay_state" and don't forget to complete the fields name and unit.
Take note of your variable's id:
5Take note of your API Key found in "My Profile --> API Key".
Now, let's create a new widget to control the valve remotely:
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.
Add another widget called "indicator" and choose the variable Valve_State:
This is how your dashboard looks now:
4. 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 WiFi. Now 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're 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 delaysfrom ubidots import ApiClient #Library for Ubidotspifacedigital = pifacedigitalio.PiFaceDigital() #Declare piface object##Connect to Ubidotstry: 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'sexcept: print("cant connect") #Check your Apikey, variable's ID and internet connectionwhile(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()
view rawValve.py hosted with ❤ by GitHub
Run your program:
$ sudo python valve.py
5. 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!
Originally Published in Ubidots Blog July 28, 2014.