IoT development using Raspberry Pi, Elixir and Nerves
I usually work on web applications, but I started learning about embedded devices development to expand my expertise.
After studying the basics of electronics components, I built a real-time temperature and humidity monitoring system for my living room.
The Nerves IoT platform allows us to build a custom Linux-based firmware easily as well as enabling IoT development using modern programming language Elixir. It is a huge plus for developer experience and device performance.
Here is what I do:
- The target device reads a measurement from the sensor and sends it to the web server every second.
- The web server broadcast a measurement to a real-time monitoring dashboard.
- The host machine can update the firmware over the WIFI anytime.
Host machine (My laptop)
- develop a firmware (Elixir progmamming)
- burn a firmware into a micro SD card (only one time)
- upload a firmware over the wifi
- ssh into the target device over the wifi
Target device (Raspberry Pi Zero W)
- a small general purpose computer
- has WIFI capability
- has Micro SD card slot
Firmware (device-specific software)
- burned into a micro SD card that is inserted into a target device
- contains Linux, Erlang VM, Elixir and my business logic
Sensor (Bosch BME680)
- Environmental sensor
- I2C and SPI are among the most common serial communication protocols for transporting data between devices
- Elixir library: github.com/fhunleth/bmp280
- accepts a sensor measurement from the target device
- broadcasts a new measurement to a dashboard
- Elixir - programming language
- Nerves - platform for developing IoT devices
- Phoenix - web framework
- Phoenix LiveView - enables rich, real-time user experiences with server-rendered HTML
Technically what I am doing is not a traditional embedded system development.
Generally the embedded systems engineers deal with microcontrollers, which are good at doing dedicated tasks efficiently. They do not have an operating system. Typically C/C++ are used for programming.
On the other hand, most Raspberry Pis are a general purpose computer just like my laptop despite being tiny. So I can let it do various tasks out of the box.
I chose the latter because computers is more powerful than microcontrollers and I want to build things quickly taking advantage of modern development tools, using my favorite programming language Elixir.
- functional programming language built on top of Erlang
- particularly built-in support for process supervision and concurrency is great
- Erlang is battle-tested in telecommunication systems since 1980's; recent years, Erlang is used for a number of messaging apps, like whats app, Slack, Discord etc.
- platform for embedded device development
- builds firmware with lightweight Linux + Erlang VM (~20MB)
Difference between IoT development and web development
I am more aware of physics than usual when I am dealing with IoT devices.
- each electronics components comes with a data sheet, which looks like a physics textbook
- data is transmitted as high or low voltage through electric wires
- I've seen smoke coming out of a circuit; probably I made a mistake in wiring
In embedded system programming, we express the high voltage as 1, low voltage as 0. So we need to encode/decode sequence of 0s and 1s frequently. Usually a peripheral has physical addresses called registers we read data from and write data to.
Speaking of bit operations, in many programming languages, bit operations can look complicated despite simply handling 0s and 1s. Thankfully Elixir's pattern matching makes it so easy.
For example, where C++ does something like this,
Elixir can express the same logic in a lot more human-readable ways. Elixir's pattern matching is so powerful.
<<_state::8, h::20, t::20>> = data # 6 bytes (48 bits) that is read from the sensor humiduty_rh = h * 100 / 0x100000 # 15.079402923583984 temperature_c = t * 200 / 0x100000 - 50 # 28.26671600341797
So my IoT development using Raspberry Pi and Elixir has been successful and fun. I just wanted to share it with you.
Thank you for listening!