0

This simple bash script always executes with my embedded error message, 'Something went horribly wrong.'

This script was originally coded by someone else; I have no idea what (exactly) gpio -g mode 22 in means, and a google search for it turns up literally nothing.

This was part of a git source from: this project.

  #!/bin/bash

    gpio -g mode 22 in
    echo 'entering if block'
    if [ $(gpio -g read 22) = 1 ]
    then
        sudo python /home/pi/.config/pianobar/scripts/ButtonWatcher.py
        #echo 'This went well.'
    else
        echo 'Something went horribly wrong.'
    fi
2

You can read the man page for gpio there:

http://projects.drogon.net/raspberry-pi/wiringpi/the-gpio-utility/

gpio -g read 22

Means that you read the gpio pin 22 (-g specify you are using the pin numbering using the BCM numbering schema).

The possible caveats are several:

  • have you wired the correct pin?
  • is the line on when you read it?

The line is read just one time, the script does not wait for the pin to go high. You would want to use a loop to read the pin.

0
1

I would just comment on the first response but I don't have enough reputation yet...

As the first comment already explained, the first command tells the GPIO if it is reading or writing to the pin. You can use the actual pin numbers or the BCM pin as referred to here https://projects.drogon.net/raspberry-pi/wiringpi/pins/

gpio -g mode 22 in

is the same as

gpio mode 3 in

Basically this script is checking to make sure you have the GPIO pin set to read and if it is launch the python script. Because the evaluation is failing it goes right to the other condition. For troubleshooting you could echo out the status of the pin.

echo "$(gpio -g read 22)"

The evaluation may also need to have your condition in quotes and try using -eq instead of the = (although either should work).

#!/bin/bash

gpio -g mode 22 in
echo 'entering if block'
if [ $(gpio -g read 22) -eq "1" ]
then
    sudo python /home/pi/.config/pianobar/scripts/ButtonWatcher.py
    echo 'This went well.'
else
    echo 'Something went horribly wrong.'
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.