The aim was to create a system where Raspberry controls several relays.
There are many tutorials but I wanted to build a simple one which can be controlled from remote and also automate easily, without modification of home router, so it can work anywhere, as long as there Internet connectivity. This was done eventually by using Redis.
So what is needed
- Raspberry Pi with network connectivity
- Redis server somewhere
- Automagic package for automation on my Android (Tasker will do as well)
- USB relay (I will look later to using Wifi or BT module) this tutorial is for USB
- Cords & sockets for connecting appliances
And I will avoid describing how to make the relay connection in details. Basically there are three connections to control the circuit. you choose by the functionality required, whether when starting the connection is on or off. See in the picture
Step 1: Preparing Redis and Network
Now I need Redis
This is communication package which is stored on memory of server and acts as kind of temporary searchable storage system. You can define items like 'relay' and then keys and values like status 1 = on or off. And then retrieve it easily
I installed Redis server on my Google cloud instance (example)
Example for Python on server to set value to key
r = redis.StrictRedis(host='XXXX', port=6379, db=0)
Since I want to access the Redis server from Web I used also package Webdis and now I can also set values of keys by URL
The Raspberry is examining in 'for ever' loop these keys and will act upon
Step 2: Preparing Raspberry
I will skip all basic Raspberry preparation.
I connected the relay via USB and used Python to control on off
full script here
Need to add python library for redis
I added the call to script on raspberry pi cron (crontab -e)
@reboot sudo python /home/pi/projects/scripts/relay.py &
Basically the script is just a loop which checks the status of keys in redis (forgive the lack of correct indetention, pasting here is not straight forward)
you can play with the delay value if you want to the device to act more quickly or the other way around
print "1 on"
ser.write("o") print "1 off"
Step 3: Mobile Control
Now the fun stuff
As we can control the redis key from URL basically we can do everything now
I will explain about Automagic
- Install it on your Android phone (link on frost step)
- Create new flow as shortcut and call it 1on (or whatever)
- Add action of http request and paste the URL to set relay 1 to on (in my case http://XXXXX:7379/HSET/relay/1/on
- Save and name the flow as 1on or whatever, as long as you follow logic
- Click on screen add new shortcut, choose Automagic and from the list choose 1on or the name you used.
- Create the same for 1off
Now every time you press this icon key 1 will be set on and your raspberry will read and turn on connection 1
Basically you can associate now the action to almost anything. Automagic will let you call this http request if time is XX or temperature is XXX or you are in location XXXX, or use NCF chip.
your imagination will be the limit