An intro to Mender — Part 1

Oliver K. Ernst, Ph.D.
Practical coding
Published in
5 min readJul 11, 2020

The modern way to update IoT devices.

Image by Hans Johnson — source and license.

Mender makes over-the-air (OTA) software and firmware updates easy. Really easy. It’s just not-so-easy to get started.

This tutorial is the first part in a multi-part series on setting up OTA updates for your IoT device using Mender:

  • [This part] Setting up a golden device for Mender. This entails flashing an SD card and getting started with Raspberry Pi OS (headless, i.e. without monitor/keyboard).
  • Part 2 link - Setting up a Mender server on Google cloud platform (GCP).
  • [TBD] Setting up a device that can be deployed in the field.
  • [TBD] Making an image from your current golden device and sending the update to a deployed device.

Sorry for the missing parts for now — I got bored of writing today. Check back soon for the other parts! Or drop me a line if I’m procrastinating too much….

We will be using a Raspberry Pi Zero WiFi running Raspberry Pi OS (32-bit) Lite (Raspbian? When did they rename it?!).

There is some overlap between this article and the setup in another article on getting a headless setup of Raspberry Pi — here we focus on the necessary steps to get Mender up and running.

The magic of Mender for OTA updates

The robots of Laputa, running Mender, obviously. Image by Su-May. Image source and license.

This is the boilerplate text for why OTA updates are so magical with Mender. But if you already knew that, just go ahead and skip on to the next part.

Mender makes IoT device updates easy. There are two types of updates, both of which can be done with Mender:

  • Firmware updates — this updates the entire OS.
  • Software updates — this updates just some software on your device, e.g. like a Docker container.

In this tutorial I’m gonna focus on the first one — maybe a later one will discuss software updates.

Both firmware and software updates are done by what are called Mender artifacts. For firmware updates, artifacts can be generated from the image of a “golden device”. The way this works is:

  1. You create a golden device. The golden device will run all of your firmware and software that is needed to make your product go. It will not contain any Mender software.
  2. You copy over the image of your golden device, and run it through a helpful convertor to generate a ready-to-go Mender artifact.
  3. You provision devices that will be deployed to the field with your artifact.
  4. You can see your devices in your hosted Mender server (hosted by you for free, or here for a fee over Google cloud platform (GCP), or otherwise hosted for a fee on Mender’s platform).
  5. To send firmware updates to devices in the field, simply repeat step (2) and deploy the update by uploading the new artifact to your server.

Easy as that!

Let’s get start creating a golden device. I’ll show you how to set up the Raspberry Pi Zero WiFi without a keyboard or monitor, and how to enable some fancy features that your product may need.

I’ve marked every section with one of:

required
recommended
optional

and you can pick what parts you like.

Flash the SD card (required)

Get the latest Raspberry Pi OS Lite image here. I used:

Raspberry Pi OS (32-bit) Lite
Minimal image based on Debian Buster
Version: May 2020
Release date: 2020–05–27
Kernel version: 4.19
Size: 432 MB

Get balenaEtcher here and flash an SD card with the image.

Headless setup of RPi (required)

Image by Hans Johnson — source and license.

We will now setup your Pi without a monitor or keyboard.

You can find more details and options in this article. Here we will only review the necessary steps to get Mender going later.

Mount the SD card on your computer, and navigate to the boot partition.

Then we can set up the Pi as follows.

WiFi (required)

You can edit WiFi by making a file called `wpa_supplicant.conf` with contents:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
network={
ssid=”YOURSSID”
psk=”YOURPASSWORD”
}

NOTE: this didn’t work for me. I had to set it up later by editing sudo nano /etc/wpa_supplicant/wpa_supplicant.conf directly as described below. But you can connect to pi using SSH over USB without setting up networking to begin.

NOTE NOTE: The correct way to restart the WiFi is:

sudo wpa_cli -i wlan0 reconfigure

SSH (required)

To enable ssh, make empty file simply titled ssh (no extension) in the boot partition. You can use the command touch ssh to do this.

SSH over port USB (required)

To enable USB connection, add to the bottom of `config.txt`:

# Enable USB OTG like ethernet
dtoverlay=dwc2

and to the end of cmdline.txt after rootwait such that it reads:

… rootwait modules-load=dwc2,g_ether

Make sure you connect the USB cable to the USB port — not the power port on the Raspberry Pi Zero WiFi!

If ssh still does not work later (ugh!) you will have to use a keyboard to enter to pi and run:

sudo systemctl enable ssh.service

which should do it.

Connecting to your device (required)

Now you can connect — find the address:

ping raspberrypi.local

which should give e.g. 169.254.29.31, then:

ssh pi@169.254.29.31
password: raspberry

Setup important features (required)

Image by Yeung Ming — source and license.

WiFi (required)

  • Check WiFi connects:
ping 8.8.8.8
  • If not, edit sudo nano /etc/wpa_supplicant/wpa_supplicant.conf:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
network={
ssid=”YOURSSID”
psk=”YOURPASSWORD”
}
sudo wpa_cli -i wlan0 reconfigure

New user (recommended)

  • Make a new user called joehisashi with password abc123:
sudo adduser joehisashi
password: abc123
  • Add to sudo:
sudo adduser joehisashi sudo
  • Now log out and back in as joehisashi.
  • Delete the pi user
sudo userdel -r pi
  • We can add ourselves to sudo so we don’t need sudo for a command like some_command:
joehisashi ALL=(ALL) NOPASSWD: some_command

at the bottom.

Change hostname (recommended)

  • Change the hostname so instead of ping raspberrypi.local we can ping ghibli.local .
  • Edit:
sudo nano /etc/hostname

such that it reads simply

ghibli
  • This requires a restart to work:
sudo reboot -h now

Now we can ping the device with simply

ping ghibli.local

Final thoughts

That’s it! It should be enough to get your “golden” Raspberry Pi going for your project. In the next part, we will work on setting up the Mender server.

--

--