Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi Zero Cookbook

You're reading from  Raspberry Pi Zero Cookbook

Product type Book
Published in Mar 2017
Publisher Packt
ISBN-13 9781786463852
Pages 422 pages
Edition 1st Edition
Languages

Table of Contents (17) Chapters

Raspberry Pi Zero Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Kick-Start Your Raspberry Pi Zero Setting Up Physical and Wireless Connections Programming with Linux Programming with Python Getting Your Hands Dirty Using the GPIO Header Controlling the LEDs and Displays Controlling the Hardware Taking Digital Inputs to the Raspberry Pi Zero Interfacing Sensors with the Raspberry Pi Zero Cooking up Projects to Amaze the World!

Chapter 2. Setting Up Physical and Wireless Connections

In this chapter, we will cover the following recipes:

  • Controlling the RPZ from the UART GPIO port using the console cable

  • Adding a Wi-Fi dongle over USB OTG

  • Hacking RPZ hardware to add a permanent Wi-Fi dongle

  • Setting up dynamic and static IP addresses for the RPZ

  • Pinging from another computer over same network

  • SSHing your RPZ from your desktop computer

  • Sharing a screen on your desktop computer

  • Copying different files to and from your home network

  • Adding USB functions to Raspbian Jessie

  • Using a virtual serial adapter on USB OTG

  • Programming over a virtual Ethernet modem on USB OTG

  • Making your RPZ a USB mass storage device

Introduction


Now that we have our operating system running, we can really start to thinking about the portability advantages this Raspberry Pi Zero can bring. We'll start talking to the Raspberry Pi Zero, get it on the network, and look at the unique features and modes you get with the Zero's small form factor.

Controlling the RPZ from the UART GPIO port using the console cable


If you already have a computer that you use at home, the Raspberry Pi Zero will probably not be serving as a replacement. Wouldn't it be nice if you could work with the Zero through your own home computer? Then you only need one mouse, one keyboard, and one monitor. This recipe will show you how to use the Raspberry Pi Zero's UART GPIO to communicate with a USB-to-serial interface. Becoming familiar with this interface is great for troubleshooting (for example, when USB isn't working or you don't have a monitor nearby) and is one many different ways you can interact with your Zero.

Note

What is a UART and a GPIO? UART stands for Universal Asynchronous Receiver/Transmitter. Basically, that means it is a communication device understood by a lot of different devices, and it talks back and forth and doesn't wait for an acknowledgement. GPIO stands for General Purpose Input/Output and is the set of communication connections that...

Adding Wi-Fi dongle over USB OTG


An easy way to communicate with your Pi and get your little computer connected to the Internet is connecting it to a USB Wi-Fi adapter. Once you have your Raspberry Pi Zero connected to Wi-Fi, we can get it to interact with pretty much anything that lives on your home network.

Getting ready

For this recipe, you will need your Raspberry Pi Zero, a USB OTG adapter, and a USB Wi-Fi device. Most of this equipment comes with any starter kit and is inexpensive to buy individually-the OTG adapters can be found for less than $5 and Wi-Fi dongles are $10-$15. It is still amazing that a lot of these accessories cost at least as much as the computer they are needed for!

A small USB OTG dongle and Wi-Fi adapter make for a pretty small setup:

You do need to add power, but even with that, you maintain a very small footprint for what will be an Internet-enabled computer.

The embedded Linux site ( elinux.org ) has a comprehensive listing of tested and compatible Wi-Fi cards...

Hacking RPZ hardware to add up permanent Wi-Fi dongle


The USB dongle solder hack is considered one of the very first hacks shared in the community after the release of the Raspberry Pi Zero. With such a small form factor and USB OTG connectors, many hackers didn't like the idea of adding to the size of the board with an adapter and then tacking on a USB dongle. This recipe shows you how to attach an uncased Wi-Fi adapter to the Raspberry Pi Zero to keep your footprint still small enough to fit a mints tin.

Getting ready

This is another soldering hack, and not necessarily for the faint of heart. You'll need a compatible USB Wi-Fi dongle, but it is important to make sure to check the compatibility list on https://www.elinux.org to ensure that your dongle is both compatible and does not require additional power. Since we will be soldering this one right to the Zero, it will need to be able to get all of the power it needs from the incoming USB 5V port.

A great way to test whether you have the...

Setting Up dynamic and static IP address for RPZ


Now that we have our Raspberry Pi Zero on the network, let's look a little closer at what we can do with the networking. Everything should have connected to your Wi-Fi network thanks to DHCP (Dynamic Host Allocation Protocol). DHCP takes incoming requests from devices to be added to the network. It has a pool of IP addresses on the network that it will "lease" to the device for a period of time. After the lease expires, the device may renew (and, depending on the DHCP server rules, will get a new or the same address), or the address will be returned to the available pool. This makes it easy to add machines to a network without keeping track of individual addresses assigned to devices. When your Raspberry Pi Zero connected to your Wi-Fi network, by default, it would have been assigned at least one IP address: an IPv4 address (four sets of numbers between 0 and 255, as in 192.168.17.250), and/or a newer IPv6 address (8 sets of hexadecimal numbers...

Pinging from another computer over same network


Now that we have our computer on the network, we want to make sure other computers are able to contact it. The ping command is one of the first tests to tell whether your computer can be contacted. The command sends a small test packet of data to another computer's IP address or hostname and waits to hear a response. If it does, it tells you how long it took for the response to come back. If it doesn't, it tells you it is not receiving a response.

Note

Some computer systems have firewalls that do not allow ICMP ECHO attempts. This means that even though the computer is available on the network, attempts to ping it will fail. On the default configuration of Raspbian, ping commands are allowed, so attempts to ping your Pi should succeed.

Getting ready

As long you as you know your IP address (whether it was dynamically assigned using DHCP or you assigned a static IP from the previous recipe), you should be able to contact it from your network. The...

SSHing your RPZ from your desktop computer


The ideal way to talk to your Raspberry Pi Zero is using Secure SHell (SSH). This gives you a secure, encrypted channel to talk to your Pi from anywhere on the network. For example, let's say you want your Pi to collect temperature information from your attic and make it available on your home network. You don't want to drag a monitor, mouse, and keyboard up to your attic every time you want to make a change-you want to do it from your regular home computer! If you have your RPZ using Wi-Fi, on the network, and ping-able, the next step is to work with it remotely over SSH. Here's how.

Getting ready

If you are using a Windows machine, PuTTY is the way to go, just like a serial connection. If you are on OSX or Linux, SSH should be installed and available using any terminal window. You'll also need the IP address of your Raspberry Pi Zero.

If you ran through the earlier recipe to enable SSH using the rasp-config utility covered in Chapter 1, Kick-Start...

Sharing a screen on your desktop computer


While it is inevitable that you will use the command line a lot when using Linux, a lot of people enjoy Raspbian's GUI. But you still want to keep your Zero in the attic! No problem at all: the Virtual Network Computing (VNC) service makes the GUI available from anywhere you able to connect.

Getting ready

Stay connected to your Raspberry Pi Zero with your SSH terminal. On your home computer, you will need a VNC viewer application. I love the VNC Viewer Chrome extension, and it is available on any platform.

How to do it...

  1. First, you need to install a VNC server on your Raspberry Pi Zero. To install it on Raspbian, type the following:

            sudo apt-get install tightvncserver
    

  2. Once it's installed, starting it is as simple as typing vncserver:

            $ vncserver
            New 'X' desktop is rpz14101:1
            Starting applications specified in 
            /home/pi/.vnc/xstartup
            Log file is /home/pi/.vnc/rpz14101:1.log 
    

  3. The first...

Copying different files to and from your home network


Now that your Raspberry Pi Zero is sitting comfortably on your home network, it can work as a central part of it and interact with the other computers using it. This will show you how to set up a file share on your Raspberry Pi that can interact with the computers on your home network.

Getting ready

You can either stay connected to your Zero over SSH, or you can open a terminal in your VNC viewer window-it's all up to you. Because it is rendering graphics, VNC will tend to be a more resource intensive and maybe a little more choppy than your SSH terminal; this recipe does not require any GUI tools to get working.

How to do it...

Samba is really the easiest way to set up a file share if you have Windows machines on your network. While Macs and Linux machines work with Network File Sharing (NFS) very easily, success with using this on Windows operating systems varies quite a bit. Most Home Edition versions of Windows unfortunately do not support...

Adding USB functions to Raspbian Jessie


The latest release of Raspbian Jessie added some wonderful hacks that make the Raspberry Pi both an ideal test device and at the same time the most inexpensive awesome little Linux server you could fit into a box of mints. With the next series of hacks, we will use the USB port to connect as a serial device, a network device, or even a mass storage device (like a USB flash drive). The possibilities grow with every release!

If you are using Mac OS X, you should be all set-the Bonjour services come installed. If on Windows, Apple's Bonjour print services will be the only installation you need to get things working. Finally, on Linux, avanti-daemon needs to be installed, which it is by default as of Ubuntu 14.04.

Note

The USB OTG functions are only available on the Raspberry Pi Zero. The reason for this is that the USB port is connected directly to the processor for the Zero but runs through a USB hub on its larger siblings. This is an RPZ-only hack!

Getting...

Using a virtual serial adapter on USB OTG


Some of the coolest hacks that are available on the Raspberry Pi Zero are to do with getting it to work with other computers using a single USB cable. In this recipe, we will create a "virtual" serial adapter, so instead of connecting with a specialized USB-to-serial adapter/cable, we can create the same connection using a standard regular-to-micro USB cable. I can carry around the Pi Zero and a cable in my backpack, and any time I need a Linux terminal, I just need a machine with a keyboard and a screen to plug into.

Getting ready

All you need to do to be set up here is perform the USB OTG previous functions recipe. After making the change, you can connect your Pi to any computer from the micro-USB connection. For most desktops, you won't even need additional power to run the Pi; it will all work over the one connection.

How to do it...

This is the most finicky file you will have to edit. Be very careful not to make any mistakes, as it will probably...

Programming over a virtual Ethernet modem on USB OTG


This is my favorite way to connect to the Raspberry Pi Zero. Not only can you operate it with a single USB cable, but you can get it attached to the network and be able to communicate with other machines and the Internet.

Getting ready

The same as the last recipe, if you have the USB OTG functions installed and a regular-to-micro USB cable, you are ready to go.

How to do it...

Just like the last recipe, be careful with the edits to cmdline.txt. You can make this change from the serial connection you made before, reboot, and go!

  1. The unedited version of the file should look something like this:

            pi@rpz14101:~$ sudo cat /boot/cmdline.txt
            dwc_otg.lpm_enable=0 console=serial0,115200 
            console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 
            elevator=deadline fsck.repair=yes rootwait
    

    If you're coming from the previous recipe, it will look like this:

            pi@rpz14101:~$ sudo cat /boot/cmdline.txt
       ...

Making your RPZ a USB mass storage device


This last hack makes it possible to use your Raspberry Pi Zero as a mass storage device. While at first it doesn't seem like much more than turning it into a flash drive, depending on what you are doing, and what else you are doing with your Raspberry Pi Zero, you can make your Raspberry Pi Zero be recognized as a USB flash drive to other computers.

Getting ready

Since we will be using the USB connection for a mass storage device, we should connect to the Raspberry Pi Zero using the USB-to-serial adapter and the GPIO pins, as shown in the first recipe of this chapter. You can make your modifications here, connect your USB and Raspberry Pi together, reboot, and work with the Pi as a flash drive or as the little computer you're used to.

How to do it...

  1. We're going to add one piece after rootwait to make the Pi enabled for USB mass storage. You've seen this in the previous recipes:

            pi@rpz14101:~$ sudo cat /boot/cmdline.txt
            dwc_otg.lpm_enable...
lock icon The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi Zero Cookbook
Published in: Mar 2017 Publisher: Packt ISBN-13: 9781786463852
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}