Erlang

Erlang on Raspberry Pi, starting with a fresh Wheezy-Raspbian image. Erlang takes a couple of hours to compile (when you run "make").

You start the interactive shell with erl and quit with Ctrl-g q. Read about Erlang at.

Introduction
On this page I will show how to use Erlang on Raspberry Pi to put a sensor value from Gertboard on the web using an interface written in Forth and how to put a picture from Pi Camera on the web.

A Web Server
Erlang is a perfect language to use for programming a web server, and that is exactly what Loïc Hoguin did. Cowboy is one of the most scalable web servers there is and it's also small, so it fits nicely in a Raspberry Pi. I was thinking of using it to display the kW load from my Atlast Forth interface to  Gertboard. Here is how I did it; first install the cowboy web server and test run it. Follow the instructions in the | Getting Started section.

Replace the file nameofyourserver_app.erl with the following code and replace housserver with the name of your server:

This dispatcher will run toppage_handler.erl when called with a plain URL. It will run ws_handler_1.erl when called with URL/kwh, and when called with URL/static/somefile.html it will look for somefile.html in priv/static/

My ws_handler_1 is a websocket handler to let me display kwh-load in realtime on a web page. I'll add that to this wiki at a later time.

External interfaces - Gertboard
Erlang processes communicate with the outside world using the same message passing mechanism as used between Erlang processes. This mechanism is used for communication with the host operating system and for interaction with programs written in other languages. If required for reasons of efficiency, a special version of this concept allows e.g. C programs to be directly linked into the Erlang runtime system. The easy way is good enough, so just open a port to Atlast Forth in the file src/toppage_handler.erl. Replace toppage_handler.erl with the following: and compile the file: Now, we need to put atlast in the ./priv directory, but first we have to get rid of the banner and the prompt.

In atlmain.c comment out PR("ATLAST 1.2 (2007-10-07) This program is in the public domain.\n"); at line 55 and comment out if (!fname) at 162,163,164 and 165. Save and run make again.

In directory cowboy, create a directory priv and copy atlast to it: Create a file kwh.atl and put it in the same priv directory with the word definitions for getting kW and W load values from Gertboard: OK, now we just have to start it up (sudo is needed by the Gertboard driver, not by Cowboy, and for pure simplicity I take another shortcut and starts the Cowboy with sudo rights. Don't do this in a production environment). Remember to change the name "houseserver" to the name of your server. Point the browser to 192.168.0.178:8080 or whatever IP address you have on your Raspberry Pi. After a few seconds the W load should show up. I stop here as this is not the right place to discuss how to make a web GUI; lots of other sites can help with that. Have fun :)

Note: The suggested way to call the port directly from examples/hello_world/src/toppage_handler.erl is just to make it easy. In a real website with thousands of users you should open the port from a separate process and let it run and serve more than one web call.

raspistill
If you have a PiCamera and wants to have a picture on a web page. The cool thing with this is that it's in real time, it is not a file that is served, but a new picture is taken when you do the web call. Check that /opt/vc/bin/raspistill works on your system, then replace the code in examples/hello_world/src/toppage_handler.erl with the following (then follow the Gertboard example for compiling and running): Note: The suggested way to call the port directly from examples/hello_world/src/toppage_handler.erl is just to make it easy. In a real website with thousands of users you should open the port from a separate process and let it run and serve more than one web call.

raspivid
raspivid can create an H.264 video file, and with the command line parameter -o - it will stream to stdout. Below is an Erlang module that takes this stream, slices it in 10 second chunks and sends these slices over a websocket connection to a webserver. Save the code in a file raspivid.erl and compile.

I am using https://github.com/jeremyong/websocket_client as websocket client and Cowboy as websocket server. On the server side you just have to save each chunk in a file and enclose it in a ts-container. I use tsMuxeR for that: You can play the file my_media.ts as it is, or you can put it in an m3u8 playlist. If you want to have one large media file you can just concatenate the 10 second H.264 chunks before running tsMuxeR.

External Interfaces - Onewire
This is a simple owserver client. It can call dir and print a directory listing from a onewire network. First, you have to install owfs: Configure owfs in /etc/owfs.conf

Then, save a file, onewire.erl with the following content: Compile the file with: erlc onewire.erl Start an Erlang console with erl and type onewire:start, and you should see a listing of your onewire network.

Another Erlang module can send this message to onewire: {ow_dir,<<"/",0>>,self} and get back a directory listing.