Book Image

Getting Started with Raspberry Pi Zero

By : Richard Grimmett
Book Image

Getting Started with Raspberry Pi Zero

By: Richard Grimmett

Overview of this book

Raspberry Pi Zero is half the size of Raspberry Pi A, only with twice the utility. At just three centimeters wide, it packs in every utility required for full-fledged computing tasks. This practical tutorial will help you quickly get up and running with Raspberry Pi Zero to control hardware and software and write simple programs and games. You will learn to build creative programs and exciting games with little or no programming experience. We cover all the features of Raspberry Pi Zero as you discover how to configure software and hardware, and control external devices. You will find out how to navigate your way in Raspbian, write simple Python scripts, and create simple DIY programs.
Table of Contents (16 chapters)
Getting Started with Raspberry Pi Zero
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Adding Internet access


The Raspberry Pi Zero does not have a LAN connection. To connect the Raspberry Pi Zero to the Internet, you have two choices. You can establish a wireless LAN connection or you can connect by using a USB to LAN adapter if you want to connect to an actual LAN port. Let's look at both of these possibilities.

If you are going to connect wirelessly, make sure that you have a wireless access point available. You'll also need a wireless device. The official Raspberry Pi Foundation markets a device itself, but other brands also work. See http://elinux.org/RPi_USB_Wi-Fi_Adapters to identify the wireless devices that have been verified to work with the Raspberry Pi Zero. Here is one that is available at many online electronics outlets:

You'll also need to connect a powered USB hub for this process, so that you can access both the keyboard and mouse, as well as the USB wireless LAN device. Now, connect the device to the powered hub.

Boot the system and then edit the wpa_supplicant.conf file by typing sudo nano /etc/wpa_supplicant/wpa_supplicant.conf. You need to change it to look like this:

The wpa-ssid and wpa-psk values here must, of course, match what your wireless access point requires. Reboot and your device should be connected to your wireless network. You'll know if it is connected by looking in the upper right hand corner of the screen where you should see the following:

You can now download any additional functionality you'll want to install from the Internet.

If you want to connect to an actual LAN cabled connection you need a device that goes from USB to LAN. This site http://elinux.org/RPi_USB_Ethernet_adapters lists a number of different possibilities. Here is an image of one such device:

Connecting the Raspberry Pi Zero in this way is actually amazingly easy. Simply plug the USB to LAN adapter into the powered USB hub, connect an active LAN cable and you should then have Internet access.

Accessing your Raspberry Pi Zero from your host PC

Once you have established an Internet network connection with your device, you can access it from your host computer. There are three ways to access your system from your remote computer:

  • The first is through a terminal interface called SSH.

  • The second way is by using a program called VNC server. This allows you to open a graphical user interface remotely which mirrors the graphical user interface on the Raspberry Pi.

  • Finally, you can transfer files through a program called WinSCP, which is custom-made for this purpose. You can use a program called SCP for Linux.

So, firstly, make sure that your basic system is up and working. Open a terminal window and check the IP address of your unit. You're going to need this however you communicate with the system. Do this by using the ifconfig command. It should produce the following screenshot:

You need inet addr, which is shown in the third line of the preceding screenshot to contact your board through the Ethernet. If you are using a wireless device to gain access to the Internet, your ifconfig will look like this:

The inet addr associated with the wlan0 connection, in this case 10.10.0.31, is the address you will use to access your Raspberry Pi.

You also need an SSH terminal program running on your remote computer. An SSH terminal is a Secure Shell (SSH) connection, which simply means that you'll be able to access your board and give it commands by typing them into your remote computer. The response from the Raspberry Pi Zero will appear in the remote computer terminal window.

Note

If you'd like to know more about SSH, visit: https://www.siteground.com/tutorials/ssh/.

If you are running Microsoft Windows, you can download an alternative application. My personal favorite is PuTTY. It is free and does a very good job of saving your configuration so that you don't have to type it in every time. Type putty in a search engine and you'll soon come to a page that supports a download or you can go to http://www.putty.org/.

Download PuTTY to your Microsoft Windows machine. Then, run putty.exe. You should see a configuration window which looks something like the following screenshot:

Type the inet addr from the previous page in the Host Name space and make sure that the SSH selection is selected. You may want to save this configuration under Raspberry Pi so that you can reload it each time.

When you click on Open, the system will try to open a terminal window onto your Raspberry Pi through the LAN connection. The first time you do this, you will get a warning about an RSA key as the two computers don't know about each other. Windows therefore complains that a computer that it doesn't know is about to be connected in a fairly intimate way. Simply click on OK and you should get a terminal with a login prompt, like the following screenshot:

Now you can log in and issue commands to your Raspberry Pi Zero. If you'd like to do this from a Linux machine, the process is even simpler. Bring up a terminal window and then type ssh [email protected] where the xxx.xxx.xxx.xxx is the inet addr of your device. This will then bring you to the login screen of your Raspberry Pi Zero, which should look similar to the preceding screenshot.

SSH is a really useful tool to communicate with your Raspberry Pi Zero. However, sometimes you need a graphical look at your system and you don't necessarily want to connect to a display. You can get this on your remote computer by using an application called vncserver. You'll need to install a version of this on your Raspberry Pi Zero by typing sudo apt-get install tightvncserver in a terminal window on your Raspberry Pi Zero. This is a perfect opportunity to use SSH, by the way.

Tightvncserver is an application that allows you to view your complete Raspberry Pi Zero remotely. Once you have it installed, you need to start the server by typing vncserver in a terminal window on the Raspberry Pi Zero. You will then be prompted for a password, then be prompted to verify the password, and then asked if you'd like to have a view-only password. Remember the password that you entered, you'll need it to log in via a VNC Viewer remotely.

You need a VNC Viewer application for your remote computer. On my Windows system, I use an application called RealVNC. When I start the application, it gives me the following screenshot:

Enter the VNC Server address, which is the IP address of your Raspberry Pi Zero, and click on Connect. You will see a pop-up window, as shown in the following screenshot:

Type in the password that you just entered while starting vncserver and you should then get a graphical view of your Raspberry Pi that looks like the following screenshot:

You can now access all of the capabilities of your system, although they may be slower if you are doing a graphics-intense data transfer.

Note

There are ways to make vncserver start automatically on boot. I have not used them; I prefer to type the vncserver command from an SSH application when I want the application running. This keeps your running applications to a minimum and, more importantly, presents fewer security risks. If you want to start yours each time you boot, there are several places on the Internet that show you how to configure this. Try the following website: http://www.havetheknowhow.com/Configure-the-server/Run-VNC-on-boot.html.

To view this Raspberry Pi desktop from a remote Linux computer, which is running Ubuntu for example, you can type sudo apt-get install xtightvncviewer, then start it by using xtightvncviewer 10.25.155.110:1 and supplying the chosen password.

Linux has viewers with graphical interfaces such as Remmina Remote Desktop Client (select the VNC-Virtual Network Computing protocol), which might be used instead of xtightvncviewer. Here is a screenshot of the Remote Desktop Viewer:

Make sure that vncserver is running on the Raspberry Pi Zero. The easiest way to do this is to log in using SSH and run vncserver at the prompt. Now, click on Connect on the Remote Desktop Viewer. Fill in the screen as follows, under the Protocol selection, choose VNC, and you should see the following screenshot:

Now, enter the Host Internet address, making sure that you include a :1 at the end and then click on Connect. You'll need to enter the vncserver password you set up, as shown in the following screenshot:

Now you should be able to see the graphical screen of the Raspberry Pi. To access the Raspberry Pi Zero graphically from a Mac or another Apple device, you can still use Real VNC Viewer, see https://www.realvnc.com/products/ for more information.

The final piece of software that I like to use with Windows is a free application called WinSCP. To download and install this piece of software, go to https://winscp.net/ and follow the instructions to download and install. Once installed, run the program. It will open the following dialog box:

Click on New and you will get the following screenshot:

Here, you fill in the IP address in the host name tab, pi in the user name tab, and the password (not the vncserver password) in the password space. Click on Login and you should see a warning displayed, as shown in the following screenshot:

The host computer, again, doesn't know the remote computer. Click on Yes and the application will display the following screenshot:

Now, you can drag and drop files from one system to the other. You can also do similar things with Linux by using the command line. To transfer a file to the remote Raspberry Pi, you can use the scp file [email protected]:path command, where file is the filename, and [email protected]:path is the location you want to copy it to. For example, if you wanted to copy example.py from your Linux system to the Raspberry Pi Zero, you would type scp example.py [email protected]:/home/pi/. The system will ask you for the remote password which is the login for the Raspberry Pi Zero. Enter the password and the file will be transferred.

Now that you know how to use ssh, tightvncserver, and scp, you can access your Raspberry Pi remotely without having a display, keyboard, or mouse connected to it! If you are connecting via a WLAN connection, your system will now look like this:

You only need to connect the power and the LAN, either with a cable or through wireless LAN. If you need to issue simple commands, connect through SSH. If you need a more complete set of graphical functionality, you can access this through vncserver. Finally, if you want to transfer files back and forth, you can use WinSCP from a Windows computer or scp from a Linux computer. Now you have the toolkit to build your first functionality.

One of the challenges of accessing the system remotely is that you need to know the IP address of your board. If you have the board connected to a keyboard and display, you can always just run the ifconfig command to get this information. However, you're going to use the board in applications in which you don't have this information. There is a way to discover this by using an IP scanner application. There are several available for free; on Windows, I use an application called Advanced IP Scanner, available at www.advanced-ip-scanner.com/. When you start the program, it looks like the following screenshot:

Clicking on the Scan selector scans for all the devices connected to the network. You can also do this in Linux; one application for IP scanning in Linux is called Nmap. To install Nmap, type sudo apt-get install nmap. To run Nmap, type sudo nmap -sp 10.25.155.1/154 and the scanner will scan the addresses from 10.25.155.1 to 10.25.155.154.

These scanners let you know which addresses are being used and this should allow you to see your Raspberry Pi address without typing ipconfig.

Your system has lots of capabilities. Feel free to play around with the system—try to get an understanding of what is already there and what you'll need to add from a software perspective. Remember, there is limited power on your USB port, so make sure that you are familiar with the power needs of accessories plugged into your Raspberry Pi. You may very well need to use a powered USB hub for many projects.