PiPHP / GPIO

A PHP library for accessing the GPIO pins on a Raspberry Pi.
MIT License
425 stars 43 forks source link

InterruptWatcher always fires callback immediately #2

Closed colinodell closed 8 years ago

colinodell commented 8 years ago

I cannot seem to get the interrupt functionality working - my callback is always fired immediately, regardless of the timeout I set or the state of the pin.

I'm using the exact code from the README, but with two changes:

The screen floods with these messages:

Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1

If I change the state of the pin, I'll see the value change accordingly:

Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 1

But the callback still fires rapidly when the state isn't changing.

I tried testing the underlying stream_select() myself by running the following commands as root...

echo 4 > /sys/class/gpio/unexport # Start from a clean state

echo 4 > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio4/direction
echo "both" > /sys/class/gpio/gpio4/edge

Followed by this PHP script as root:

<?php

$r = $w = [];
$e = [fopen('/sys/class/gpio/gpio4/value', 'r')];

$val = stream_select($r, $w, $e, 99999);

var_dump($val);

The script immediately ends with the following output:

int(1)

This occurs both when nothing is connected to the pin (value is HIGH) and when the pin is connected directly to GND (value is LOW).

I wonder if perhaps this only occurs with certain Pi devices? I'm using a Raspberry Pi 3 running Raspbian Jessie.

colinodell commented 8 years ago

It looks like I was using 0.2.0 but the issue is fixed in 0.2.1. Closing this.