echeadle (87) [Avatar] Offline
#1
In this function, line #A repeatedly tries to open pin 11 causing a stream of errors, Since the example was a throwaway meant to show the difference between polling and event driven code. I removed the gpio.open statement from the readProximity function and put it under the pin variable assignment.

Taken from the book:

var gpio = require("pi-gpio");
pin = 11;
function readProximity() {
gpio.open(pin, "input", function (err) { //#A
gpio.read(pin, function (err, value) { //#B
if (err) exit(err);
console.log(value ? 'there is some one!' : 'not anymore!'); //#C
readProximity();
});
});
}

Changed to:

var gpio = require("pi-gpio");
pin = 11;
gpio.open(pin, "input");

function readProximity() {
gpio.read(pin, function (err, value) { //#B
if (err) exit(err);
console.log(value ? 'there is some one!' : 'not anymore!'); //#C
readProximity();
});
}

If we were to use this code, I would have made sure the open function caught a return error, but since this was just a quick example, this fix shows the difference the book talks about.
Vlad Trifa (16) [Avatar] Offline
#2
Thanks for your thoughts! As mentioned in the other comments, we have updated to onoff so this problem is hopefully gone. Let us know if you find something else!