Distributed programming made easy with Elixir
Elixir of Life

© Lead Image © Andrejs Pidjass, 123RF.com
The Elixir programming language on a Raspberry Pi lets you create distributed projects in just a few lines of code.
Creating distributed and concurrent applications doesn't have to be difficult. Elixir [1] allows hobbyists and new programmers to create projects that can work across multiple nodes. A general-purpose programming language, Elixer runs on top of the Erlang virtual machine (VM) [2], which is known for running low-latency, distributed, and fault-tolerant systems.
In this article, I look at three projects (Figure 1) that use basic Elixir functions, with no custom project setup or imported libraries. The first project employs remote functions between a PC and a Raspberry Pi, the second project uses multinode requests to get Pi statistics, and the final project looks at dynamic sharing of data between three nodes.
These projects require only 10 to 25 lines of Elixir code, showing that distributed projects don't have to be complicated.
Getting Started
For instructions on how to install Elixer on your particular system, refer to the Elixir website [3]. The process installs the Erlang VM and three new executables: iex
(interactive Elixir shell), elixir
(Elixir script runner), and elixirc
(Elixir compiler).
A good first example is to use the interactive Elixir shell (iex
) on a Raspberry PI to write to a general purpose input/output (GPIO) pin. To begin, open the shell with iex
and call the Raspberry Pi gpio
[4] command-line tool from the base Erlang :os.cmd
function to write a value of 1 to pin 7; then, read it back:
$ iex iex> :os.cmd(:"gpio write 7 1") [] iex> :os.cmd(:"gpio read 7") '1\n'
Elixir calls Erlang functions when you place a colon (:
) in front of the Erlang function or custom variable.
Remote GPIO Control
The next step is to control a Pi's GPIO from a different node. A two-node network can be configured by defining a username with a node address and a common cookie between the two nodes.
For my setup, I logged in to the Raspberry Pi iex
shell with the name pi3@192.168.0.105 and a cookie named pitest (Figure 2, top). Next, I logged in to the PC iex
session with the name pete@192.168.0.120 and the same cookie, pitest.
From my PC iex
session, I only need two lines of Elixir code to write remotely to a Pi GPIO pin (Figure 2, bottom). The first line connects to the Pi Elixir node, and the second line issues a remote procedure call (RPC) to run an :os.cmd
statement on the Pi node:
$ iex --name pete@192.168.0.120 --cookie pitest iex> Node.connect :"pi3@192.168.0.105" true iex> :rpc.call(:"pi3@192.168.0.105",:os,:cmd ,[:"gpio write 7 1"]) []
It's important to note that the underlying Erlang VM on the Raspberry Pi managed the RPC request, and for this example, no Elixir code was required on the Pi node.
User Interface
Now you will want to create a simple way for the user to enter a GPIO pin and value. Elixir tends to be used for back-end applications, but you also have a number of good web server options from which to choose and an Erlang wx
graphical module (a wxWidgets port).
One user interface approach is to use the Elixir IO
module to do text console reads and writes. The IO.gets()
function gets user input, and IO.puts
writes to the console. Variables can be inserted into a string with #{<the_variable>}
:
iex> thepin = IO.gets("Select the Pi GPIO pin: ") Select the Pi GPIO pin: 7 "7\n" iex> IO.puts "Selected GPIO pin: #{thepin}" Selected GPIO pin: 7
For simple dialogs, I like to use the Bash Zenity [5] command-line package. Zenity support a number of different dialog types, and it is preloaded on Raspian and most Linux distributions.
The zenity --forms
command can be configured with a presentation that asks for a GPIO pin number and value. After the user enters data and presses OK, Zenity returns a string of the GPIO pin number and pin value (Figure 3).
The Zen2gpio.exs
script in Listing 1 launches a Zenity form to get user input – a pin number and value – that is passed to the :rpc.call
function to do a remote GPIO write.
Listing 1
Zen2gpio.exs
01 #------------- 02 # Zen2gpio.exs - Use a Zenity form to set the GPIO pin and value input 03 #------------- 04 defmodule Zen2gpio do 05 def show_form (pnode) do 06 thecmd = "zenity --forms --title='Set Pi GPIO Pins' --separator=' ' --add-entry='GPIO Pin (0-26)' --add-entry='New Value (0-1)' " 07 pininfo = :os.cmd(:"#{thecmd}") 08 # If data is entered in form, write to GPIO and refresh 09 if byte_size("pininfo") > 0 do 10 :rpc.call(:"pi3@192.168.0.105",:os,:cmd ,[:"gpio write #{pininfo}"]) 11 show_form (pnode) 12 end 13 end 14 end 15 16 # Connect to the Pi node 17 pnode = :"p34@192.168.0.105" 18 Node.connect pnode 19 20 # Show the dialog 21 Zen2gpio.show_form(pnode)
For this script, the Zen2gpio
module is created with the function show_form
. Elixir does not support a while
statement; instead, loops are implemented by recursion. In this script, the show_form
function is called initially to open a dialog. An if
statement checks for feedback from the dialog; if present, the RPC call is executed, and the show_form
function is called again. No feedback or a press of the Cancel button exits the script.
The Elixir script runner launches the code with the common project cookie and a unique username (Figure 4).
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Find SysAdmin Jobs
News
-
OpenMandriva Lx 23.03 Rolling Release is Now Available
OpenMandriva "ROME" is the latest point update for the rolling release Linux distribution and offers the latest updates for a number of important applications and tools.
-
CarbonOS: A New Linux Distro with a Focus on User Experience
CarbonOS is a brand new, built-from-scratch Linux distribution that uses the Gnome desktop and has a special feature that makes it appealing to all types of users.
-
Kubuntu Focus Announces XE Gen 2 Linux Laptop
Another Kubuntu-based laptop has arrived to be your next ultra-portable powerhouse with a Linux heart.
-
MNT Seeks Financial Backing for New Seven-Inch Linux Laptop
MNT Pocket Reform is a tiny laptop that is modular, upgradable, recyclable, reusable, and ships with Debian Linux.
-
Ubuntu Flatpak Remix Adds Flatpak Support Preinstalled
If you're looking for a version of Ubuntu that includes Flatpak support out of the box, there's one clear option.
-
Gnome 44 Release Candidate Now Available
The Gnome 44 release candidate has officially arrived and adds a few changes into the mix.
-
Flathub Vying to Become the Standard Linux App Store
If the Flathub team has any say in the matter, their product will become the default tool for installing Linux apps in 2023.
-
Debian 12 to Ship with KDE Plasma 5.27
The Debian development team has shifted to the latest version of KDE for their testing branch.
-
Planet Computers Launches ARM-based Linux Desktop PCs
The firm that originally released a line of mobile keyboards has taken a different direction and has developed a new line of out-of-the-box mini Linux desktop computers.
-
Ubuntu No Longer Shipping with Flatpak
In a move that probably won’t come as a shock to many, Ubuntu and all of its official spins will no longer ship with Flatpak installed.