Sunday, November 09, 2014

ModMyPi, node-red, gpio issues, the great WiringPi library

Now that I figured out how to use a PIR motion sensor with Arduino I decided to try the same thing on a Raspberry Pi; pretty easy task since a quick search on google returned a lot of links. Using one of these examples, I put together a quick Python script which worked flawlessly. The only thing I didn't like: the continuous polling of the pin; instead, I wanted to use interrupts, same as I did on my Arduino. A few more searches later I found an article about using the PIR sensor via interrupts; this article is just one among many awesome ones on the ModMyPi blog - I am very happy I discovered this website and very grateful to the authors for all the tutorials, articles and help they offer to the community. Thank you!

Since the Python script in the article mentioned above worked without any issues, I decided to try the same thing using node-red. According to the node-red docs related to RPi, there are 2 ways of interacting with RPi: the gpio command using the rpi-gpio nodes and using the wiring-pi module which is the more complex way. So, I decided to give the rpi-gpio node a try. I created a simple flow with an inject and rpi-gpio nodes; to my surprise, as soon as I deployed the flow, I got an error in the debug window: "Error: Command failed: /bin/sh: 1: gpio: not found". This was very strange because I know I tried the gpio command and it works fine; also, the Python scripts had no issues. After a lot of tries and failures and retries, I think I finally figured out what is happening: this is only an issue when node-red is started automatically on boot using the auto-start script (my current setup is based on these steps including the auto-start script that starts node-red when RPi boots up); if I do a simple sudo service node-red restart then gpio works without a glitch. I guess it may have to do with loading node-red before gpio or something like it but since I am not a Linux wiz, I don't know if this is the case and what to do to fix it but maybe someday I will figure it out.

Because of the issue with gpio I decided to look into using the wiring-pi module which is a wrapper to the WiringPi library - I've heard about it before but never got to use it until now: just looking at the docs and examples I can say that it looks like a great library. Indeed, writing the code to blink an LED in a function node as described here worked great from the first try. The only issue is that I wanted to write code for my PIR sensor using interrupts and while WiringPi has great ways of working with interrupts, I couldn't find anything like that in the wiring-pi wrapper. So, while WiringPi is great and I plan to learn more about it in the future, for my plans it may not work in node-red via the wiring-pi wrapper which is in fact fine because it gives more of an incentive to look into and maybe learn other libraries or even languages.

No comments: