I’m preparing to upgrade my old humidity/temperature meeeeeter solution with raspberry pi. While it is easy to read stuff in C i don’t want to build whole app in C because i know how this can end when you are 1500 – 3000 km from home and app stops to respond and the only living person to fix it is your Cat Haskell. So i want to have everything in Erlang
and only small module reading stuff in C.
Ports
Firt thought was to build small C program that will check stuff periodically or just “check stuff” in SHT1x and just print it out to output. So my first attempt was
For the example here i will use /proc/cpuinfo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Nothing super special, except that raspberry pi is not really cool with different packet sizes and it can for example not read whole input properly. I observed some issues with it. So i decided to explore more FFI.
erl_interface
The thing i found is called erl_interface and it is designed for FFI. This is it! What you do is you build process like thing in C and micro module in erlang that handles this. (It is just to make it look nice). But there are few glitches!
This is my module posting back “pong” on “ping” message
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
First of all complexity goes p fast. You have to think about many things. Here i know that my host is called “emu@raspberrypi” and this is actually process that runs so if you will not remember about freeing memory you will fast learn what means “memory leak”.
But most important thing is how to build this and run.
Building
My make file looks like this
1 2 |
|
It is important to remember about -pthread.
Running everything.
No how to make everything work… we need module
1 2 3 4 5 6 7 8 |
|
` This c1 is for c extension 1 not super obvious :) cN will be for c extension N =).
And finally we need to spawn our node…
1
|
|
This spawns node named emu@raspberrypi on my mini box with cookie “cookie” now … i said finally… but it was not final step.
Final step is to run out lolpong
binary. It is important to run it after node is up because it will try to connect to this node.
1
|
|
Now we can run in our erlang shell check if everything works!
1 2 |
|
Works!
Summary
It is good fun, now i need to wait for the parts to assemble everything and build rest of application :)
Cheers!!