Raspberry Pi -> Fonera relay control

So, now that I have a Raspberry Pi and a hacked fonera with 4 relays, it’s time to let the Raspberry Pi control the relays of the fonera. This can be done by logging in to the Fonera with ssh and giving some commands.

Of course, having to type these commands everytime you want to switch a relay on or off is cumbersome and useless for automatisation.

Luckily there is an interesting tool available Expect (apt-get install expect).

With expect, I’ve created a small script named gpio.sh that I can use to remotely control the relays of my hacked fonera.

#!/usr/bin/expect -f

set ip		192.168.242.201
set user	root
set passwd 	averysecretpasswd

set gpio	""
set relay  	[lindex $argv 0]
set onoff 	[lindex $argv 1]

# Translate relay to gpio
# -----------------------

if { "$relay" == "1" } {
	set gpio 3
}

if { "$relay" == "2" } {
        set gpio 4
}

if { "$relay" == "3" } {
        set gpio 1
}

if { "$relay" == "4" } {
        set gpio 7
}

# Default is switch relay off
# ---------------------------

if { "$onoff" == "" } {
	set onoff 0
}


# Check if port is valid, if not -> error
# ---------------------------------------

if { "$gpio" == "" } {
        puts "Usage : gpio.sh relay \[1\]\n"
	exit
}

# Put some info on the screen
# ---------------------------

puts "Send $onoff to relay/gpio $relay/$gpio\n"

# Check if $ip is alive, if not -> error
#---------------------------------------

spawn ping -c 1 -W 1 $ip
expect -re "100%"	{
	puts "No connection to $ip"
	exit
}

# DO IT
# ----

set timeout 10
spawn ssh $user@$ip
expect -re "password" 	{send "$passwd\r"}
expect -re "#"		{send "echo 1 > /proc/gpio/$gpio\_dir\r"}
expect -re "#"		{send "echo $onoff > /proc/gpio/$gpio\_out\r"}
expect -re "#"		{send "exit\r"}
close $spawn_id

To switch relay 1 on, I just type ./gpio.sh 1 1.
To switch it off, I type ./gpio.sh 1 0.
Expect does all the magic like entering the password and ”typing” the right commands.

4 comments for “Raspberry Pi -> Fonera relay control

  1. freakqnc
    February 7, 2013 at 1:51 pm

    ?? I guess you are doing it because you already have the la fonera hacked to control the relayboard… otherwise you can control those relays using the raspberry pi directly which can also run a web server (and more) to create the web interface to allow controlling the relays via UI

    • February 7, 2013 at 5:05 pm

      I’m doing all this stuff because it’s fun doing them 😉

      Also, with this setup it is possible to control a relay in a different location (wifi) without the need of cables between the Raspberry Pi and the relay.

      But you are right, it is possible to control a relay directly with a Raspberry Pi.

  2. Mark
    October 5, 2015 at 11:04 am

    Hi there) Can you explain me, as i understood it is possible to control all kinds of relays with a Raspberry Pi.? I was searching for relays here and I found http://hardware.be/weidmuller in normal price for me but i still hesitating to buy it..

Leave a Reply

Your email address will not be published. Required fields are marked *