Выбрать главу

After the values are updated, the remainder of the read_arduino method sets the color of the fields to red or green as appropriate.

To ensure that the read_arduino method is called at regular intervals, it is necessary to schedule a call to it from the Tk user interface object:

def update():

    app.read_arduino()

    root.after(500, update)

root.after(100, update)

This code ensures that after 100 milliseconds (1/10 second), the function update will be called. The function update first calls read_arduino and then schedules itself to run again in 500 milliseconds (half a second), meaning that our control center checks all of our sensors every half second. If you’re in danger, whether from zombies or environmental hazards, you’ll know quickly!

You can run this program at the same time as you run the USB webcam of “Project 7: Monitor Zombies with a USB Webcam” on page 87 by opening two LXTerminal windows and running one program in each terminal window. That way, you can see instantly what might have triggered your alarms.

USING THE CONTROL CENTER

Now, you have a screen that will give you continuous updates on all of the safeguards of your stronghold. Place your control center somewhere you can easily see it, and if you’ve included all of the components from Figure 8-3, you’ll know instantly if your supplies are in danger, if your power supply is running low, and if zombies have breached your perimeter.

If you find that the user interface values do not update, then go back to “Arduino Software” on page 143 and again test the Arduino using the serial monitor by sending the ? command to look for a status response in the serial monitor.

PROJECT 14: GOING WIRELESS WITH BLUETOOTH

The control center of Project 13 is bogged down in wires right now, and you have to keep the Arduino and the Raspberry Pi together. That also means that you’ll probably only know that, say, your base has caught on fire once the flames have reached you—and then it will be too late. You can make your control center much more effective by connecting the Raspberry Pi and the Arduino wirelessly over Bluetooth, as we’ll do in this project, so your sensors can detect danger before it reaches you and your monitor.

The Raspberry Pi does not have Bluetooth built in, but it will accept a wide range of Bluetooth USB dongles. We’ll add Bluetooth to the Arduino using a Bluetooth serial module, shown sticking out at the right in Figure 8-6.

Figure 8-6: Adding Bluetooth to an Arduino

To make this project, first complete “Project 13: A Raspberry Pi Control Center” on page 140 and make sure that everything else is working properly. Then you’ll be ready to add the wireless link.

WHAT YOU WILL NEED

To make this project, you are going to need everything from Project 13 plus the following parts:

ITEMS

NOTES

SOURCE

USB Bluetooth dongle

Compatible with Raspberry Pi

Computer store, eBay

Bluetooth module

HC-06 Bluetooth serial module

eBay

270 Ω resistor

Mouser (293-270-RC)

470 Ω resistor

Mouser (293-470-RC)

Connecting wire

Header pins

4-way

Adafruit (392), eBay

Screwshield

Adafruit (196)

Multistranded or solid-core hookup wire

For making connections on the prototyping area of the screwshield

Adafruit (1311), scavenge

Female-female jumper wires (×4)

(Optional) Would replace header pins

Adafruit (266)

The hardware for this project can be built onto the screwshield that you have used while building up the various sensor projects (4, 6, 10, 11, and 12) that use a screwshield. The Bluetooth module I used is a Cambridge Silicon Radio (CSR) device. For a list of Bluetooth dongles compatible with the Raspberry Pi, visit http://elinux.org/RPi_USB_Bluetooth_adapters/. If you are worried about soldering the Bluetooth module directly to the header pins, then you may prefer to use four female-to-female jumper wires to link the header pins to the Bluetooth module.

NOTE

You can save yourself some tricky soldering by looking for a module and adapter pair that already has the module soldered into place.

A lot of the Bluetooth HC-06 modules have six rather than four pins. The pins you will be using are +5V, GND, TXD, and RXD, so you can ignore the other two. These are usually the outside pins, but do check the pinout names as occasionally some designs swap the pin positions around.

CONSTRUCTION

To enable Bluetooth connectivity for your Raspberry Pi, you only need to attach a USB dongle to your system.

The Arduino requires the aforementioned Bluetooth module and a pair of resistors to divide the 5V signal level of the Arduino to the 3V level expected by the Bluetooth module. Mount the module and resistors to the side of the screwshield’s prototyping area not already being used by the fire alarm interface from Project 11.

Figure 8-7 shows the wiring layout for the project. To avoid confusion, Figure 8-7 shows the Bluetooth module attached to a screwshield without any other projects built on it.

Figure 8-7: Wiring layout for adding Bluetooth to an Arduino

The Bluetooth module needs to lie flat to keep it out of the way of the LCD shield. For this, you need to solder a row of four 0.1-inch header pins and then solder the Bluetooth module perpendicular to the pins, lying flat over the screwshield. If you prefer, you may also use female-to-female jumper wires to connect the Bluetooth module to the header pins.

STEP 1: SOLDER THE HEADER PINS

Solder the strip of header pins into place. You can see in Figure 8-8 that the +5V and GND pin connections neatly line up with the two power rows at the top of the screwshield.

Figure 8-8: The header pins soldered in place

Note that the wire shown leading to pin 3 of the Arduino is part of the fire alarm from Project 11, not this project.

STEP 2: SOLDERING THE RESISTORS AND LINKING WIRE

Solder the resistors and linking wire to the screwshield in the positions shown in Figure 8-9: the 470 Ω resistor goes from GND at Arduino column 7 to the bottom header at column 4; the 270 Ω resistor goes from the bottom pin of the header at row 3 to Arduino pin 1. The connecting wire runs from Arduino pin 0 to the third header pin down.

Figure 8-9: Soldering the resistors and connecting wire