Introduction: Raspberry With Relay Using Redis and Mobile Automation

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

  1. Raspberry Pi with network connectivity
  2. Redis server somewhere
  3. Automagic package for automation on my Android (Tasker will do as well)
  4. USB relay (I will look later to using Wifi or BT module) this tutorial is for USB
  5. 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

import redis
r = redis.StrictRedis(host='XXXX', port=6379, db=0)

r.hmset('relay',{'2':'off'})

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

http://XXXXX:7379/HSET/relay/1/off

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

if (status1=='on'):

ser.write("e")

print "1 on"

else:

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

  1. Install it on your Android phone (link on frost step)
  2. Create new flow as shortcut and call it 1on (or whatever)
  3. 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
  4. Save and name the flow as 1on or whatever, as long as you follow logic
  5. Click on screen add new shortcut, choose Automagic and from the list choose 1on or the name you used.
  6. 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

Comments

About This Instructable

1,039views

10favorites

License:

Bio: פעיל סביבה ופעיל חברתי, משלב כעת פרויקטים מבוססי ארדוינו
More by Rsegoly:IFTTT button raspberryPlug And Play remote relay (Raspberry and Arduino and reading sensors)Raspberry with relay using redis and mobile automation
Add instructable to: