7

I'm a semi-linux newcomer and i have a question:

I need to set some GPIO pins in this way: (pin 18 out, pin 17 in) (actually i'm doing this via WiringPi(pyton) trought this commands)

gpio export 18 out

gpio export 17 in

All Works fine if i run these commands via terminal. However i have set these commands in

/etc/rc.local

before exit 0

to run it at boot, but after some tryies it won't work. It could be a path problem? Any help is appreciated, Thanks mboy.

  • Path thing - yes, better fix it to using full path. What happens when you run /etc/rc.local - are you getting any output, is the script runnable? – abolotnov Jan 25 '13 at 20:39
  • Ok, but i'dont know how to set full path.. it's the first time that i use rc.local and wiringpi.. – malgaboy Jan 26 '13 at 10:34
7

I've fixed it! All i needed is to set full path. In my case /usr/local/bin/gpio export 18 out because gpio is a third party command.

2

Another good idea is to create your own script like:

#! /bin/bash gpio mode 18 out gpio mode 17 in exit 0

Save it to file e.g. gpio_init.sh

Run chmod a+x simple_bash.sh to make it executable

Copy to init.d with: sudo cp /path_to_script/script.sh /etc/init.d/

Add to boot sequence: sudo update-rc.d script.sh defaults

0

As an addition for Andrey Volkov's answer, new Debian system has a set format for the init script. Please refers to here

With that I have made a script that should work on your system.

Name the following script gpio_init and chown to root.root and chmod to 755. Copy it to /etc/init.d/

#!/bin/sh
### BEGIN INIT INFO
# Provides:          gpio_init
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       Initialize GPIO pins for the project
### END INIT INFO

SCRIPT=/usr/local/bin/gpio-init
RUNAS=root

PIDFILE=/var/run/gpio_init.pid
LOGFILE=/var/log/gpio_init.log

start() {
  if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
    echo 'Service already running' >&2
    return 1
  fi
  echo 'Starting service…' >&2
  local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
  su -c "$CMD" $RUNAS > "$PIDFILE"
  echo 'Service started' >&2
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo 'Service not running' >&2
    return 1
  fi
  echo 'Stopping service…' >&2
  kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
  echo 'Service stopped' >&2
}

uninstall() {
  echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
  local SURE
  read SURE
  if [ "$SURE" = "yes" ]; then
    stop
    rm -f "$PIDFILE"
    echo "Notice: log file is not be removed: '$LOGFILE'" >&2
    update-rc.d -f gpio_init remove
    rm -fv "$0"
  fi
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  uninstall)
    uninstall
    ;;
  retart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|uninstall}"
esac

This script will call another script under /usr/local/bin/

gpio-init (sorry about the naming here...

#! /bin/bash

function cleanup {
    echo 5 > /sys/class/gpio/unexport
    echo 6 > /sys/class/gpio/unexport
    exit 0
}

# Clean up when exit
trap cleanup EXIT
trap cleanup SIGHUP
trap cleanup SIGQUIT
trap cleanup SIGINT
trap cleanup SIGTERM

echo 5 > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio5/direction
echo 1 > /sys/class/gpio/gpio5/value
sleep 0.000030  # 30us
echo 0 > /sys/class/gpio/gpio5/value
sleep 0.000030  # 30us
echo 1 > /sys/class/gpio/gpio5/value
sleep 0.001

echo 6 > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio6/direction
echo "rising" > /sys/class/gpio/gpio6/edge

sleep infinity

exit 0

A couple of things here. First I use echo GPIO_PIN > /sys/class/gpio/export for exporting the pin, similarly for setting up IN/OUT 1/0 INTERRUPT edge. You can customize it according to your need.

I use sleep infinity to let it run forever

I capture any interrupt signals and do a clean up function where I unexported the GPIO PINS.

Again made this script only writable to root (for security reason) and put it under /usr/loca/bin

Test with sudo /etc/init.d/gpio_init start And sudo /etc/init.d/gpio_init stop

And once ready use sudo update-rc.d gpio-init defaults to run the script at start up

  • 2
    Might work for anyone running an obsolete SysV OS, but hardly relevant. – Milliways Jun 7 at 4:18

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.