Hardware Reference
In-Depth Information
This command sets the logic level of the GPIO pin to high. If you were to now place a multimeter on P1-04 and
P1-25 you would see that it is running at 3.3 V. This is known as setting the pin high. If you were to set it low by echoing
0 into the value file your voltage would drop back to 0.0 V, thus setting the pin low. If everything has gone according
to plan your mains device should be on. If you are lucky your remote will have an LED on it that lights up when you
press a button. If your unit has this, take a look at it: you will notice it's constantly lit. That's not really simulating a
button press! That's because you have just set the GPIO pin high, which is the same as holding down your finger on
the button. You now need to run the following command to pull the GPIO pin back down to low:
# echo 0 > /sys/class/gpio/gpio4/value
This will simulate a button press. However, coding this is a little trickier than simply reading a value from the
GPIO pins.
Coding a Simulated Button Press
There are many ways to solve this issue. I use a bash script, as seen in the code in Listing 10-1.
Listing 10-1. A Simple Bash Script to Simulate a Button Press
#!/bin/bash
# Description : command line tool to turn on/off a device
# You must have the GPIO pins set up first. Use gpio-setup.sh
# Use : ./cmd-ctl.sh on|off
# Author : Brendan Horan
# Turn on
function on {
/bin/echo 1 > /sys/class/gpio/gpio4/value
sleep 1
/bin/echo 0 > /sys/class/gpio/gpio4/value
# Create a file to indicate current outlet state
/bin/echo power-on > outlet-status
}
# Turn off
function off {
/bin/echo 1 > /sys/class/gpio/gpio17/value
sleep 1
/bin/echo 0 > /sys/class/gpio/gpio17/value
# Create a file to indicate current outlet state
/bin/echo power-off > outlet-status
}
# What shall I do ?
$1
 
Search WWH ::




Custom Search