googlecreativelab / coder

A simple way to make web stuff on Raspberry Pi
http://goo.gl/coder
Apache License 2.0
2.42k stars 275 forks source link

Using RPi GPIO #45

Closed manarjan closed 10 years ago

manarjan commented 11 years ago

Can the GPIO pins of the Raspberry board be used in anyway through our web design.. to make a sort of home automation system

thanks..

rotmgkillroyx commented 11 years ago

I was just about to try to tackle this. I was going to combine this project: https://code.google.com/p/webiopi/wiki/RESTAPI with some directions like the ones here: http://stackoverflow.com/questions/5643321/how-to-make-remote-rest-call-inside-node-js-any-curl.

If anyone has any pointers, or a better way to do this, I'd be happy to learn a better approach.

Thanks!

mrutgerj commented 11 years ago

I am in the process of coding this. First and foremost, I adapted a google coder application with node.js functions, because I want to use node.js gpio libraries. The app that I am customizing is: http://www.codeproject.com/Articles/653230/Raspberry-Pi-Episode-3-Get-Coding-With-Coder

I made a button on the web front end to query the value of the pin. That adds the name of the pin and the value of the pin to the CSS table. However, it is not working. I am using the node.js based rpi-gpio, which you can install with npm. I installed it both locally and globally. I do not get an error on the "require" statement in the node.js file, but any server side calls do not work. You DO have to give your user access to the GPIO pins, to do so use this: https://github.com/quick2wire/quick2wire-gpio-admin

So, I'm almost there. I got webiopi running just fine and was able to read some 1-wire devices. I realize that it has a javascript interface too, but I don't think it is node.

A library that I want to use is onoff. It is more sophisticated than the others. https://npmjs.org/package/onoff

BUT that requires a node.js version greater than what coder is distributed with. I tried upgrading node.js but the bindings required by google coder need to be updated. On my TODO list.

At this point I'm just trying to read GPIO from node.js, via an ajax call from the javascript file, and generate some HTML. After that, I'm adapting Dave's TODO demo to have the "addItem" functionality to POST and change pin values.

Lastly, a good comparison of the node.js gpio libraries is right here: http://thefloppydisk.wordpress.com/2013/06/06/raspberry-pi-gpio-inputoutput-in-javascript/

I'll keep you updated on any progress, hopefully generating a .zip to install to google coder.

mrutgerj commented 11 years ago

I am trying to setup eclipse to debug the node.js app.js as a remote "virtual project" as described by google. If anyone has succeeded in this, please share.

https://code.google.com/p/chromedevtools/wiki/DebuggerTutorial

Otherwise, I can't really see what's going on with the javascript server-side GPIO calls, and why they are failing.

DexterInd commented 11 years ago

This would be awesome! +1!

brannondorsey commented 11 years ago

I too have been using pi-gpio with Node.js independent of Google Coder and am having trouble to get it working Coder. I have a working example on the Pi but not through Coder itself. For some reason I can't access Coder's Node from anywhere but coder. i.e. if I run node example.js from my home/ I get a command not found error. I had to install a new Node.js on the Pi. I was also wondering how debugging works on Coder. Writing in Node.js without access to a console where it can be monitored sounds absolutely terrible. Have you had any luck @mrutgerj ?

mrutgerj commented 11 years ago

I've had luck! But first you need to learn how to debug coder, or just debug node.js, period. This can be difficult to setup with Eclipse, which is a beast of a program. You love it and hate it. For this purpose, I hated it! After a day of trying to get it to work, I took a different approach with the webgui node-inspector. I'm impressed. But if you try installing it, the inspector will report that it needs a version of node greater than 8. Coder is released with version 6.19.

So I upgraded node to version 10.9, and now I can use the node gpio module onoff too! Upgrading is a little tricky, I wrote a HOWTO here: http://www.mrjansen.com/Raspberry-Pi/howto-upgrade-node-on-google-coder.html

If anything doesn't work, please report it in the comments section, to avoid too much off-topic here. After you have successfully upgraded node, you can install node-inspector and start debugging on the raspberry pi with ease.

Let me have a cup of joe, I'll write up a HOWTO on debugging, and report back here. Then we can more effectively debug the server-side GPIO calls. Otherwise it is all a shot in the dark.

mrutgerj commented 11 years ago

Debugging!

The formatting is wonky, I'll work on that after laundry. But here is a howto on debugging Google Coder with Node Inspector. Comment with errors or suggestions on my site, so we don't muck up github with our nonsense.

http://www.mrjansen.com/Raspberry-Pi/howto-debug-node-on-the-raspberry-pi-with-node-inspector.html

Next howto: getting GPIO on node.js working, with a web interface. Hopefully.

mrutgerj commented 11 years ago

Ok I figured out why it isn't working. There aren't ANY mappings in /sys/class/gpio/

as in: /sys/class/gpio/gpio0 /sys/class/gpio/gpio1 /sys/class/gpio/gpio2

Can any of you confirm that you have these mappings? Did I install something that messed things up? Otherwise, I am able to debug rpi-gpio on node.js and couldn't be happier with it.

brannondorsey commented 11 years ago

I can confirm that the above files are missing in my Coder which was just loaded with the v0.4 img. My /sys/class/gpio directory contains only:

Does this mean that we have still been unsuccessful with any read/write access to gpio from Coder?

mrutgerj commented 11 years ago

Correct, the RPI-GPIO lib, and others, call on pins that are already defined here. I created a new issue addressing this, and started poking around for a script to enable these GPIO pins so we can use them.

But, Coder should really be distributed with these defined by the default kernel driver.

brannondorsey commented 11 years ago

I have had luck using GPIO via the pi-gpio module (NOTE: not rpi-gpio) on a Pi with Coder but I was using a version of Node.js that I installed via this method. At that time I did not know that Coder's node was invoked with nodejs instead of node. What do you think would cause this to work but not using Coder's node, even if you upgrade to node v0.10.9?

mrutgerj commented 11 years ago

The way they access the pins are different. RPI-Gpio has its read PATH set to:

var PATH = '/sys/class/gpio';

This is the "unsafe" way to access the gpio, but I'm not sure what that means.

Now, pi-gpio uses a different method - the quick2wire gpio admin package: https://github.com/quick2wire/quick2wire-gpio-admin That uses the path:

sysFsPath = "/sys/devices/virtual/gpio";

Now, how does pi-gpio call the export function, to set these pins up? On the fly. If you open pi-gpio.js, look at the var gpio funtion. Each time a method is called (open, setDirection, read), it performs these functions on the fly.

Which way is better, faster, more foolproof? I don't know. Here's a better description from http://thefloppydisk.wordpress.com/2013/06/06/raspberry-pi-gpio-inputoutput-in-javascript/

rpi-gpio has two key differences to the pi-gpio package:

pi-gpio functions use the Raspberry Pi GPIO I/O port numbers rather than the physical pin numbers. That means if you physically connect a peripheral to the RPi header pin 11, you need to address it by its GPIO port designation 17.

All of the I/O functions of the rpi-gpio package are asynchronous, so where necessary – for example in reading the value of a pin – a callback must be provided. This package is thus more closely aligned with the asynchronous nature of the Node platform.

As I look through the code of RPI-GPIO, it seems as if it takes the same route in dynamically creating and modifying the pins, so I think it is a permissions problem. I'll do a bit more debugging and messing around.

mrutgerj commented 11 years ago

.... and after some experimentation, forget about RPI-GPIO and use PI-GPIO instead. I started modifying RPI with PI code, using exec calls instead of the self.emit stuff in RPI. And I realized they are pretty much the same package.

There is a permissions problem with PI-GPIO though, see the new issue I opened.

jmstriegel commented 10 years ago

I think you need to add the coder user to the gpio group for gpio-admin to work properly. This all boils down to the GPIO pins only having root permissions, but coder runs as the 'coder' user. Let's sort out a preferred way for this all to work seamlessly, and I'll get the 'official' recommended pi gpio setup into the coder download image.

mrutgerj commented 10 years ago

Well, you were right. It does boil down to user permissions. I thought these libraries would work simply by putting pi and coder in group gpio. I have the 'onoff' node library working, but only after exporting pins via (synchronous) shell commands (via node package exec-sync). A better way is to call exec (or spawn) asynchronously, and use callbacks, but it takes a 'for loop' to export all the pins less than a second.

The ability for node libraries to access GPIO would have to rely on shell calls or just executing a shell script asynchronously, and then checking if the pins were indeed exported. Or do you have an idea how to do this natively?

Right now I plan to experiment with the SPI and i2c bus access, via node modules.

There is also the possibility of using node-ffi to call functions in other libraries, like wiringPi. But, I tried that just now and it doesn't seem to work at all. There is also "significant" overhead with ffi calls.

jmstriegel commented 10 years ago

This is all fixed and working in v0.5 which can now be downloaded at http://goo.gl/coder

I've also created a project that shows how to use GPIO inputs and outputs here: http://googlecreativelab.github.io/coder-projects/projects/blinky_lights/

speccy88 commented 10 years ago

In the blinky lights project, in app.js you have this line : var gpio = require("gpio"); So I suppose there is a gpio library. I would like to see the source code of this, like where the method gpio.export is defined? ps: I'm a total node.js noob