PIR sensor

As I would like my HomeAutomation to learn my schedule I need to give the Pi some eyes. Let’s start with a PIR (passive infrared) sensor.
The Fritzing screenshot below shows how the sensor is wired up to the cobbler. Basically the red wire goes to 5V0, the black wire to GND and the yellow data wire is hooked up to #25.

PIR Fritzing

As an initial test the following statement executed from the command line served me well

while true; do gpio read 6; done

This reports a 0 when the PIR is not triggered and a 1 when triggered.

As a next step I replicated the very same functionality in a basic perl script using WiringPi-Perl.

#!/usr/local/bin/perl -w</code>

use lib "/appco.de/WiringPi-Perl";

require "wiringpi.pm";

if (wiringpic::wiringPiSetup () == -1)
{ exit 0};

# set pin #6 (marked with #25 on the T-Cobbler) to mode INPUT
wiringpic::pinMode (6, 0);

while (1){
  # read from PIR

  # PIR not triggered
  if (wiringpic::digitalRead (6) == 0){
    print ("off \n");
    wiringpic::delay (500); # milliseconds
  }

  # PIR triggered
  if (wiringpic::digitalRead (6) == 1){
    print ("on \n");
    wiringpic::delay (500); # milliseconds
  }
}

This code writes “off” to the standard out when the PIR is not triggered and “on” when triggered until you stop the script with ctrl+c.

Blink LED in WiringPi-Perl

I reworked the script from yesterday “Preparing the GPIO basis” using the WiringPi-Perl wrapper.

#!/usr/local/bin/perl -w

use lib "/appco.de/WiringPi-Perl";

require "wiringpi.pm";

if (wiringpic::wiringPiSetup () == -1)
{ exit 0};

# set pin #0 (marked with #17 on the T-Cobbler) to mode OUTPUT
wiringpic::pinMode (0, 1);

while (1){
  # LED off
  wiringpic::digitalWrite (0, 0);
  wiringpic::delay (500); # milliseconds

  # LED on
  wiringpic::digitalWrite (0, 1);
  wiringpic::delay (500); # milliseconds
}

While digging into the details I found a quite useful overview of the function wrappers in the file ../WiringPi-Perl/wiringpi.pm

package wiringpi;

*wiringPiSetup = *wiringpic::wiringPiSetup;
*wiringPiSetupSys = *wiringpic::wiringPiSetupSys;
*wiringPiSetupGpio = *wiringpic::wiringPiSetupGpio;
*wiringPiGpioMode = *wiringpic::wiringPiGpioMode;
*pullUpDnControl = *wiringpic::pullUpDnControl;
*pinMode = *wiringpic::pinMode;
*digitalWrite = *wiringpic::digitalWrite;
*pwmWrite = *wiringpic::pwmWrite;
*digitalRead = *wiringpic::digitalRead;
*shiftOut = *wiringpic::shiftOut;
*shiftIn = *wiringpic::shiftIn;
*delay = *wiringpic::delay;
*delayMicroseconds = *wiringpic::delayMicroseconds;
*millis = *wiringpic::millis;
*serialOpen = *wiringpic::serialOpen;
*serialClose = *wiringpic::serialClose;
*serialPutchar = *wiringpic::serialPutchar;
*serialPuts = *wiringpic::serialPuts;
*serialDataAvail = *wiringpic::serialDataAvail;
*serialGetchar = *wiringpic::serialGetchar;
*serialPrintf = *wiringpic::serialPrintf;

WiringPi Perl

I had some problems with the compilation of Wiring Pi Perl, here is how it worked out for me:

Get the clone from github
git clone git://github.com/WiringPi/WiringPi-Perl.git

This will get you a directory named
WiringPi-Perl

When I tried to compile WiringPi-Perl by using the script
./build.sh
I faced some errors.

It turned out that the link to the perl library needed to be modified from
/usr/lib/perl/5.10.1/CORE to /usr/lib/perl/5.14.2/CORE to match my system.

In addition the sub-directory WiringPi was empty, to fix that I simply cloned wiringPi into the directory
git clone git://github.com/WiringPi/WiringPi.git

This left me with one final error: a missing WiringPi/wiringPi/serial.h
I checked the directory and found a wiringSerial.h.
So I created my own serial.h with cp wiringSerial.h serial.h

With these modifications the compilation completed without any issues.

I did not test it yet, but will try it out soon.

GPIO Pin Mapping

I was looking for an option to use the GPIO Pins with Perl directly. Thanks to Gordon the solution was pretty easy with the wiringPi framework and the perl wrapper wiringPi-Perl created by Gadgetoid. Gordon also created the useful PIN mapping overview which I used for my own table shown below which also includes the translation to the Ardafruit T-Cobbler.

PIN Mapping