In this worksheet you will be shown how to setup and configure a raspberry pi as a central 'hub' for your home automation system. By the end of this you will have:
- The operating system (Debian Stretch Lite) installed on the hub.
- The I2C interface configured for use (and a small display attached).
- A working MQTT broker installed (but not fully secured!).
- The latest versions of Python and Pip.
- A NodeJS runtime environment for running scripts. We will then use this to publish a simple API.
In this worksheet we will be using the Raspbian Buster Lite distro which is a very cut down version of the Debian Server. To complete this step you will need an SD card of at least 8GB since the operating system takes up 4GB and you will need to leave room for your scripts and applications.
Make sure you download the lite version which should be an .img
file of around 426MB. If your download is over 1GB you have downloaded the wrong file!
The download will be an .img
file and needs to be burned onto the SD card. There are several different ways to do this but we recommend you use the Balena Etcher application.
Before we boot for the first time there are a number of settins that need to be changed. These changes are made by pluggin in the SD card into your computer and looking for an external drive called boot
. If you open this you will see a number of config files. It is here that we will need to make our changes.
The first is to allow us to connect remotely into the Raspberry Pi using SSH. By default this is disabled (for security reasons). Create a new text file in the boot
partition and call it ssh
. Make sure your computer has not added a file extension. This file should be left empty.
The second change is to enable the on-board WiFi and connect it to a suitable network. If you are only going to be using the Raspberry Pi at home you can connect to your home WiFi router however if you are planning to use it in different places it would be better to connect to your smartphone hotspot as it is difficult to change the settings and we won't be downloading large files.
Create a new text file called wpa_supplicant.conf
and insert the following, making sure to substitute your WiFi SSID and password. Note that you won't be able to connect to the University Cisco networks requiring a username and password.
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
ssid="YOUR_SSID"
psk="YOUR_WIFI_PASSWORD"
key_mgmt=WPA-PSK
}
Now you can eject the MicroSD card, insert it into your Raspberry Pi and power it up. Note that for this initial boot you will need to have a keyboard and monitor connected. Once you have completed this section you will be able to connect remotely and can run the raspberry Pi headless. Take care inserting the power supply as the USB socket on the board can be quite delicate.
The username is pi
with a default password of raspberry
.
The first task we should do is to update the software installed on the Raspberry Pi and install some additional packages. This is done using the apt
tool.
sudo apt update
sudo apt upgrade -y
sudo apt install -y apt-transport-https rsync sudo nano git tree curl software-properties-common mc build-essential libssl-dev libffi-dev i2c-tools
When you logged in using the pi
user you used a default password which will be the same for all installations unless it is changed. For the sake of security we should change this as soon as possible using the passwd
command. If you run this you will be prompted for the current password then asked to type in your new password twice to ensure you have not miss-typed. Don't forget this new password! Note that when you enter any password text nothing shows on the screen!
$ passwd
Changing password for pi.
Current password:
New password:
Retype new password:
passwd: password updated successfully
it is a good idea to make a note of the current IP address of your Raspberry Pi. You can do this by running the ifconfig
command and looking under the wlan0
section of the report for the inet
label. In the example below the device ip address is 192.168.0.59
:
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.59 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 2a02:c7d:768d:9900:a9f6:6bd9:3c31:8b5f prefixlen 64 scopeid 0x0<global>
Make a careful note of this IP address, you may need it in the next section!
Every device on the network is configured with a host name. If you run the hostname
command you can see that this defaults to minibian
. Since every raspberry pi running the Minibian distro could have the same name this is not very satisfactory!
To change the hostname you need to run the sudo raspi-config
command. This will display a menu containing a number of configuration choices (see below).
You should choose menu option 2 using the down arrow and enter key. On the second screen choose the Hostname
option (first in the list). Read the information on the next screen and press enter to continue where you will be asked to choose a new name. To ensure you have a unique name use your University username entered as lowercase letters and digits. When done press enter to return to the main menu. Press the right arrow to exit.
You will be asked to reboot, opt for the not now option as we need to fully power down the Raspberry Pi.
Once you are back at the prompt do a controlled shutdown using the sudo shutdown now
command. This will power down the computer in a controlled manner.
Up to this point we have connected a monitor and keyboard to our server and used this to interact with the system. Most servers run headless and we connect remotely using secure shell (ssh). To do this we need to know the hostname or the ip address of the system.
Start by disconnecting the power cable, screen and keyboard and then re-insert the power cable, this will boot up the Raspberry Pi in headless mode. You should then pick from either section 4.1 if you want to connect using a MacOS or Linux computer or section 4.2 if you are stuck with Windows 10.
If you are using a Mac or Linux laptop you connect using the ssh command through the terminal, replacing doej
with the username you used as your hostname.
You will be prompted for your password and, the first time you connect, the server will attempt to send you its public encryption key, you will be asked if you want to accept this.
$ ssh pi@doej
The authenticity of host 'aa7401 (192.168.0.59)' can't be established.
ECDSA key fingerprint is SHA256:Yy1dIb646N+j98oJoa+I5Omt7eGfXp7HdLzihUVaEC8.
Are you sure you want to continue connecting (yes/no)?
Warning: Permanently added 'aa7401' (ECDSA) to the list of known hosts.
pi@doej's password:
Linux aa7401 4.19.58-v7+ #1245 SMP Fri Jul 12 17:25:51 BST 2019 armv7l
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Jul 31 17:56:05 2019 from 192.168.0.54
pi@doej:~ $
If you are using Windows 10 you will need to download the Putty application. After launch you need to enter the hostname and user credentials in the appropriate boxes. Once logged in, Putty behaves like a standard terminal window.
If the credentials are correct you are now connected to the server and all the commands you enter will be run on it. To quit and return to your computer enter the exit
command.