Raspberry Pi GPIO Support

So I got my breakout board and was able to get GPIO up and running this morning. I decided to go with a general purpose library that relies on config script (JSON) to tie I/O pins and devices. This should make it easy to adapt the to many different kinds of H/W projects via simple configuration. This also provides an example of GPIO on the Pi, for more advanced uses.

Here’s a quick video demo: Explore | Flickr

And the initial commit, I’ll probably continue to tweak this a bit.

Thoughts or suggestions?

@valentin @Carsten the onoff lib requires teardown code to prevent open file handles/leaks. I can imagine several practical applications that could beneficent from some form of teardown code, so I’m considering adding a teardown method to the generic H/W interface and some simple code to capture the process halt event which would call the teardown methods (if present) for each device.

2 Likes

The code cleanup is complete and I submitted a PR. I went ahead and added an exports.shutdown() method to the H/W interface and support to call that (if it exists) in server.js

@V_Mohammed_Ibrahim I’m interested in hearing your thoughts on this. Does this implementation help with the work that you are doing? Feel free to post here or message me directly if you have ideas on how to make this better or if you are interested in working together a custom implementation.

Very nice @KevinOrtman. I can certainly see the benefits of having the shutdown() method. And I like your config approach with the json objects. That should simplify things a lot for PI users. Great work.

2 Likes

@KevinOrtman your work is very nice and i was able to understand it easily , after some research i have found a method for linking the nodejs and python
this method can utilize the nodejs GPIO codes for the pi by calling them using a python script from inside nodejs code
and the best thing is you can send data as text or JSON mode , this should maybe allow us to pass the GPIO data in a similar way as you did the config.json :slight_smile:
This is a fork of the method https://github.com/ibrahim-darkcoder/python-shell.git check it out

Cool. I would be more than happy to work with you on that, @V_Mohammed_Ibrahim.
I will follow up in a direct message so we can work out the details together.

1 Like

@valentin @KevinOrtman i tested this method → https://github.com/ibrahim-darkcoder/python-shell.git and was able to print the result of a python script from nodejs usng console.log() :smile:
We are planning to do the implementation for the gpio interface in purely nodejs first , without any python adapter
and then do the python adapter after that ,( probably using the python-shell method)

1 Like

@KevinOrtman i cloned the beta branch and did npm install , all went fine node server,js started the server successfully , but then i got this error

pi@raspberrypi ~/Desktop/betatest/object $ node server.js
got it started
Loading template: base
Starting System:
Enter loadHybridObjects
TempFolderName: null
 object test has no marker yet
socket.io started
raspberryPi: receive()
/home/pi/Desktop/betatest/object/node_modules/onoff/onoff.js:97
          throw e;
                ^
Error: EACCES, permission denied '/sys/class/gpio/gpio16/direction'
    at Error (native)
    at Object.fs.openSync (fs.js:500:18)
    at Object.fs.writeFileSync (fs.js:1099:15)
    at new Gpio (/home/pi/Desktop/betatest/object/node_modules/onoff/onoff.js:93:12)
    at /home/pi/Desktop/betatest/object/hardwareInterfaces/raspberryPi/index.js:50:21
    at Array.forEach (native)
    at setup (/home/pi/Desktop/betatest/object/hardwareInterfaces/raspberryPi/index.js:38:14)
    at Object.exports.receive (/home/pi/Desktop/betatest/object/hardwareInterfaces/raspberryPi/index.js:104:5)
    at Object.<anonymous> (/home/pi/Desktop/betatest/object/server.js:290:43)
    at Module._compile (module.js:460:26)

tried sudo node server.js and got this

pi@raspberrypi ~/Desktop/betatest/object $ sudo node server.js
got it started
Loading template: base
Starting System:
Enter loadHybridObjects
TempFolderName: null
 object test has no marker yet
socket.io started
raspberryPi: receive()
raspberryPi: adding item with the id = 'led1' and ioName = 'digital'
Creating folder: /home/pi/Desktop/betatest/object/objects/led1/
AddIO objectID: null   raspberryPi
raspberryPi: adding item with the id = 'button1' and ioName = 'digital'
Creating folder: /home/pi/Desktop/betatest/object/objects/button1/
AddIO objectID: null   raspberryPi
it's all cleared
found 1 internal server
starting internal Server.
events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: listen EADDRINUSE
    at exports._errnoException (util.js:746:11)
    at Server._listen2 (net.js:1156:14)
    at listen (net.js:1182:10)
    at Server.listen (net.js:1267:5)
    at Object.<anonymous> (/home/pi/Desktop/betatest/object/server.js:104:52)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)

You probably have another instance of the hybrid objects server running. Try sudo killall node and then try to start the server again. Or just reboot. And you should take some time and learn how to read those error messages yourself :wink:

1 Like

oh yes ! i had run the server in the master branch , that must have been it :smile:
Thanks @Carsten
@Carsten the multiple instance problem is the second one , so the error when i gave node server.js without sudo was because i was not root and the gpio needs root access right ?

Yes, that’s right. You could probably add the user to some group which is allowed to use gpio instead of running the whole thing as root. But I am not sure. Google will be your friend :wink: or just run it as root as you did.