I'm doing some experiments with openHAB2 on my Raspberry Pi 3 (with Raspbian installed).

At the moment I've connected a LED to GPIO pin #2 and I'm able to turn it on/off from openHAB2.

The problem is that when I shutdown my Raspberry, the LED linked to GPIO pin #2 remains active.

I've read this link from OpenHAB official documentation about GPIO binding and seems that I have to unexport GPIO pin editing the body of the do_stop() function on openHAB init.d script.

I've done two attempts:

1) I've edited the do_stop() function that way (copy-paste from openHAB documentation about GPIO):

   start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile 
   #unexport all gpio's
   echo 2 > /sys/class/gpio/unexport

   [ "$RETVAL" = 2 ] && return 2
   # Wait for children to finish too if this is a daemon that forks
   # and if the daemon is only ever run from this initscript.
   # If the above conditions are not satisfied then add some other code
   # that waits for the process to drop all resources that could be
   # needed by services started subsequently.  A last resort is to
   # sleep for some time.
   start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec 
   [ "$?" = 2 ] && return 2
   # Many daemons don't delete their pidfiles when they exit.
   rm -f $PIDFILE
   return "$RETVAL"

2) I've edited the default init.d script adding only the line about unexporting GPIO pin #2:

do_stop() {
  log_daemon_msg "Stopping $DESC" "$NAME"

  echo "2" > /sys/class/gpio/unexport

  if /usr/share/openhab2/runtime/bin/stop
    # workaround stop returns before the openhab process has really stopped
    if stpid=`cat /var/run/openhab2.pid 2> /dev/null`; then
      killwaitpid $stpid
    rm -f $PIDFILE
    # In case the pid in the pidfile was wrong, also kill/stop existing process
    killwaitpid $EXISTINGPID
    log_end_msg 0
    return 0
    log_end_msg 1
    return 1

Both solutions don't work and the LED turns ON after Raspberry was turned off, even if I turn my Raspberry off with the LED OFF; where is the mistake?

  • I have a stupid question: before unexporting the GPIO pin, have you tried turning the LED off first?
    – tlhIngan
    Nov 11 '17 at 22:36
  • Yes @tlhIngan, I've updated the question ;) I've also tried to turn off the LED before shutting down the Raspberry and after the shutdown, the LED turns ON :( Nov 13 '17 at 21:33

If you are using BCM2 (physical pin 3), this has a 1.8kΩ pullup resistor. This is intended for use as I²C SDA, and the state of the pin will be HIGH, even if set as INPUT, unless you deliberately pull it low.

In general pin 3, 5 should not be used for GPIO, unless you make allowance for the pullup.

There are 7 pins with pullup, although most use the internal pullup, and are easily over-ridden, but can still activate external circuits - see http://www.panu.it/raspberry/

  • Thanks for your help mate! I was using a PIN with pull high! Now, using a pull low PIN it works fine. So the openHAB script that I wrote in the question was made for "deliberately pull it low" a pull high PIN? If so, why the script doesn't work? Thanks in advance! :) Nov 13 '17 at 21:40
  • @RobertoMilani If you are referring to pin 3 the pullup is a physical resistor on the board.
    – Milliways
    Nov 13 '17 at 22:52

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .