On Aug 18, 2015, at 8:13 AM, jps birdzbeez wrote:
> I am not a programmer and never claimed to be one.. I just read and read and
> read.. trying to learn and understand.. but for some reason I am not sure I
> am understanding how this works... if someone doesn't mind I need help to
> point me in the correct direction to get my project going.. What I am able to
> do is make it work in python but I like the idea of node.js over python..
>
>
> The project is to use my raspberry pi to raise or lower a door (not a garage
> door). At the top and bottom are Hall Effect sensors. Before raising the door
> I want to verify the door is closed. If it is closed then raise the door till
> it reaches the opendoor state. I have not had any luck with node.js
> programming.. this is what I have come up with so far..
>
> var gpio = require('rpi-gpio');
> var doorclosed = 31; //hall effect sensor when door is closed
> var dooropen = 33; //hall effect sensor when door is open
> var opendoor = 35; //cause door to open
> var closedoor = 37; //cause door to close
>
>
> function raisedoor(callback) {
> //verifydoorclosed(function verified door closed on gpio 31)
> gpio.read(doorclosed, gpio.DIR_IN, readInput);
> //if doorclosed is = true then opendoor till dooropen is true
> if dooclosed = true
> gpio.write(opendoor, 1, function(err){
> gpio.read(dooropen, true, function(err){
> gpio.write(opendoor, 0, function(err){
> callback();
> });
> });
> });
> }
>
> I don't mind reading if someone has a link that explains what I am missing..
> thank you
You said the code doesn't work, but what actually happens when you run the code?
There's one typo, at least: "dooclosed" should be "doorclosed".
"if doorclosed = true" isn't valid JavaScript. You would need to have
parentheses around the condition. Also, "=" is assignment; you want "==" which
is equality comparison: "if (doorclosed == true)"
I've never used GPIO before, but I took a look at the documentation:
https://www.npmjs.com/package/rpi-gpio
And what you've written doesn't seem to correspond to that API. For example, it
says you must use gpio.setup before reading or writing, and the code you've
shown doesn't do that.
The first time you call gpio.read, you're calling it as if it is a synchronous
function, but the documentation shows an asynchronous function (i.e. one that
takes a callback). It also only takes two parameters: a channel and a callback,
but you're passing three parameters.
In your other call to gpio.read, you're passing three parameters again, where
only two are expected, and the callback you're passing only has an error
parameter, whereas the API expects to provide the value that was read as the
second parameter in the callback.
Your code is also never checking or doing anything with the error parameter.
You always have to check if an error occurred, and if so, do something
appropriate.
Knowing, as I said, nothing about GPIO, I'm just guessing here, but as a
starting point, if you wanted to check if the door is closed, it seems like the
code would be:
function raisedoor(callback) {
gpio.setup(doorclosed, gpio.DIR_IN, function (err) {
if (err) return callback(err);
gpio.read(doorclosed, function (err, value) {
if (err) return callback(err);
if (value) {
// the door is closed
} else {
// the door is not closed
}
callback();
})
})
})
Probably the setup code should not actually be in the raisedoor function, but
instead in an initialization function you call once at the beginning of your
program.
You said you want to raise the door until it reaches opendoor state, but your
code doesn't contain any loops that would be necessary for implementing "until"
logic. Of course, you shouldn't actually write such loops in node since that
would block the event loop. Instead you'll probably need to check the sensor,
and if it is not yet the right value, use setTimeout to wait a number of
milliseconds and then check again. Or, better yet, it looks like the API sends
"change" events when a value has changed, so instead, you should listen for
that event.
The similarity of the names of your variables (dooropen, opendoor) is confusing.
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/669681BE-B479-439A-B55A-8E0F56CB94A5%40ryandesign.com.
For more options, visit https://groups.google.com/d/optout.