Distributed programming made easy with Elixir
Getting Records from Mnesia
Unfortunately, Mnesia does not support SQL syntax, but it does support some basic filters like dirty_match_object
(Listing 5).
Listing 5
Getting Records
iex> # Get all the records, use :_ for "all", add a sort at the end iex> :mnesia.dirty_match_object({Pi3, :_ ,:_}) |> Enum.sort [ {Pi3, 0, '0'}, {Pi3, 1, '0'}, {Pi3, 2, '1'}, ... ] iex> # Get only Pi Values that are '1' iex> :mnesia.dirty_match_object({Pi3, :_ ,'1'}) |> Enum.sort [ {Pi3, 2, '1'} ]
A dialog from a zenity --list
command can display table output in columns (Figure 8). The last argument in the command is the data string, which fills in the defined columns. The --extra-button
option returns the button text when the button is pressed.

The script in Listing 6 (Show_gpio.exs
) reads the Mnesia results. As in the earlier example, Enum.map
and Enum.join
functions format the results as one long string for a Zenity list dialog (Figure 9).
Listing 6
Show_gpio.exs
01 #------------------- 02 # Show_Show_gpio.exs - show Mnesia table in a Zenity list dialog 03 #------------------- 04 defmodule Show_gpio do 05 def getdata() do 06 result = :mnesia.dirty_match_object({Pi3, :_ ,:_}) |> Enum.sort 07 # create a presentable string for output 08 output = Enum.map(result, fn x -> "#{elem(x,1)} #{elem(x,2)} " end) |> Enum.join 09 feedback = :os.cmd(:"zenity --list --title=Pi3_Table --text='Pin Values' --extra-button Refresh --column=Pin --column=Value #{output}") 10 if ("#{feedback}" =~ "Refresh") do 11 getdata() 12 end 13 end 14 end 15 # Start Mnesia 16 :mnesia.start() 17 # Wait for tables to update 18 :timer.sleep(1000) 19 # Show a Zenity list dialog. 20 # Refresh button to continue, other buttons to quit 21 Show_gpio.getdata()

To test that things are working, use the first project (Zen2gpio.exs
) to toggle GPIO pin values; then, the Show_gpio.exs
dialog can be refreshed to check the new values.
When the final project is running, you have an example of Elixir concurrency. The Pi3 node is populating a Mnesia table, and it is handling remote GPIO writes and CPU temperature messages.
Final Comments
Elixir with the Erlang VM offers a lot of functionality out of the box. The next steps from here would be to look at messaging between nodes and creating projects with imported Elixir libraries.
Infos
- Elixir: https://elixir-lang.org/
- Erlang: https://www.erlang.org/
- Elixir installation: https://elixir-lang.org/install.html
- GPIO utility: http://wiringpi.com/the-gpio-utility/
- Zenity: https://help.gnome.org/users/zenity/2.32/
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
Red Hat Migrates RHEL from Xorg to Wayland
If you've been wondering when Xorg will finally be a thing of the past, wonder no more, as Red Hat has made it clear.
-
PipeWire 1.0 Officially Released
PipeWire was created to take the place of the oft-troubled PulseAudio and has finally reached the 1.0 status as a major update with plenty of improvements and the usual bug fixes.
-
Rocky Linux 9.3 Available for Download
The latest version of the RHEL alternative is now available and brings back cloud and container images for ppc64le along with plenty of new features and fixes.
-
Ubuntu Budgie Shifts How to Tackle Wayland
Ubuntu Budgie has yet to make the switch to Wayland but with a change in approaches, they're finally on track to making it happen.
-
TUXEDO's New Ultraportable Linux Workstation Released
The TUXEDO Pulse 14 blends portability with power, thanks to the AMD Ryzen 7 7840HS CPU.
-
AlmaLinux Will No Longer Be "Just Another RHEL Clone"
With the release of AlmaLinux 9.3, the distribution will be built entirely from upstream sources.
-
elementary OS 8 Has a Big Surprise in Store
When elementary OS 8 finally arrives, it will not only be based on Ubuntu 24.04 but it will also default to Wayland for better performance and security.
-
OpenELA Releases Enterprise Linux Source Code
With Red Hat restricting the source for RHEL, it was only a matter of time before those who depended on that source struck out on their own.
-
StripedFly Malware Hiding in Plain Sight as a Cryptocurrency Miner
A rather deceptive piece of malware has infected 1 million Windows and Linux hosts since 2017.
-
Experimental Wayland Support Planned for Linux Mint 21.3
As with most Linux distributions, the migration to Wayland is in full force. While some distributions have already made the move, Linux Mint has been a bit slower to do so.