Tuesday, 20 June 2017

First Boot of a Raspberry Pi Zero with Raspbian Jessie Lite

For building a robot, we used a Raspberry Pi Zero with the Raspbian Jessie Lite OS. The robot does not need a graphical desktop running as it is controlled by python scripts, and this combination of low power Pi and low overhead OS should minimise the power needed to be supplied from batteries to run the computer. But it presents some challenges in getting the Pi up and running. So here is what you need to do to get the OS installed, configured and the Pi accessible over a wireless network from another computer. (See the previous article for how to get the Pi Zero set up with the OS installed on the SD card ready to power up for the first time).

On booting up, lots of messages scroll up screen, until eventually the login prompt is displayed.


The default user account on a new install is as follows:
Username: pi
Password: raspberry

This gives you a command prompt, ready to type in commands. The file system had already been expanded to fill card on first boot using the latest Raspbian image, but you can check this for yourself using the 'df' command (type: df  then press ENTER). With my 8GB card it displayed the following information for /dev/root

   7344424 1K blocks. 861480 used. 6154748 available

These numbers are in KB, so the 6154748 available means we have around 6GB of free space. This is about right for an 8GB card, as the OS files take up some of the space, and the 8GB does not result quite that much of usable space since the formatting of the card uses some space to store the directory information and index of files. If you find you have much less space then you can use the raspi-config tool to expand the file system to fill the card. To launch this configuration tool, type:
sudo raspi-config

For full details of this configuration tool, see the official documentation.

The first job after booting up is to get the Pi zero online by configuring the wifi network settings. We will do this using the command line text editor 'nano' to edit the network interfaces file. Open this file in the editor by typing:
sudo nano /etc/network/interfaces

Change the line: auto lo, to auto wlan0
Then change manual to dhcp inside the wlan0 block.

Press Ctrl+x to exit the editor, followed by Y and Enter to save the edits into the existing file.

Now we have told our Pi to connect the WiFi network by default, but we also need to give it the details of which WiFi network to try and connect to. This is done in the wpa_supplicant.conf file which we can edit by typing:


sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
(Tip: press the TAB key after typing part of the folder name or filename and it will autocomplete based on matching folders or files. This saves a lot of typing.)

In the editor, add the following to the end of the file:

network{
  ssid="MyNetworkName"
  psk="wirelesspwd"
  proto=RSN
  key_mgmt=WPA_PSK
  pairwise=CCMP
  auth_alg=OPEN
}

Replace MyNetworkName with the actual name of your WiFi network, and replace wirelesspwd with the password for your WiFi network. Exit and save the file as before (Ctrl+x, then Y and Enter).

The other settings in this network configuration are suitable for most current WiFi routers using the more secure WPA2 encryption. They have worked fine with both my BT Home Hub v5 and v6 routers. But if you have an older router or are using other network encryption settings then you may need to change these too. I found the following article useful if you are looking for further details: http://weworkweplay.com/play/automatically-connect-a-raspberry-pi-to-a-wifi-network/

Once we get our Raspberry Pi connected to our network, we want to connect to it using SSH from another computer. This is handy because unless we have a USB hub we cannot have the WiFi USB dongle plugged in at the same time as the keyboard. The newer Pi Zero W model does not have this problem as it has WiFi built in. But assuming we are using the cheaper Pi Zero here, we will need to make sure that SSH is enabled before we disconnect the keyboard. Since November 2016, the Raspbian OS distributions have SSH disabled by default. It can be enabled using the raspi-config tool, which is launched by typing:
sudo raspi-config

In the config tool, select 'Advanced Options' and then 'SSH'. Choose to enable SSH, then exit the tool.

If everything is ready to go, then we will need to unplug the keyboard from the USB socket in order to plug in the USB WiFi dongle. My dongle did not work when plugged into the USB hub socket on my keyboard. Possibly it would work using a powered hub? Before we unplug the keyboard we need to shut down the Pi. Do this by typing:
sudo shutdown now

Once the Pi is fully shut down (the green LED on the board turns off), unplug the power cord and the keyboard. Then plug the WiFi dongle directly into the Pi USB cable, and reconnect the power. When the Pi Zero boots up again if should automatically connect to the network if we got the wifi configuration right. You should see the IP address it has been assigned displayed on the screen.

e.g. 'My IP address is 192.168.1.109 ....' (followed by some alphanumeric codes)

Sometimes two IP addresses are displayed. I am not sure why, but the first one appears to be the one which works OK.

If the IP address is not displayed then the WiFi network connection likely failed. While the Pi is turned on you can unplug the WiFi dongle from the USB port and plug the keyboard back in so you can login and edit the settings files to try again.

Assuming your Pi is now connected to your network, you should be able to connect to is using SSH. I used the free program WinSCP which you can download from https://winscp.net/

Install WinSCP, and use it to connect to your Pi over SSH using the IP address displayed on boot up. WinSCP is a graphical file explorer tool which enables you to copy files between a Windows computer and a Linux computer (our Pi). You can also use it to edit files stored on the Pi using Windows editors. The command terminal program Putty is included in the install, and you can directly launch this from the Login button too. Or once you are connected, you can launch a terminal session using the Putty tool from a button on the toolbar in WinSCP. Here are the options I used for my Pi (which was using the IP address 192.168.1.109 on my network).


Once you have connected using Putty, you will be prompted to enter the password for the pi user. On first login from putty, the OS warns you if you are using the default password as it is a security risk. Anyone could access your pi as root if they can make an SSH connection over your network. You should change the pi account password to something other than the default of 'raspberry' to secure your pi on the network if you are leaving SSH enabled.


Now you can run commands using putty from a laptop. The screen and keyboard no longer need to be connected to the pi zero, except that without a screen connected we do not get to see what IP address the Pi was allocated on your network when it boots up. Often it will keep the IP address it had the last time, but this is not guaranteed with the DHCP configuration. There are tools you can run from a Windows computer to scan the network looking for devices, or you can change the configuration to use a static IP address so you always know what it is. But you need to make sure it does not clash with any other devices on your network. For now, keep the screen connected when you first boot up so you know which IP address to connect to over SSH.

As this is our first boot up of a new Pi, we should update the packages to the latest stable versions. This is done using the command line apt-get tool. First we need to get the latest package list. Type:
sudo apt-get update

If you are successfully connected to the internet then this will get the latest list of packages. Otherwise it will warn you that if could not access the servers. Just because we are connected to the network does not mean that the network is connected to the internet. So check it did manage to update from the messages displayed.

If you only want to update the packages already on your system to the latest versions, then type:
sudo apt-get upgrade
   
To upgrade the Raspbian distribution to the latest version (which will update all the packages and also remove redundant ones), you type:
sudo apt-get dist-upgrade

This distribution upgrade can take some time (over an hour), so run it when you don't want to use the pi for a while. Keep an eye on it, because you get some information on changes to read once the downloading of new packages completes (my update of a 1 month old Raspbian image after first install took around 2 minutes to download the updates and display the messages which I had to scroll through, then the installation phase of the update started and took another 38 minutes to complete).

Now we have a completely updated OS, we can look at how to get additional packages. You are likely to want to run Python3 scripts, and use the Python GPIO libraries to interface to electronics you connect to your Pi. In the Raspbian Jessie Lite distribution I had, neither of these capabilities were installed already, so here is how to get them.
sudo apt-get -y install python3-rpi.gpio

The package manager will look up the dependencies for the package we are asking to be installed, and automatically install these too. So by just requesting the python3 GPIO package, we will find that Python3 is installed too. Once the install completes, you should be able to type:
python3

The Python 3 REPL environment should launch. You can run python commands directly here. Try typing to following python3 statement at the prompt:
print("Hello Pi World!")

It should display the message in the quotes below the command. You can explore other python3 commands here. When you are done, press CTRL+z to exit REPL and return to the linux shell command prompt. Everything is now ready to start extending your Pi zero and interact with the electronics you connect using Python3 scripts.

No comments:

Post a Comment