Posts From April 2016 - Musing, Rants & Jumbled Thoughts

Header Photo Credit: Lorenzo Cafaro (Creative Commons Zero License)

In my previous posts, (Garage Pi – My Raspberry Pi Playground, GaragePi v2: Temperature and Light, and GaragePi v3: Data Persistence and Visualization and Other Small Improvements) I described the Raspberry Pi-based system I had setup in my garage to detect when the garage door was left open or the light was left on and turn on an LED inside the house. It also reads the current temperature in the garage. This was all originally written in Python (with a node.js-based website added in the third post), but I wanted to play around with other languages on the Pi, so I followed it up with a port to JavaScript and .Net (using Mono).

I'm not going to go into too much detail in this post, because (as I hope to write up a post here shortly), I've since abandoned the RaspPi platform for this project and converted it over to an Arduino-based solution.

Some general notes:

In both cases, I tried to keep the layout of the code similar to the Python layout to provide better side-by-side comparisons of the codebase. In some cases, this may result in code that doesn't match the languages' general styling.

The hardware didn't change for these -- just the software. I also didn't change the website or MongoDB, which let me switch back and forth between languages and compare results using the same db and website. (I added a "source language" field to the db records to help call out any oddness.)

All of this was done about a year ago (Feb 2015) -- I'm just lazy and never got around to writing it up.

.NET/Mono Implementation

The .NET implementation, using Mono, was extremely straightforward. Now, in all fairness, this is the language I spend all day writing at my day job, so it's clearly the one I'm most familiar with, but this is running on Linux and interacting with the hardware -- this is supposed to be hard!

I used the "raspberry-sharp-io" library to interact with the GPIO pins. I had to add RaspPi 2 pin support and wrote a driver for the 1-wire temp sensor, both of which have since been merged to the main repository (open source pull requests for the win!). I also used a NuGet package for the MongoDB client.

If you're thinking about going this route, this may not be a good guide. Since the time that I wrote this code, Microsoft has shifted .NET in major ways, including (pre-release) support for the next version of the framework to run natively on Linux. Additionally, Windows10 IoT edition will run natively on the RaspPi, so you could do this without Linux. At the time I went through this excersize, there wasn't good GPIO/1-wire support in the Win10 IoT releases -- that's likely changed.

I initial expected the performance of running in .NET/Mono to be bad. While I've lost the raw numbers I collected, the performance generally was very good, both for CPU and memory utilization

You can find my code on GaragePi GitHub repo in the "dotNet" folder:

JavaScript Implementation

Wow -- this did not go well. Since several of my sensors rely on precise timing (the Ultrasonic Range Finder in particular), it did not fit well with the JavaScript callback model. I ended up using the r-pi-usonic npm module which is a Node module written in C so that it can better handle the timing. Note: the r-pi-usonic project is no longer maintained..

I also used the rpi-gpio npm module for GPIO pin interactions. For some reason, I had to pull it from a personal clone of the GitHub repo instead of from the npm website, though I don't recall why. I think the version on npm was very stale at the time.

Ultimately, this solution never fully worked for me. It would periodically just die and I never got all the kinks worked it. It also took me significantly more time (like 2 orders of magnitude or more) to implement than Python and .NET combined!

I also investigated projects like Cylon.js, but ended up going deep into a rabbit hole only to hit a major roadblock. Again, this was a year ago, which in JS terms is lifetimes, so your millage may vary.

A big thanks to Clark Sell, Brett Slaski and Brandon Martinez, who worked through some of the uglier JavaScript bits with me. These guys are all on the That Conference Staff and we have a #microcontrollers Slack channel! You should consider going to That Conference is you're reading my post and live anywhere near the US Midwest!

You can find my code on GaragePi GitHub repo in the "JavaScript" folder: