Getting the Raspberry Pi ready for IoT

There are a few things we need to do with the Raspberry Pi single-board computer before using it in IoT projects. Here is a short guide of that I typically do, mostly as a reminder to myself; but it might be of use to others as well :) This article assumes that the Raspberry Pi will be headless, so no GUI.

Contents

The basics

Alright, first we need to download the Raspbian disk image, get the “Lite” version. And write it to the SD card, for that you can use a tool called Win32 Disk Imager.

The Raspbian images no longer have SSH enabled by default, but it’s easy to enable it; simply place a file named ssh (without any extension) onto the boot partition of the SD card after you have written the disk image.

If you are using the Raspberry Pi Zero you need to plugin a monitor and keyboard and get it on the wireless network, just remember that the Raspberry Pi Zero only has mini-HDMI and micro USB. So you will be needing some converters.

After we have gotten access to the terminal, either by using SSH or with a monitor and keyboard we need to run the raspi-config command; there we setup hostname, enable SSH (if not already) and set the timezone. Remember to enable SPI or I2C if your project needs that, I normally just use the GPIO pins and leave them disabled. When done we reboot.

With SSH enabled leaving the default user pi with the default password is somewhat of a security risk, so I like to set up my own user and delete the default pi user:

$ sudo adduser thomas
$ sudo adduser thomas sudo

Log out pi and in as the new user.

$ sudo deluser pi

Now we just need to allow our new user to run sudo without providing a password. First we delete the sudoers config for the user pi since we deleted that user:

$ sudo rm /etc/sudoers.d/010_pi-nopasswd

Then we make a new file for the new user:

$ sudo vim /etc/sudoers.d/thomas-nopasswd

And write the following:

thomas ALL=(ALL) NOPASSWD: ALL

Now to set the permissions for that file:

sudo chmod 440 /etc/sudoers.d/thomas-nopasswd

Done! :)

WiFi

Alright, so for a lot of projects, and especially if using the Raspberry Pi Zero, WiFi is useful. So lets set it up! We will be doing this from the command line since we haven’t installed any GUI. We can search for available networks with this command: sudo iwlist wlan0 scan.

We need to put our network credentials in this file /etc/wpa_supplicant/wpa_supplicant.conf. So we open it in the Nano editor:

$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

And input our network credentials at the end. If your SSID is hidden, you need to include scan_ssid=1 like shown below.

network={
    ssid="yourHiddenSSID"
    scan_ssid=1
    psk="Your_wifi_password"
}

Reconfigure and verify:

$ sudo wpa_cli reconfigure
$ ifconfig wlan0
$ ping google.com

You can find the full documentation here: Raspberry Pi: Setting WiFi up via the command line.

GPIO

Time for the I/O! First we need to install a few packages and add our new user to the gpio group:

$ sudo apt-get install vim python3 python3-rpi.gpio
$ sudo adduser thomas gpio

I like installing Vim because I love Vim, you can use whatever editor you like :)

Next we can run the Python script below, to make sure everything is working. Connect an LED to GPIO5:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)  # set board mode to Broadcom

GPIO.setup(5, GPIO.OUT)

while True:
   GPIO.output(5, 1)  # turn on pin 5
   time.sleep(1)      # wait 1 second
   GPIO.output(5, 0)  # turn off pin 5
   time.sleep(1)      # wait 1 second

Your LED should now flash!

Python

Python is the script of choice for the Raspberry Pi, and to make it easy to install new modules we are going to install Python Package Index, or pip.

$ sudo apt-get install python3-pip

This installs pip for Python version 3, which I prefer to use whenever I can. Now we can get new modules with a simple command:

$ sudo pip3 install paho-mqtt

This will install the paho-mqtt package; which comes in very handy if you have an MQTT broker set up. If you don’t, you should, it’s awesome!

Supervisor

To make sure that our Python scripts are running I like to use supervisor, so we need to install it and make a few configuration changes:

$ sudo apt-get install supervisor

$ sudo vim /etc/supervisor/supervisord.conf

# add/modify this
chmod=0770
chown=root:thomas

These configuration changes allows our newly created user to run the supervisorctl command which can start and stop processes.

So now we need to define a process:

$ sudo vim /etc/supervisor/conf.d/my-proccess.conf

# add this
[program:my-proccess]
command=python3 ./script-name.py
directory=/home/thomas/python/script
autostart=true
autorestart=true
user=thomas

And restart supervisor sudo service supervisor restart.

This process will now auto start when the Raspberry Pi is booted, and it will restart if the process crashes. Whoho! To start, stop or restart it use the supervisorctl command.

If you have anything to add or ask; you can reach me on twitter or send me an email.