Skip to content

Instantly share code, notes, and snippets.

@hallgren
Last active June 28, 2017 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hallgren/c68e0cb648e2abc701b85f483d5902e9 to your computer and use it in GitHub Desktop.
Save hallgren/c68e0cb648e2abc701b85f483d5902e9 to your computer and use it in GitHub Desktop.
#!/bin/ash
# MQTT SETTINGS
host="<mqtt_broker_url>"
user="<user>"
password="<password>"
port=1883
topic="<topic>"
#ONION OMEGA2 GPIO PID SETTING
io_pid=0
gpioctl dirout $io_pid > /dev/null
p="backpipe"
ctrl_c() {
echo "Cleaning up..."
pid=$(cat pidfile)
rm -f $p;rm cmds;rm pidfile;kill $pid 2>/dev/null
if [[ "$?" -eq "0" ]];
then
echo "Exit success";exit 0
else
exit 1
fi
}
listen(){
([ ! -p "$p" ]) && mkfifo $p
(mosquitto_sub -h $host -u $user -P $password -p $port -t $topic >$p) &
echo "$!" > pidfile
while read line <$p
do
echo $line > cmds
if grep -q "toggle" cmds; then
gpioctl dirout-high 0 > /dev/null && sleep 1 && gpioctl dirout-low 0 > /dev/null
fi
done
}
trap ctrl_c INT
listen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment