Closed omkarkhair closed 6 years ago
Is your line 2 incorrect? Surely it should be gpio.setup(7, gpio.DIR_IN);
The reason it's working in the shell and not as a program is that in the shell you have a big delay between you entering each line input, and that delay is allowing the function you're running to complete. In code, everything will run as quickly as it can. You haven't set your code up to be asynchronous so your (presumed) setup
function is immediately followed by the read
, before it completes.
Compare your solution to the first example of reading a pin in the readme. You'll notice that in the example, it passes a callback readInput
, and that callback isn't triggered until setup
completes.
That was it. Damn do I feel stupid now. I thought of the setup callback as optional.
It seems like I am doing something wrong here, but I'm unable to figure out what due to the nature of the problem.
I've setup my
Raspberry Pi 2 Model B v1.1
withNode.js v8.7
and installedrpi-gpio
.I used Node shell (REPL) to test the module, and it worked like a charm. But when I put the same 3 lines of code in app.js and try a node app.js I get the following error. Code added below for reference.
Error:
Code:
Any help is appreciated. I'm investigating what difference the Node shell environment makes that things just work.