The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

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!