It seems to me that the Internet of Things (IoT) and Erlang are like peanut butter and jelly. They just go together. I don’t mean this to be a blanket statement, though. I know there are plenty of IoT devices that do not have the processing horsepower or memory to host Erang, but given its lightweight communicating processes, speed, robustness, solid user base, multi-platform support, and decades of successful use in critical real time systems, it deserves a close look for IoT applications.
On the other hand, I could just be trying to justify to myself, learning Erlang and using it to get my Raspberry Pi to do something useful.
In any case, below are the steps I am taking (I use the present tense deliberately, because I am not there yet.) , to get to the point where I can use an Erlang program to control the RBP’s GPIO pins.
NOTE: All of the commands mentioned in this post, are run via a remote session using SSH.
Step 1. Attach an LED to the Raspberry Pi GPIO pins.
This is probably the simplest piece of hardware that you can attach to your RBP in order to observe it doing something. Just one lead of an LED connected to a GPIO pin, the other lead connected to one lead of a resistor, and the second lead of the resistor connected to a ground pin. Not in possession of a breakout board, I thought I would have to physically attach the LED and resistor leads to the GPIO pins on the RBP board. However, I remembered I had a bunch of ribbon cables from my PC building and upgrading days, so I dug into my basement electronics junk box, and found a 40 conductor ribbon cable with female connectors at both ends. Perfect! It was a simple matter to attach one end of the cable to the RBP GPIO pin header, while the connector on other end of the cable was just waiting to accept the LED and resistor leads.
Step 2. Demonstrate I can turn the LED on and OFF at the command line.
I’m sure there are plenty of how-to articles about this on the web, but this one seemed to be pretty straightforward. Basically, reading or setting the state the GPIO pins, is abstracted as operating system file I/O calls. Slower than actually writing bits directly to the hardware, but safer. I just followed the steps in the linked tutorial, substituting the GPIO pin number I was using where necessary, and sure enough, I was able to turn the LED on and off. Cool!
Step 3. Install Erlang
I followed the steps at this site to download the Erlang source, substituting “17.5” for “17.4”. As the site mentions it takes a few hours, but when it was done, I typed: “erl” at the command, and I was in the Erlang shell.
There is a pre-compiled version available too, but the latest Raspbian build for the Raspberry Pi was labeled R15B03, while the latest Erlang version is 17.5, so I did not use that.
4. Link a GPIO control library to Erang
At this point, I am going to have to hold up a bit and do some thinking. Yeah, I know. Just when it was getting interesting. There are libraries of C code that wrap the functionality to control the GPIO pins, inside of functions. If you look at the source code, you will see, these code libraries do the same thing in code (Operating system file I/O) that we did at the command line in Step 2. These library functions can then be called by other programs. If you are writing a C program, it should be relatively trivial to call a fellow C function in another library. However, to call these C functions from an Erlang program, requires a bit more work. From what I understand, (Which is not much!) I need to create either a Port or a NIF (Native Implemented Function).
I can see I have some more homework to do.