Introduction: Plug and Play Remote Relay (Raspberry and Arduino and Reading Sensors)

Picture of Plug and Play Remote Relay (Raspberry and Arduino and Reading Sensors)

The goal was to create easy to use End to End control over power sockets using relay, including (and maybe mainly) Web GUI and automation.

As always I try to use existing solutions and rely as little as possible on specific platforms.

A Plug&Play solution and I just added sensor reading as ell.

I wanted to avoid installation of packages like openhab

I had some of building blocks working for sometime, but finding only now the missing two blocks:

1. Online control system (devicehub or Adafruit.IO)

2. Arduino Firmata

Scripts will be available on links

Short video

Let's start

Step 1: Components

HW components

1. Raspberry Pi

2. Arduino UNO

3. Arduino 5V relay with any number of ports (example)

4. Cords

5. Optional temperature sensor (Example I used)

Online platforms

1. Online GUI for control, I use devicehub or Adafruit.io

2. Web automation IFTTT

3. Mobile automation IFTTT and Automagic for Android (like Tasker)

4. I use my own webhooks to send commands to devicehub API will try to find independent solution later

All will be detailed in following steps

Step 2: Arduino and Firmata

Picture of Arduino and Firmata

The easy way to connect to relay is by using Arduino. And the easy way I found to use Arduino is by controlling it from Raspberry.

Connect the Arduino to the relay, I will use 2 ports relay here.

Arduino Relay connection

5V -> VCC

Ground -> Ground

D8 (or any other) In1

D9 (or any other) In2

On the other side of the relay there are three connections for every port, normally it will be COMM NO NC (common, normally open normally close).

Connecting COMM and NO will be normally open circle until 1 is sent to port, and the other way around.

I will not deal with Arduino code, all is done by Raspberry via Firmata firmware..

Upload Firmata firmware

1. Connect Arduino to PC

2. Open IDE go to examples choose Firmata->StandardFirmata and upload to board

That is all

You can check is all works by using Firmata test on Windows PC

You can check if all is working fine by sending 0 or 1 to the above pins, leds will be switched on or off on relay

Step 3: Devicehub

We will deal with devicehub before Raspberry as we need some data for script configuration

Again easy stuff (great work for devicehub guys)

1. Login or create free account, create project, you will be given Api key and project ID, note both

2. Add device, choose Raspberry as development board and Python as programming language.

3. You will be given UUID for the device, note it.

4. Add Actuator, choose whatever name you want (note it) make sure it is digital.

For our project, add two actuators LampA LampB

Note the fact you can make your device public and have publicly accessible dashboard (example)

Step 4: Now to Raspberry

I assume you have working Raspberry, if any of the packages on my script is looking for dependencies I am sure you can deal with it.

Raspberry can be connected to the Web via ethernet WIFI or even cellular modem, I use regular USB WIFI stick

Install pyfrmata library

SSH to Raspberry and type ls /dev

You will be given long list of devices

Connect the Arduino to Raspberry using serial cable

Repeat the ls/dev the newly added port either /dev/ttyACM and a number or /dev/tty/USB and a number

I assume you know how to use Python and editor

Python script

1. It looks for Arduino on the right port (from a list)

2. It makes sure There is internet connection (if run from cron on Raspberry it might be executed before network is established)

3. It reads port state from devicehub (mind the fact you have to change ports on the device you created for the first time, so it has initial value otherwise there will be an error).

4. Current state is stored locally so we can detect changes. Commands are sent to relay via Arduino

5. it loops forever comparing devicehub state to local, if changes are detected, a command will be sent to Arduino and change will be stored locally

Code here

Step 5: Webhooks

I used IFTTT to achieve web automation

Since devicehub expects JSON REST structure and I did not find a way to send it from browser I created my own web hooks

I have php script in my root web server directory

It calls Python script which takes the arguments and send to devicehub

Code

Step 6: Automation

Web

create your own IFTTT account

create recipe for triggering, like every day at XXXX

Create event with maker channel

Use the previous webhook

Mobile

Install IFTTT do button

Create button to call maker channel with previous webhooks

Step 7: Sensor Reading

Picture of Sensor Reading

After I finished the guide I added description how to add sensor, temperature one, but anyone you can read by Raspberry or Arduino will do.

Mind the fact that if you read from Arduino using Firmata you have always to read several times till you get a some value.

I used this sensor for temperature, it's plug&play using Raspberry USB.

The installation guide can be found here

If you follow it you will be able to read temperature calling sudo temper-poll from command line

The output will be something like

Found 1 devices Device #0: 22.5°C 72.5°F

This below python function will return the right format (add to previous script)

You can add the read value to your devicehub dashboard (create analog sensor and add the follwing line to your script using my library

dh.set_sensor_value('temp',dh.project,dh.uuid,dh.apikey,t)

def get_temp():
try:

res = commands.getstatusoutput('sudo temper-poll')

d=res[1]

return d.split(" ")[4][:-3]

except:

return 0

Step 8: Viewing Sensor Data Online

Picture of Viewing Sensor Data Online

The linked image shows the sensor data as displayed on device hub dashboard, but if all you need is to display without control use these two magnificent services

1. Dweet.io

2. Freeboard.io

Dweet gives you the ability to upload data easily (real easy) and even accumulate

And the data can be displayed using gauges on freeboard like that one

Step 9:

Comments

About This Instructable

1,946views

28favorites

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: