To talk about Elixir, we first have to mention Erlang. Many people consider Erlang to be a difficult language with a plethora of innovative features. Erlang is built for massive-scalable, real-time systems with requirements for high availability. It was developed by Ericson to handle millions of connections for digital switches in the telecommunication industry.
Elixir is also a dynamic and elegant language, with a Ruby-like syntax that leverages Erlang VM. It embraces functional programming which gives you access to modern features such as Pattern Matching, Pipe Operator, and Immutable Data to name a few. And the real advantage of Elixir is its strong community – building apps from Embedded to SaaS platforms.
There are many boards used for rapid prototyping – with Arduino being one of the most popular ones. It’s a microcontroller that does real-time processing on a loop, usually programmed in C / C++, and without an operating system. I consider it to represent a great jumpstart into the embedded world, as it’s very simple to get going with just a few lines of code. Simultaneously, I find it easy to hit its limitations when it comes to IoT. Here there are 2 key factors to consider: connectivity and security. If you take a look at Linux-based microcontrollers, you’ll find Raspberry Pi to be considerably more powerful, most likely running a debian OS. On this device, you have access to better libs for security and connectivity and all high level programing languages many developers are used to. But, this comes at a cost as it’s pretty unpleasant to build and distribute apps for this platform.
To take advantage of both worlds, Elixir manages to ease the build and distribution process for Linux-based microcontrollers with Nerves. This is a lightweight and easy to get started framework that allows you to bundle your whole application with Linux, Erlang VM and your code into a firmware. It has support for the most popular Linux based boards like Raspberry Pi or Beaglebone.
To better present Elixir in the embedded world, let’s have a look at a simple application, an automatic water system for plants. For this build, I’ve used the following components:
Wiring Moisture sensor
Wiring Water Pump
- RaspberryPi and a microSD card
- Moisture sensor and MCP3008 chip
- Water pump with a Relay
- Transistor 2N2222, Resistor 1k, Resistor 10k
- Wires and prototyping board
Everyone who’s passionate about electronics or building things with their own hands will find this pretty satisfying once it gets it to work. Let’s put together some hardware components:
With minimal effort I’ve wired everything I need on the device. Now I’ll push some code to the device. The source code for the embedded device can be found here together with all build instructions.
To interact with the device, I built a chatbot app, using a framework, written in Elixir, called Phoenix. This is a web framework that brings simplicity and joy in writing modern apps. It encourages you to write clean and maintainable code, and most importantly, it performs on both small and large applications because it borrows the concurrency model from Erlang which allows to have millions of connections over a distributed network.
The communication between device and web app can be done using various protocols, HTTP, MQTT and WebSockets being one of the most popular. Because I want to ensure real-time communication (and to simplify this demo) I’ve only implemented MQTT and WebSockets.
Application infrastructure diagram
Find the code and instruction for the chatbot app here.
Let’s see all this in action:
There is a lot of fear around hardware, but it’s so entertaining! Taking the time to learn something new is so enlightening and it’s marvelous to interact with the world around you. All these sensors have so much tech embedded in them, with functions that detect light, temperature or moisture and simply responding with a number, most of the time with an on and off function.
Go out there, find the project you always wanted to build and give Elixir a try! I promise you will learn a lot in the process.
Links to presented app source code:
About this presentation
On September 2019, Cognizant Softvision celebrated the 5th edition of Programmers’ Week, a global event where more than 140 speakers delivered technical-talks from Argentina, US, Canada, India, Ukraine and Romania. Lucian Leustean, who is based in Bucharest, delivered a presentation about “Year of Elixir – why IoT and Elixir will take over the world” at the event.