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

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.
