GPIO

From Domoticz
Jump to: navigation, search

Introduction

Support for the standard GPIO is available in Domoticz release 1726 and onwards. Any standard Raspberry Pi features 17 digital In/Out (or even 21 on revision 2 boards) that can now be managed by Domoticz. Only plain logic signals (true/false) are handled.

Compared to the PiFace board, you can not only avoid buying a daughter board to just control leds or detect a signal change, but mostly you can choose how many pins are IN or OUT. The PiFace, on the other hand, has the advantage of giving a bit of protection to the inputs and of conveniently packing the relays and solderless terminal blocks on a clean board.

As both are supported, you're free to choose :-).

Installation

  • The GPIO driver is based on the wiringPi library by Gordon Henderson. Install it like so;
 sudo apt-get install wiringPi

and test the installation with;

 gpio readall

If that doesn't work; try this this procedure

GPIO pins

First, you need to get familiar with the GPIO pins and select which pins are suitable for the goal you want to achieve. I could not explain it better than Gordon, so please refer to those two pages: general considerations and special pin functions. Then you can select which pins you are going to use and configure them.

Configuration

Before starting Domoticz

The pins you intend to use must be made available to unpriviledged processes (as Domoticz should not run with root priviledges). To do so, you need to run a few commands before starting Domoticz. Note that their effect is not permanent, so they should be performed upon each boot before Domoticz starts. Probably the best place is in /etc/rc.local or in the domoticz.sh script. The commands are as follows: (Note: the <BCM PIN #> below refers to the corresponding column on this page)

For outputs

gpio export <BCM PIN #> out

e.g.

gpio export 17 out

For inputs

gpio export <BCM PIN #> in
gpio edge <BCM PIN #> both

e.g.

gpio export 18 in
gpio edge 18 both

Check

Once it's done, you can check the configuration with the following command:

gpio exports

This should return something like:

GPIO Pins exported:
  17: out  0  none
  18: in   0  both

Configure at startup

Because the command is not permanently, you likely want to configure the GPIO settings at startup automatically.
You can do so by adding the lines you would like to use to the 'rc.local'.
Type: sudo nano /etc/init.d/domoticz.sh to edit the file Go to the end of the file, and just add after ':' place your commands.
In my case the /etc/init.d/domoticz.sh looks like this:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          domoticz
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Home Automation System
# Description:       This daemon will start the Domoticz Home Automation System
### END INIT INFO
 
# Do NOT "set -e"
 
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
DESC="Domoticz Home Automation System"
NAME=domoticz
USERNAME=pi
DAEMON=/home/$USERNAME/domoticz/$NAME
#DAEMON_ARGS="-daemon -www 8080 -sslwww 443 -log /tmp/domoticz.txt"
#DAEMON_ARGS="-daemon -www 8080 -sslwww 443 -syslog"
DAEMON_ARGS="-daemon -www 8080 -sslwww 443"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
 
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
 
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
 
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
 
#
# Function that starts the daemon/service
#
do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started
        start-stop-daemon --chuid $USERNAME --start --quiet --pidfile $PIDFILE $
                || return 1
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
                $DAEMON_ARGS \
                || return 2
}
 
#
# Function that stops the daemon/service
#
do_stop()
{
        # Return
        #   0 if daemon has been stopped
        #   1 if daemon was already stopped
        #   2 if daemon could not be stopped
        #   other if a failure occurred
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDF$
        RETVAL="$?"
        [ "$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 $D$
        [ "$?" = 2 ] && return 2
        # Many daemons don't delete their pidfiles when they exit.
        rm -f $PIDFILE
        return "$RETVAL"
}
 
case "$1" in
  start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  status)
        status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
        ;;
  restart)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
          0|1)
                do_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;; # Old process is still running
                        *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
          *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
        exit 3
        ;;
esac
 
:
#Run export GPIO Raspberry
 
/usr/bin/gpio export 17 out
/usr/bin/gpio export 27 in
/usr/bin/gpio edge 27 both
/usr/bin/gpio export 22 in
/usr/bin/gpio edge 22 both


Now issue a reboot (sudo shutdown -r now) and you will see that the GPIO is activated at boot :)

Inside Domoticz

You need first to declare the GPIO port in Domoticz. To do so, go to the "Hardware" tab, type in a name like "GPIO Port" and select "Raspberry's GPIO Port" as type, then click add.
Next, go to the "Switches" tab, and for each GPIO pin, click "Manual Light/Switch" and proceed as follows:

For outputs

Select the GPIO port hardware you just configured, give it a name, select the "On/Off" switch type and a "GPIO" type. You can now select the pin in the list. Obviously, the ones you configured above should not start with the "! NON EXPORTED" mention. You can test the output if you like (the "Test" button will just set the output to HI), and then click "Add Device".

For inputs

Select the GPIO port hardware you just configured, give it a name, select the "Contact" switch type and a "GPIO" type. You can now select the pin in the list. Obviously, the ones you configured above should not start with the "! NON EXPORTED" mention. Then click "Add Device". The mention "open/closed" corresponds to a switch connected between the pin and the ground, with the pin also connected to a pull-up (as you have read on the wiringPi website, some GPIO have an internal permanent pull-up.

Check

You should now be able to click on the light bulb to toggle the outputs, and you should see the contact status change when you connect the pin to GND or VCC.

Have fun!