Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - How to turn on an LED with Fedora IoT

#1
How to turn on an LED with Fedora IoT

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/09/how-to-turn-on-an-led-with-fedora-iot.png" width="1024" height="768" title="" alt="" /></div><div><p>Do you enjoy running Fedora, containers, and have a Raspberry Pi? What about using all three together to play with LEDs? This article introduces Fedora IoT and shows you how to install a preview image on a Raspberry Pi. You’ll also learn how to interact with GPIO in order to light up an LED.</p>
<p><span id="more-22492"></span></p>
<h2>What is Fedora IoT?</h2>
<p>Fedora IoT is one of the current Fedora Project objectives, with a plan to become a full Fedora Edition. The result will be a system that runs on ARM (aarch64 only at the moment) devices such as the Raspberry Pi, as well as on the x86_64 architecture.</p>
<p><img class="aligncenter wp-image-22495 size-large" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/09/how-to-turn-on-an-led-with-fedora-iot.png" alt="" width="616" height="462" /></p>
<p>Fedora IoT is based on OSTree, like <a href="https://teamsilverblue.org/">Fedora Silverblue</a> and the former <a href="https://www.projectatomic.io/">Atomic Host</a>.</p>
<h2>Download and install Fedora IoT</h2>
<p>The official Fedora IoT images are coming with the Fedora 29 release. However, in the meantime you can download a <a href="https://kojipkgs.fedoraproject.org/compose/iot/latest-Fedora-IoT-28/compose/IoT/">Fedora 28-based image</a> for this experiment.</p>
<p>You have two options to install the system: either flash the SD card using a dd command, or use a <em>fedora-arm-installer</em> tool. The Fedora Wiki offers more information about <a href="https://fedoraproject.org/wiki/InternetOfThings/GettingStarted#Setting_up_a_Physical_Device">setting up a physical device</a> for IoT. Also, remember that you might need to resize the third partition.</p>
<p>Once you insert the SD card into the device, you’ll need to complete the installation by creating a user. This step requires either a serial connection, or a HDMI display with a keyboard to interact with the device.</p>
<p>When the system is installed and ready, the next step is to configure a network connection. Log in to the system with the user you have just created choose one of the following options:</p>
<ul>
<li>If you need to configure your network manually, run a command similar to the following. Remember to use the right addresses for your network:
<pre>$ <strong>nmcli connection add con-name cable ipv4.addresses \ 192.168.0.10/24 ipv4.gateway 192.168.0.1 \ connection.autoconnect true ipv4.dns "8.8.8.8,1.1.1.1" \ type ethernet ifname eth0 ipv4.method manual</strong></pre>
</li>
<li>If there’s a DHCP service on your network, run a command like this:
<pre>$ <strong>nmcli con add type ethernet con-name cable ifname eth0</strong></pre>
</li>
</ul>
<h2><b>The GPIO interface in Fedora</b></h2>
<p>Many tutorials about GPIO on Linux focus on a legacy GPIO sysfis interface. This interface is deprecated, and the upstream Linux kernel community plan to remove it completely, due to security and other issues.</p>
<p>The Fedora kernel is already compiled without this legacy interface, so there’s no <em>/sys/class/gpio</em> on the system. This tutorial uses a new character device <em>/dev/gpiochipN</em> provided by the upstream kernel. This is the current way of interacting with GPIO.</p>
<p>To interact with this new device, you need to use a library and a set of command line interface tools. The common command line tools such as <em>echo</em> or <em>cat</em> won’t work with this device.</p>
<p>You can install the CLI tools by installing the <em>libgpiod-utils</em> package. A corresponding Python library is provided by the <em>python3-libgpiod</em> package.</p>
<h2><b>Creating a container with Podman</b></h2>
<p><a href="https://github.com/containers/libpod">Podman</a> is a container runtime with a command line interface similar to Docker. The big advantage of Podman is it doesn’t run any daemon in the background. That’s especially useful for devices with limited resources. Podman also allows you to start containerized services with systemd unit files. Plus, it has many additional features.</p>
<p>We’ll create a container in these two steps:</p>
<ol>
<li>Create a layered image containing the required packages.</li>
<li>Create a new container starting from our image.</li>
</ol>
<p>First, create a file <em>Dockerfile</em> with the content below. This tells podman to build an image based on the latest Fedora image available in the registry. Then it updates the system inside and installs some packages:</p>
<pre>FROM fedora:latest RUN  dnf -y update RUN  dnf -y install libgpiod-utils python3-libgpiod</pre>
<p>You have created a build recipe of a container image based on the latest Fedora with updates, plus packages to interact with GPIO.</p>
<p>Now, run the following command to build your base image:</p>
<pre>$ <strong>sudo podman build --tag fedora:gpiobase -f ./Dockerfile</strong></pre>
<p>You have just created your custom image with all the bits in place. You can play with this base container images as many times as you want without installing the packages every time you run it.</p>
<h2>Working with Podman</h2>
<p>To verify the image is present, run the following command:</p>
<pre>$ <strong>sudo podman images </strong>REPOSITORY                 TAG        IMAGE ID       CREATED          SIZE localhost/fedora           gpiobase   67a2b2b93b4b   10 minutes ago  488MB docker.io/library/fedora   latest     c18042d7fac6   2 days ago     300MB</pre>
<p>Now, start the container and do some actual experiments. Containers are normally isolated and don’t have an access to the host system, including the GPIO interface. Therefore, you need to mount it inside while starting the container. To do this, use the <em>–device</em> option in the following command:</p>
<pre>$ <strong>sudo podman run -it --name gpioexperiment --device=/dev/gpiochip0 localhost/fedora:gpiobase /bin/bash</strong></pre>
<p>You are now inside the running container. Before you move on, here are some more container commands. For now, exit the container by typing <em>exit</em> or pressing <strong>Ctrl+D</strong>.</p>
<p>To list the the existing containers, including those not currently running, such as the one you just created, run:</p>
<pre>$ <strong>sudo podman container ls -a</strong> CONTAINER ID   IMAGE             COMMAND     CREATED          STATUS                              PORTS   NAMES 64e661d5d4e8   localhost/fedora:gpiobase   /bin/bash 37 seconds ago Exited (0) Less than a second ago           gpioexperiment</pre>
<p>To create a new container, run this command:</p>
<pre>$ <strong>sudo podman run -it --name newexperiment --device=/dev/gpiochip0 localhost/fedora:gpiobase /bin/bash</strong></pre>
<p>Delete it with the following command:</p>
<pre>$ <strong>sudo podman rm newexperiment</strong></pre>
<h2><b>Turn on an LED</b></h2>
<p>Now you can use the container you already created. If you exited from the container, start it again with this command:</p>
<pre>$ <strong>sudo podman start -ia gpioexperiment</strong></pre>
<p>As already discussed, you can use the CLI tools provided by the <em>libgpiod-utils</em> package in Fedora. To list the available GPIO chips, run:</p>
<pre>$ <strong>gpiodetect </strong>gpiochip0 [pinctrl-bcm2835] (54 lines)</pre>
<p>To get the list of the lines exposed by a specific chip, run:</p>
<pre>$ <strong>gpioinfo gpiochip0</strong></pre>
<p>Notice there’s no correlation between the number of physical pins and the number of lines printed by the previous command. What’s important is the BCM number, as shown on <a href="https://pinout.xyz/">pinout.xyz</a>. It is not advised to play with the lines that don’t have a corresponding BCM number.</p>
<p>Now, connect an LED to the physical pin 40, that is BCM 21. Remember: the shorter leg of the LED (the negative leg, called the cathode) must be connected to a GND pin of the Raspberry Pi with a 330 ohm resistor, and the long leg (the anode) to the physical pin 40.</p>
<p>To turn the LED on, run the following command. It will stay on until you press <strong>Ctrl+C</strong>:</p>
<pre>$ <strong>gpioset --mode=wait gpiochip0 21=1</strong></pre>
<p>To light it up for a certain period of time, add the <em>-b</em> (run in the background) and <em>-s NUM</em> (how many seconds) parameters, as shown below. For example, to light the LED for 5 seconds, run:</p>
<pre>$ <strong>gpioset -b -s 5 --mode=time gpiochip0 21=1</strong></pre>
<p>Another useful command is <em>gpioget</em>. It gets the status of a pin (high or low), and can be useful to detect buttons and switches.</p>
<p><img class="aligncenter wp-image-22496 size-large" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/09/how-to-turn-on-an-led-with-fedora-iot-1.png" alt="Closeup of LED connection with GPIO" width="616" height="462" /></p>
<h2><b>Conclusion</b></h2>
<p>You can also play with LEDs using Python — <a href="https://github.com/brgl/libgpiod/tree/master/bindings/python/examples">there are some examples here</a>. And you can also use the i2c devices inside the container as well. In addition, Podman is not strictly related to this Fedora edition. You can install it on any existing Fedora Edition, or try it on the two new OSTree-based systems in Fedora: <a href="https://teamsilverblue.org/">Fedora Silverblue</a> and <a href="https://coreos.fedoraproject.org/">Fedora CoreOS</a>.</p>
</div>
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016