官术网_书友最值得收藏!

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 can be configured to interact with various different things, from LCD screens to humidity monitors to microphones. We'll be exploring GPIO a lot more in later recipes.

Getting ready

For this recipe, you will start using your Raspberry Pi Zero's GPIO port. Out of the box, your Zero will not have a header on its GPIO connections-this way, if you have a specific purpose in mind for your board that requires keeping a very low profile, wires can be soldered directly to the GPIO holes.

If you don't have a specific project in mind for your Zero (yet), then attaching a header is the way to go. They are quite inexpensive and available at the same places you would find a Raspberry Pi. You can attach a male or female header, and they are available in both 0- and 90-degree positions. A female header combined with an Adafruit Raspberry Pi Cobbler and a breadboard makes for an ideal prototyping setup.

A 90-degree header, Adafruit Cobbler, and breadboard make the perfect starter lab for your Zero:

Note

As you get into the Internet of Things and want to try making your Raspberry Pi do more things, inevitably, you will need to take up soldering. While it is fun and pretty easy to learn, you don't want to give it your first try on your Raspberry Pi Zero header. Find a hacker friend with some soldering experience to help you your first time, or find a local tech user group: there is always someone happy to help. There are also great videos and guides on the Internet, but I can promise it is a little bit harder than many of the videos make it look. Don't break your Pi with a soldering disaster!

For this recipe, you will also need a USB-to-serial dongle. With this, you can communicate between any computer with a USB port and your Raspberry Pi Zero. The Adafruit Raspberry Pi Starter Pack includes a USB-to-serial cable, a choice of headers, and the Raspberry Pi Cobbler-everything you need to get going. There are a lot of different choices available, all around the $10-$15 range. When you start getting more into hardware and microcontrollers, you will find yourself needing one of these regularly.

There are a few kinds of serial dongles; Adafruit's is perfect for something like this: one end attaches to your computer's USB port, and the other to your Raspberry Pi Zero's GPIO:

Another old favorite is the USB BUB:

How to do it...

  1. First, let's take a look at the configuration of the GPIO pins:

    Look for the hole with the square around it. This is pin 1 on all Raspberry Pi GPIO headers. The pin to its right is 2, below it 3, and so on:

    While you will get more familiar with several of these in the course of reading the book, the ones we use for this recipe are:

    • 6 GND (Ground)
    • 8 TXD0 (Transmit Data)
    • 10 RXD0 (Receive Data)
  2. Connect the black wire of the USB-to-serial cable to pin 6, the green cable to 8, and the white cable to 10. The red wire is not connected for this recipe. A typical USB to Serial connection to your Raspberry Pi Zero will resemble this:
    Note

    Your wire colors may vary, depending on which USB-to-serial device you are using. Also, depending on your Operating System, you may need to install the appropriate USB driver.

  3. If you are connecting your Raspberry Pi Zero to another Linux machine (or even another Raspberry Pi!), there are several ways to begin communicating with the Raspberry Pi Zero. First, identify which port your serial cable is connected to. If there aren't any other devices connected, it should be /dev/ttyUSB0. An easy method to identify the port is to plug it in and run dmesg | epgrep 'serial|ttyU*|ttyS*'. Your output should be something like this:
    $ dmesg | egrep 'serial|ttyU|ttyS'
    [ 0.000000] Kernel command line: 8250.nr_uarts=0 dma.dmachans=0x7f35 bcm2708_fb.fbwidth=640 bcm2708_fb.fbheight=480 bcm2709.boardrev=0xa02082 bcm2709.serial=0xfd550dda smsc95xx.macaddr=B8:27:EB:55:0D:DA bcm2708_fb.fbswap=1 bcm2709.uart_clock=48000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000 dwc_otg.lpm_enable=0 console=ttyS0,115200 console=tty1 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
    [ 5.342786] usbcore: registered new interface driver usbserial
    [ 5.342987] usbcore: registered new interface driver usbserial_generic
    [ 5.343113] usbserial: USB Serial support registered for generic
    [ 5.347321] usbserial: USB Serial support registered for pl2303
    [ 5.352416] usb 1-1.5: pl2303 converter now attached to ttyUSB0 
    

    The dmesg command returns an ordered list of the events that have occurred on the computer. One of the most recent events will be your connecting the USB. The last line of the output shows ttyUSB0, which corresponds to the "device" /dev/ttyUSB0. This device is the communication path to your Zero, and with a terminal program, you will be able to use this command to control it. Pretty much every Linux distribution is going to have screen, minicom, or the Python miniterm.py script available by default. Any one of these will work for talking to your Raspberry Pi Zero.

  4. The simplest command to get on your Raspberry Pi Zero over serial is this:
    screen /dev/ttyUSB0
    
    Note

    You may need to provide the speed and duplex, depending on your setup. The Raspberry Pi serial connection default is 115200, 8 Bits, No Parity, One Stop Bit (typically referred to as 8N1)

    This will replace your terminal window with a terminal window from the Raspberry Pi Zero. Anything you enter from this point will be sent to and executed on the Zero. To exit the terminal and return to your host machine, type Ctrl + A, followed by K. Minicom and miniterm.py are a little more robust and feature-rich than screen, but they all pretty much do the same thing.

    Note

    Sometimes, you will see some strange characters and undecipherable mangled text come in when you open the serial connection. You haven't hacked into the Matrix (or have you?); it is just the serial connection getting itself figured out. Usually, entering a few characters and hitting Enter a few times clears it up and gets text into human-readable format.

  5. If you are using Windows, PuTTY is the perfect open source program for connecting over serial ports and, later on in the cookbook, SSH. Once you connect your Pi to your Windows computer, you need to identify which COM port it is on. It will come online as a new device in Device Manager, and then you just need to select Serial and the COM number:
主站蜘蛛池模板: 凤庆县| 海门市| 辽中县| 孙吴县| 晋城| 昔阳县| 普兰县| 资兴市| 卓资县| 黄石市| 遵化市| 玉山县| 保定市| 星子县| 双江| 中牟县| 天柱县| 武川县| 剑河县| 志丹县| 乐都县| 额济纳旗| 姜堰市| 东平县| 宁乡县| 东乌| 五河县| 淅川县| 岗巴县| 类乌齐县| 华亭县| 福泉市| 都江堰市| 宜川县| 井研县| 凤山市| 榕江县| 泌阳县| 特克斯县| 临夏县| 马山县|