Difference between revisions of "Erlang"
m (→External Interfaces - Pi Camera) |
m |
||
(27 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | Erlang on RPi, starting with a fresh | + | Erlang on RPi, starting with a fresh wheezy-raspbian image. Erlang takes a couple of hours to compile (when you run "make"). |
<code><pre> | <code><pre> | ||
Line 5: | Line 5: | ||
sudo apt-get install libssl-dev | sudo apt-get install libssl-dev | ||
sudo apt-get install ncurses-dev | sudo apt-get install ncurses-dev | ||
− | wget http://www.erlang.org/download/ | + | wget http://www.erlang.org/download/otp_src_R16B02.tar.gz |
− | tar -xzvf | + | tar -xzvf otp_src_R16B02.tar.gz |
− | cd | + | cd otp_src_R16B02/ |
./configure | ./configure | ||
make | make | ||
sudo make install | sudo make install | ||
</pre></code> | </pre></code> | ||
− | You start the interactive shell with erl and quit with Ctrl-g q | + | You start the interactive shell with erl and quit with Ctrl-g q. Read about Erlang here [http://learnyousomeerlang.com] |
== Introduction == | == Introduction == | ||
− | + | On this page I will show how to use Erlang on RPi 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 == | == 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 [[Forth | Atlast Forth]] interface to [[RPi Gertboard | Gertboard]]. Here is how I did it, first install the cowboy web server and test run it: | + | Erlang is a perfect language to use for programming a web server, and that is exactly what Loïc Hoguin did. [http://www.ninenines.eu] 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 [[Forth | Atlast Forth]] interface to [[RPi Gertboard | Gertboard]]. Here is how I did it, first install the cowboy web server and test run it: |
<code><pre> | <code><pre> | ||
sudo apt-get install git | sudo apt-get install git | ||
Line 45: | Line 27: | ||
To make it run you first have to tell it to accept HTTP: | To make it run you first have to tell it to accept HTTP: | ||
<code><pre> | <code><pre> | ||
− | cd ebin | + | cd ~/cowboy/ebin |
cp examples/hello_world/src/hello_world.app.src ./hello_world.app | cp examples/hello_world/src/hello_world.app.src ./hello_world.app | ||
erlc ../examples/hello_world/src/*.erl | erlc ../examples/hello_world/src/*.erl | ||
cd .. | cd .. | ||
cp examples/hello_world/start.sh . | cp examples/hello_world/start.sh . | ||
− | + | ./start.sh | |
</pre></code> | </pre></code> | ||
Start a browser and point it to your RPi:8080, eg 192.168.0.178:8080 | Start a browser and point it to your RPi:8080, eg 192.168.0.178:8080 | ||
Line 85: | Line 67: | ||
cd to ebin and compile the file: | cd to ebin and compile the file: | ||
<code><pre> | <code><pre> | ||
− | cd ebin | + | cd ~/cowboy/ebin |
erlc ../examples/hello_world/src/toppage_handler.erl | erlc ../examples/hello_world/src/toppage_handler.erl | ||
</pre></code> | </pre></code> | ||
Line 94: | Line 76: | ||
In directory cowboy, create a directory priv and copy atlast to it: | In directory cowboy, create a directory priv and copy atlast to it: | ||
<code><pre> | <code><pre> | ||
+ | cd ~/cowboy | ||
mkdir priv | mkdir priv | ||
cp ../atlast-1.2/atlast ./priv | cp ../atlast-1.2/atlast ./priv | ||
Line 106: | Line 89: | ||
: w 25 getkwh 2drop 25 getkwh 36 * swap 100 / / ." "w=" . cr ; | : w 25 getkwh 2drop 25 getkwh 36 * swap 100 / / ." "w=" . cr ; | ||
</pre></code> | </pre></code> | ||
− | Ok, now we just have to call start.sh again and | + | Ok, now we just have to call start.sh again ( 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). |
+ | <code><pre> | ||
+ | sudo ./start.sh | ||
+ | </pre></code> | ||
+ | Point the browser to 192.168.0.178:8080 or whatever ip address you have on your RPi. After a few seconds the W load should show up. I stop here, this is not the right forum 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. | 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. | ||
== External Interfaces - Pi Camera == | == External Interfaces - Pi Camera == | ||
− | If you have a PiCamera and wants to have a picture on a web page, replace the code in examples/hello_world/src/toppage_handler.erl with the following: | + | 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). |
<code><pre> | <code><pre> | ||
%% @doc Hello world handler. | %% @doc Hello world handler. | ||
Line 124: | Line 111: | ||
handle(Req, State) -> | handle(Req, State) -> | ||
− | Port = open_port({spawn, "/opt/vc/bin/raspistill -o -"}, [binary]), | + | Port = open_port({spawn, "/opt/vc/bin/raspistill -t 2 -w 1024 -h 768 -o -"}, [binary]), |
− | + | Str = loop(Port,<<>>), | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{ok, Req2} = cowboy_req:reply(200, [], Str, Req), | {ok, Req2} = cowboy_req:reply(200, [], Str, Req), | ||
{ok, Req2, State}. | {ok, Req2, State}. | ||
Line 137: | Line 118: | ||
terminate(_Reason, _Req, _State) -> | terminate(_Reason, _Req, _State) -> | ||
ok. | ok. | ||
+ | |||
+ | loop(Port,Frame) -> | ||
+ | receive | ||
+ | {Port, {data, Chunk}} -> | ||
+ | Size = byte_size(Chunk) - 2, | ||
+ | case Chunk of | ||
+ | <<_:Size/binary,255,217>> -> Framestring = base64:encode_to_string(<<Frame/binary,Chunk/binary>>), | ||
+ | "<html><img src = 'data:image/jpeg;base64," ++ Framestring ++ "'></html>"; | ||
+ | _ -> loop(Port,<<Frame/binary,Chunk/binary>>) | ||
+ | end | ||
+ | after 20*1000 -> "<html>timeout</html>" | ||
+ | end. | ||
+ | |||
</pre></code> | </pre></code> | ||
+ | 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. | ||
+ | |||
[[Category: RaspberryPi]] | [[Category: RaspberryPi]] |
Revision as of 14:24, 18 September 2013
Erlang on RPi, starting with a fresh wheezy-raspbian image. Erlang takes a couple of hours to compile (when you run "make").
sudo apt-get install wget
sudo apt-get install libssl-dev
sudo apt-get install ncurses-dev
wget http://www.erlang.org/download/otp_src_R16B02.tar.gz
tar -xzvf otp_src_R16B02.tar.gz
cd otp_src_R16B02/
./configure
make
sudo make install
You start the interactive shell with erl and quit with Ctrl-g q. Read about Erlang here [1]
Contents
Introduction
On this page I will show how to use Erlang on RPi 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. [2] 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 I did it, first install the cowboy web server and test run it:
sudo apt-get install git
git clone git://github.com/extend/cowboy.git
cd cowboy
make
To make it run you first have to tell it to accept HTTP:
cd ~/cowboy/ebin
cp examples/hello_world/src/hello_world.app.src ./hello_world.app
erlc ../examples/hello_world/src/*.erl
cd ..
cp examples/hello_world/start.sh .
./start.sh
Start a browser and point it to your RPi:8080, eg 192.168.0.178:8080
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 examples/hello_world/src/toppage_handler.erl. Replace toppage_handler.erl with the following:
%% @doc Hello world handler.
-module(toppage_handler).
-export([init/3]).
-export([handle/2]).
-export([terminate/3]).
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"}},
receive
{Port,{data,{eol,D}}} -> D
after 20*1000 -> D = "timeout"
end,
Port ! {self(), {command, "0 gertboard\n"}},
port_close(Port),
{ok, Req2} = cowboy_req:reply(200, [], D, Req),
{ok, Req2, State}.
terminate(_Reason, _Req, _State) ->
ok.
cd to ebin and compile the file:
cd ~/cowboy/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:
cd ~/cowboy
mkdir priv
cp ../atlast-1.2/atlast ./priv
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:
( The first 25 getkwh is for detecting an edge, drop the result and find next edge. Convert the two integers to float and divide them. )
( 3.6 divided by the result. The result is load in kilowatts )
: kw 3.6 25 getkwh 2drop 25 getkwh float 2 roll float 2swap f/ f/ ." "kw=" f. cr ;
( The same thing done with integers only, here the result is in W )
: w 25 getkwh 2drop 25 getkwh 36 * swap 100 / / ." "w=" . cr ;
Ok, now we just have to call start.sh again ( 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).
sudo ./start.sh
Point the browser to 192.168.0.178:8080 or whatever ip address you have on your RPi. After a few seconds the W load should show up. I stop here, this is not the right forum 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.
External Interfaces - Pi Camera
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).
%% @doc Hello world handler.
-module(toppage_handler).
-export([init/3]).
-export([handle/2]).
-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
handle(Req, State) ->
Port = open_port({spawn, "/opt/vc/bin/raspistill -t 2 -w 1024 -h 768 -o -"}, [binary]),
Str = loop(Port,<<>>),
{ok, Req2} = cowboy_req:reply(200, [], Str, Req),
{ok, Req2, State}.
terminate(_Reason, _Req, _State) ->
ok.
loop(Port,Frame) ->
receive
{Port, {data, Chunk}} ->
Size = byte_size(Chunk) - 2,
case Chunk of
<<_:Size/binary,255,217>> -> Framestring = base64:encode_to_string(<<Frame/binary,Chunk/binary>>),
"<html><img src = 'data:image/jpeg;base64," ++ Framestring ++ "'></html>";
_ -> loop(Port,<<Frame/binary,Chunk/binary>>)
end
after 20*1000 -> "<html>timeout</html>"
end.
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.