Revision as of 03:14, 11 July 2013 by Skvamme (Talk | contribs) (External interfaces)

Jump to: navigation, search

Erlang on RPi, starting with a fresh image.

sudo apt-get install wget
sudo apt-get install libssl-dev
sudo apt-get install ncurses-dev
tar -xzvf otp_src_R16B01.tar.gz
cd otp_src_R16B01/
sudo make install

You start the interactive shell with erl and quit with Ctrl-g q


Erlang is a programming language which has many features more commonly associated with an operating system than with a programming language: concurrent processes, scheduling, memory management, distribution, networking, etc. During the following days I will use Erlang on RPi to put a sensor value from Gertboard on the web using an interface written in Forth.


Erlang has extremely lightweight processes whose memory requirements can vary dynamically. Processes have no shared memory and communicate by asynchronous message passing. Erlang supports applications with very large numbers of concurrent processes. No requirements for concurrency are placed on the host operating system.


Erlang is designed to be run in a distributed environment. An Erlang virtual machine is called an Erlang node. A distributed Erlang system is a network of Erlang nodes (typically one per processor). An Erlang node can create parallel processes running on other nodes, which perhaps use other operating systems. Processes residing on different nodes communicate in exactly the same was as processes residing on the same node.


Erlang has various error detection primitives which can be used to structure fault-tolerant systems. For example, processes can monitor the status and activities of other processes, even if these processes are executing on other nodes. Processes in a distributed system can be configured to fail-over to other nodes in case of failures and automatically migrate back to recovered nodes.

Soft real-time

Erlang supports programming "soft" real-time systems, which require response times in the order of milliseconds. Long garbage collection delays in such systems are unacceptable, so Erlang uses incremental garbage collection techniques.

Hot code loading

Many systems cannot be stopped for software maintenance. Erlang allows program code to be changed in a running system. Old code can be phased out and replaced by new code. During the transition, both old code and new code can coexist. It is thus possible to install bug fixes and upgrades in a running system without disturbing its operation.

Incremental code loading

Users can control in detail how code is loaded. In embedded systems, all code is usually loaded at boot time. In development systems, code is loaded when it is needed, even when the system is running. If testing uncovers bugs, only the buggy code need be replaced.

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 RPi. I was thinking of using it to display the kW load from my Atlast Forth interface to Gertboard. Here is how to install it:

sudo apt-get install git
git clone git://
cd cowboy

To make it run you first have to tell it to accept HTML:

cd ebin
cp examples/hello_world/src/ ./
erlc ../examples/hello_world/src/*.erl
cd ..
cp examples/hello_world/ .
sudo ./

Start a browser and point it to your RPi:8080, eg

External interfaces

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 examples/hello_world/src/toppage_handler.erl.

%% Feel free to use, reuse and abuse the code in this file.

%% @doc Hello world handler.


init(_Transport, Req, []) ->
        {ok, Req, undefined}.

handle(Req, State) ->
  Port = open_port({spawn, "./priv/atlast -i./priv/kwh.atl"}, [{line,40}]),
  Port ! {self(), {command, "1 gertboard\n"}},
  Port ! {self(), {command, "w\n"}},
    {_,{data,{eol,D}}} -> D
  after 20*1000 -> D = "timeout"
  Port ! {self(), {command, "0 gertboard\n"}},
  {ok, Req2} = cowboy_req:reply(200, [], list_to_binary(D), Req),
  {ok, Req2, State}.

terminate(_Reason, _Req, _State) ->
cd to ebin and compile the file:
cd ebin
erlc ../examples/hello_world/src/toppage_handler.erl

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:

mkdir priv
cp ../atlast-1.2/atlast .

Create a file kwh.atl and put it in the priv directory with the word definitions for getting kW and W load values from Gertboard:

1 gertboard
: kw 3.6 25 getkwh 2drop 25 getkwh float 2 roll float 2swap f/ f/ ." "kw=" f. cr ;


: on 12 0 do 1 setio loop ;
: off 12 0 do 0 setio loop ;

Ok, now we just have to call again and point the browser to or whatever ip address you have on your RPi.

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