[Solved] Little help with GPIOs and weirdness

I got a pi3 thats it’s running a bit hot now during the summer, so i cooked up a little gadget with a mini fan that checks via crontab every 10min what’s the temperature and fires up the fan if its over 45 oC.
Everything works fine, the only thing that doesnt work is the states of the GPIOs on boot and/or during these 10minutes.
I’ve set up my rc.local like so to enable GPIO2 to be the ‘signal’ for the fan to turn on:

!/bin/sh
echo "2">/sys/class/gpio/export
echo "out">/sys/class/gpio/gpio2/direction
exit 0

Now my main script (fan.sh) is this:

#!/bin/sh
timestamp() {
date +"%Y-%m-%d %T"
}
LOGDIR="/var/log/fan.log"
VALUE=45
TEMP=`vcgencmd measure_temp | cut -c6,7`
STATUS=`cat /sys/class/gpio/gpio2/value`

echo `timestamp` " Info: Temperature: $TEMP">>$LOGDIR
if [ $TEMP -ge $VALUE ] && [ $STATUS -eq 0 ]
then
echo `timestamp` " Warning: Fan started.">>$LOGDIR
echo "1">/sys/class/gpio/gpio2/value
elif [ $TEMP -le $VALUE ] && [ $STATUS -eq 1 ]
then
echo `timestamp` " Warning: Fan stopped.">>$LOGDIR
echo "0">/sys/class/gpio/gpio2/value
fi

The problem is during these 10 minutes the GPIO symlinks are getting deleted for some reason and the $TEMP value is null…so i have to manually do the lines in the rc.local for the whole thing to work again.

  1. Why is this happening ?
  2. Right now as a temp solution i’ve added those 2 lines from the rc.local in my main script under the STATUS line. I know its wrong, but what else can i do ? Any ideas ?

This is my setup btw (where speaker = fan obviously) if anyone wants to do the same :

-Thanks

Not sure – but you could use udev rules to automatically set up GPIO as you want.

1 Like

Should the links get deleted though ? not sure why this is happening…My pi2 under rasbian doesnt do that, thats why i was wondering.

They shouldn’t, but udevadm --monitor might give us some clues.

Ok 1 fix and 1 hack and now it’s working

#!/bin/sh
timestamp() {
date +"%Y-%m-%d %T"
}
LOGDIR="/var/log/fan.log"
VALUE=45
TEMP=`/opt/vc/bin/vcgencmd measure_temp | cut -c6,7`
STATUS=`cat /sys/class/gpio/gpio2/value`

if [ -z "${STATUS}" ]; then
	echo "2">/sys/class/gpio/export
	echo "out">/sys/class/gpio/gpio2/direction
fi

echo `timestamp` " Info: Temperature: $TEMP">>$LOGDIR
if [ $TEMP -ge $VALUE ] && [ $STATUS -eq 0 ]
then
echo `timestamp` " Warning: Fan started.">>$LOGDIR
echo "1">/sys/class/gpio/gpio2/value
elif [ $TEMP -le $VALUE ] && [ $STATUS -eq 1 ]
then
echo `timestamp` " Warning: Fan stopped.">>$LOGDIR
echo "0">/sys/class/gpio/gpio2/value
fi

First the vcgencmd via crontab wasnt working, needs the whole path, so it should be /opt/vc/bin/vcgencmd.
And the tiny hack is to check the STATUS value first, if that’s empty (should be 0 or 1), then do the rc.local lines…then continue with the script.

Thanks Sam, i will take a look at what you said though.