Friday, November 14, 2014

The almost perfect Raspberry Pi dev setup

I want to learn more coding for Raspberry Pi and Arduino and one problem I have is being able to do so from different places and machines. I use both github and bitbucket and they are awesome but not once it happened that I wrote a bit of code and forgot to commit before leaving that machine. Even if I could be more careful with this, there is the aspect of having an IDE installed everywhere like Eclipse for Java, Arduino IDE, IDLE for Python. The solution for Arduino coding is codebender that I mentioned in a couple of my previous posts. Python I am mostly doing on Raspberry Pi and the solution is Adafruit's WebIDE - it is a great editor and one of the first things I install on every SD card for my Raspberry Pis.

For Java, I started a couple weeks ago to look at online editors like Cloud9 and others. Cloud9 looks great and it is one of the few editors of this kind that handles Java projects. However, recently I found an article (unfortunately, I didn't keep the link) that was reviewing several online editors from a Java coding point of view and the one the author of that article liked the most was Codenvy about which I have to admit I didn't even know until then. There were a lot of pro arguments for it in the article so I decided to give it a try. And I am so happy I did! As I read in the article, creating Java projects is a breeze; I tried both a new project and one cloned from a git repo: both worked like a charm. As a side note, I don't know Maven and Codenvy uses Maven (same as Heroku and other cloud apps) so this was a bit of a hiccup for me but I'm learning.

Codenvy integrates with both github and bitbucket so one can keep online and offline projects in sync this way. Also, there is a very powerful command line utility, Codenvy CLI that can be used to interface with projects, take them offline, or execute critical developer services.

But the thing I absolutely love about Codenvy is the fact that it has an Eclipse plugin that makes Eclipse aware of the Codenvy projects and allows both updating the Eclipse project with the Codenvy changes and the other way around. This is absolutely awesome! Now I can write code from anywhere without having Eclipse installed locally and when I get home, update the Eclipse project with these changes, keep coding and when done pushing the new code back to Codenvy. More, my Eclipse setup at home uses a plugin I found recently, LaunchPi that makes it possible to run and debug Java projects on Raspberry Pi directly from Eclipse. I found another similar plugin but it needed to be configured, with jars to be installed on the Pi and started separately; LaunchPi is so simple, just configure the Pi's IP address, the main class to run and done; and it also allows debugging which is simply great.

And if I want to deploy the code to the Pi, not just test it, I can always use a Maven goal to do this; as an example, look at the pom file used by Robert Savage, the author of the amazing Pi4J - but about this awesome library in another post.

I love this setup and the only reason I said in the title the "almost" perfect setup because there is one thing missing: if I could run and debug the code on the Pi from the Codenvy editor directly, that would be the icing on the cake. Maybe it is possible, I know there are ways to create a Pi emulator using QEMU but I haven't figured out yet how to do it and if it is possible to use it directly from Codenvy; maybe not but I'll try to find a way. Even without that, I am very happy with the current setup. Now, to do some coding!

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.