
Nerves – Elixir Platform for IoT - rurban
https://www.nerves-project.org/
======
Ndymium
If you're interested in seeing a (toy) example project in Nerves, I
implemented my own version of an old Finnish reaction speed game [0]. It runs
on an RPi0W that I had lying around and has a live score display over WiFi
(using Phoenix LiveView [1]). I've still yet to implement a memory game mode,
currently it only has the classic speed game (and pro version where you start
at score 100, with the corresponding speed).

You can find the source on GitLab [2] with the interesting part being in the
lib folder. The game folder there contains the game part, while web contains
the score UI. It's somewhat messy as I was learning both Nerves and LiveView
and just trying out different things.

The hardware is still in prototype stage and the LED lights aren't even inside
the buttons. I have to say, for me the implementation of the game logic in
Elixir was much easier than planning and soldering the electronics! [3]

[0]
[https://www.youtube.com/watch?v=C8gbcYwmpvU](https://www.youtube.com/watch?v=C8gbcYwmpvU)

[1]
[https://github.com/phoenixframework/phoenix_live_view](https://github.com/phoenixframework/phoenix_live_view)

[2]
[https://gitlab.com/Nicd/ex_speed_game/](https://gitlab.com/Nicd/ex_speed_game/)

[3]
[https://drive.google.com/file/d/1zc_HlcMH3QfayqxRqWYN9M-duCN...](https://drive.google.com/file/d/1zc_HlcMH3QfayqxRqWYN9M-duCNgPm1_/view?usp=sharing)

~~~
waihtis
+1 nostalgia points for selecting that game.

FYI, [3] doesn't work (access denied)

~~~
Ndymium
Sorry about that, GDrive's permissions UI sometimes confuses me. Should work
now!

~~~
waihtis
Works, and no worries - that's a common problem with GDrive..

------
onebot
Although pretty nice if you have a Linux host, I would say this isn't well
suited to typical embedded IoT devices that would be used in large scale
deployments. It would be cost prohibitive to blanket a city with temperature
sensors if they all needed to use a $35 raspberry pi.

Many IoT devices run on very limited hardware mostly without an MMU, usually
shared with the Radio like the Nordic NRF52 or Semtech's Lora modules. Often
times with serious size, power, and cost constraints. This often rules out
higher level languages like Erlang, Go, etc. These are great languages for
different use cases, but really not well suited to any low level hardware. The
runtime language has huge impact on cost and battery performance. More memory
== more $$, more CPU cycles == more power.

~~~
baybal2
Yep, 10 out of 10 of those "IoT frameworks" unequivocally qualify as epic
fails.

As of recently, I had to work on Microsoft Azur IOT. Their "embedded" library
doesn't even load into memory if you are not plugging in an external ram on
the most beefy MCU on the market. It takes 1mb+ to do a very, very basic
HTTP+TLS+JSON parsing. And even this way, it often triggers watchdog on
moderately long transfers.

\-----

On a sidenote, had a conversation one time with an ex-webdev in the setting of
developing for MCUs:

— "How did you get it to 1MB?"

— "I shortened the strings, and removed the dead code!"

— "No, I mean, how did you manage to get it past that 1MB?"

\-----

It's quite hard finding experienced non-webdevs in general these days, but
among those, the number of people who can do C for embedded (and under
embedded I mean bare metal, not Linux on SBC) is so low that we can say that
real embedded devs have pretty much went extinct.

~~~
ncrmro
Wrote some stuff for esp32 in c. Am I an embedded dev yet?

~~~
mafro
Yes

------
zorbash
I used Nerves for a hobby project. A "smart" recycling bin which uses a camera
to recognise recycling labels and open the lid of the appropriate bin. It was
so much fun building it! Hooking a phoenix + LiveView backend was painless and
straightforward.

I don't care that it's not the most efficient framework or not suited for
industrial stuff, it does drive my creativity and brings me joy.

~~~
kiliancs
Would love to see how you built this and maybe do it myself, if you are open
to writing about it or open sourcing the code.

------
heisenzombie
I hooked up a Raspberry Pi to some dev boards from Analog Devices to make a
test rig with some analog and digital outputs and high precision ADCs. It was
a lovely experience. Supervised Elixir genservers are a wonderful abstraction
to wrap around hardware devices. Also, binary pattern-matching was a _dreamy_
way to work with registers over SPI. A Phoenix LiveView user interface on top
was performant and convenient.

Would recommend.

------
lultimouomo
It took me a while to find that this targets linux boards (Raspberry Pi,
Beaglebones), not the smaller embedded platforms that are often used in IoT.
It may be more suited for industrial projects rather than consumer ones.
Interesting nonetheless.

~~~
d33lio
BBB is killer and far superior to the Raspberry Pi IMO - if only we could hook
a nerves compatible NIF into the BBB's real-time CPU...

Also... Rust based NIF's (bits of code in other languages that can be wrapped
as modules in elixir) are insanely cool!

~~~
connorrigby
There have been a few projects that use the PRUs with Nerves. It's pretty
trivial to get setup. We've been trying to come up with a way to package an
Elixir project to get started.

~~~
d33lio
Please link me to some example code or articles! So excited to hear this is a
thing, my absolute dream is to implement a PID control loop for a PRU in
nerves.

~~~
connorrigby
[https://github.com/fhunleth/troodon-cam](https://github.com/fhunleth/troodon-
cam)

This is somewhat old now, but the PRU code which i find to be the most
important part is still good.

~~~
d33lio
Thank you!

Nerves I still love you!

------
kuzee
I've tried to build several Pi projects over the years, and I'll happily plug
Nerves as probably the easiest way to develop software that runs on the Pi,
especially for the reliability. Genservers supervising sensor inputs and
managing motor outputs is really simple to reason about. The easy learning
curve is nice given how robust Nerves is under the hood. I've worked harder
for less reliability.

Nerves is also the first Pi software that worked the very first time I plugged
in the flashed memory card, which was a really good start.

I'm currently adding Nerves/Pi to a small home autonomous robot and it's been
very easy for a guy who definitely isn't a hardware expert. I'm looking for
other fun ideas, if anyone has a good idea. I live in an apartment, so indoors
ideas only for now.

------
comboy
I've been looking at it many times, but I don't want to add myself work. Say I
want to use RF24 (nrf24l01), if I just use raspbian I know it's an environment
it's been tested on heavily, that all sensors libraries should just work
because everybody is using the same hardware and software (in case of
raspberry pi) and if it doesn't, I'm doing something wrong. But with heavily
modified environment I don't want to spend 2 days debugging why i2c sometimes
doesn't work or something like that. Am I understanding it wrong? Does anybody
has some experiences to share?

To clarify I'm doing hobby projects, not industrial applications, but I do
expect industrial-level reliability from my home automation.

~~~
lawik
If you want the reliability you open yourself to the eventuality that you
might have to put in some of the work to achieve it. Things like SPI, I2C and
GPIO can be expected to work with Nerves + Elixir Circuits. But some hobby
hardware is definitely not straight forward since they tend to have libraries
only in Python. I've ported one such myself (Inky eInk display) and many
others have contributed similarly.

Nerves is good for hobbyists that want a better workflow and more serious and
reliable setup. But it focuses on solving for production devices of you
compare it to Raspbian.

------
nslocum
Nerves looks very similar to Balena [1] which is definitely worth checking out
if you want to manage a fleet of micoro-computers. Balena also uses light-
weight docker containers which simplifies things nicely for developers who are
more comfortable working higher up the stack.

[1] [https://www.balena.io/](https://www.balena.io/)

------
d33lio
Nerves is cool, until you realize it's basically all just ported C code that
you're trusting to not crash the whole erlang VM. I say this as someone who
does erlang professionally. It's effectively one huge NIF.

I'm still waiting for a nerves compatible PID control module!

~~~
connorrigby
Hello, Nerves Core Team member here. Unfortunately, most of this just isn't
true and i'd like to address some of your concerns to prevent incorrect
information from being spread.

> until you realize it's basically all just ported C code that you're trusting
> to not crash the whole erlang VM.

We rely on very little C code at runtime. The networking libraries have a tiny
bit of C glue code to talk to Linux, but other than that we use mostly Elixir.
I've been shipping Nerves in production for 4+ years now and "trusting C code
not to crash" has never been a problem for me or anyone else I know using
Nerves in production.

> It's effectively one huge NIF.

We actually don't have _any_ NIFs in the core of Nerves. You would have to
bring them in yourself, which I expect would be caught in the dependency
screening process of your project. Any time we need C to access something in
Linux, we usually reach for Ports since they can be properly supervised.

> I'm still waiting for a nerves compatible PID control module!

there is at least one PID library in Elixir that I know several people have
used, and at least one company uses in production.

I wish you had done a bit more research before writing this message, but I
hope I've addressed any concerns for future viewers.

~~~
d33lio
Hi, I apologize for the rather inflammatory nature of my post. I actually
built a brewing controller back in the day with Nerves and it's still kicking
today! At that point I was much more of an elixir noob and was referring back
to my knowledge of nerves at the time.

Thank you for educating me and informing me that I was wrong ;). We need more
people like you who know how to tell others they're wrong while doing so in a
cordial and friendly matter.

cheers!

------
syspec
When I visit a site such as this (with 'fade up and in all of the content'
animations), the first thing I do - is quickly scroll to the bottom. Then I
scroll back to the top, so I can read it.

------
polack
Why would anyone use this? As I understand it's not even supported to do hot
code reloads on the target when developing?

Is it really only a way do package the Elixir app with a Linux distro and a
template config that includes a couple of hex dependencies?

~~~
connorrigby
You can certainly do hot code reloads. I use this constantly when developing
new projects. I even wrote a tutorial for it [here]([https://embedded-
elixir.com/post/2018-12-10-using-distributi...](https://embedded-
elixir.com/post/2018-12-10-using-distribution-to-test-hardware/))

~~~
polack
Your homemade solution to do hot code reload have nothing to do with Nerves
though, it's just plain Erling/Elixir. So again, what do I gain from using
Nerves?

~~~
connorrigby
That's the beauty of Nerves in my opinion. We aren't trying to be a web
framework. If that's what you want, bring it yourself. Nerves is here as a
tooling for deploying your application to hardware platforms. It takes care of
all the stuff that most people don't want to deal with. This means that aside
from learning some basic mix tasks (`mix firmware` to assemble your firmware
and `mix burn` to write it to an SD Card), you don't need to "learn Nerves"
only "learn Elixir".

