In this project we wanted to control an arbitrary set of 3 loads, from the comfort of the web.
We used a PiFace (http://www.piface.org.uk/products/piface_digital/), which has 2 relays that can switch voltages up to 20V (Max) or currents up to 5A (Max), and 8 "open collector" outputs. Our setup was as follows:
Setup
We placed everything in a tight box and added some connectors to isolate the device pins from the exterior:
The open collector output was connected in serial with a 9V source.
Regarding the software, make sure to:
- Install the Piface Python Library: http://www.piface.org.uk/guides/Install_PiFace_Software/
- Install the Ubidots Python Libraryhttp://ubidots.com/docs/devices/raspberrypi.html#configure-your-raspberry-pi-to-use-our-python-api-client
Create the Ubidots Variables
Login to your Ubidots account or create one here: http://app.ubidots.com/accounts/signup
Then go to the "Sources" tab and create a new data source called "PiFace" or "Actuators" and add 6 variables to it:
The first 3 variables will be read from the Raspberry Pi; if a control variable is "0" then the RPi will set the pin output to "0", if it's "1" then the RPi will set the pin output to "1".
The last 3 variables will be used to provide feedback to Ubidots about the state of the pins.
After creating them, your data source will look something like this:
Coding your Raspberry Pi
Create a file called "piface_control.py":
$ sudo nano piface_control.py
And add this code to it:
Remember to change the code so it has the IDs of the variables we had created, and also grab your Ubidots API Key in your Ubidots profile.
Make sure it's always running
A neat way to make sure our script starts at boot and restarts automatically in case of error is to install "supervisor":
$ sudo apt-get install supervisor
Create a config file:
$ sudo nano
/etc/supervisor/conf.d/piface_controller.conf
And add this text to it:
[program:piface]
command=python /home/pi/piface_control.py
stdout_logfile=/home/pi/log_output.txt
redirect_stderr=true
autostart=true
autorestart=true
startretries=1000000
Now restart your Pi and you should be able to start controlling the PiFace remotely!
Create a dashboard and alerts
Back in your Ubidots account, go to the dashboard tab and add:
- 3 indicator widgets, each pointing to a "status" variable
- 3 switch widgets, each pointing to a "control" variable
This is how it'd look:
Finally, you may want to setup an automatic control of the relays. Let's say you have a separate light sensor and you want to turn on some light bulbs attached to "relay0" when it's getting dark.
Go to the "Events" tab and create an event:
Now you have a way to automate almost any load with the simplicity of Raspberry Pi, Python and Ubidots!
Happy hacking.
Comments