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

8

BUILDING A CONTROL CENTER FOR YOUR BASE

In this chapter, you’ll learn how to make an integrated control center using a Raspberry Pi computer interfaced with earlier projects from this book. The control center will allow you to monitor all of your alarm and surveillance devices on one screen so you’ll know instantly if a zombie has breached your compound (Figure 8-1). As an extra feature, you’ll learn how to add wireless connectivity to your control center.

Figure 8-1: A quiet night at the security desk

PROJECT 13: A RASPBERRY PI CONTROL CENTER

In this project, you’ll connect the Raspberry Pi system of Chapter 5 with the following Arduino monitoring devices developed earlier in the book:

• “Project 4: Battery Monitor” on page 53

• “Project 6: PIR Zombie Detector” on page 72

• “Project 10: Door Sensor” on page 112

• “Project 11: Quiet Fire Alarm” on page 120

• “Project 12: Temperature Alarm” on page 131

We’ll link the two boards with USB cables, which we can later replace in Project 14 with a wireless Bluetooth link. The Arduino will still be able to work without the Raspberry Pi after this wireless modification, but linking it to the Raspberry Pi will allow you to show the status of your sensors and alarms in a window on the Raspberry Pi. Figure 8-2 shows the setup; you can see the sensor status window in the center of the screen.

Figure 8-2: Raspberry Pi and Arduino working together

WHAT YOU WILL NEED

This project brings together the Raspberry Pi system of Chapter 5 and most of the Arduino projects described in the book thus far. As such, all you will need is the following:

One or more of the previous Arduino projects

The Raspberry Pi system from Chapter 5

A USB lead/cable (of the same type used to program your Arduino project)

CONSTRUCTION

Assuming that you have been slowly adding projects to your Arduino, the Arduino now has five projects attached to it. If you’re really prepared, you probably built these ages ago and have them stashed in your go bag, ready for the apocalypse. Either way, you should at least have the sensors you are interested in using.

If your Arduino projects and Raspberry Pi are already set up, you won’t need to do much construction to link them. You connect an Arduino project to the Raspberry Pi by plugging one end of the USB lead into the Pi and the other end into the Arduino. If your Raspberry Pi does not have any free USB ports, then you will need to add a USB hub to provide more ports.

Now that you have linked your Arduino and your Raspberry Pi, you’ll need to program them. It’s best to program the Arduino from your regular computer before swapping the USB cable over to the Raspberry Pi, as programming the Arduino from the Raspberry Pi’s small screen can be frustrating.

Figure 8-3 shows the arrangement of the various system components.

Figure 8-3: A schematic of the control center

This arrangement plays to the strengths of both the Arduino and Raspberry Pi. The Raspberry Pi cannot directly use many of the sensors that are connected to the Arduino, while the Arduino can. At the same time, the Arduino does not have a screen, while the Raspberry Pi does.

SOFTWARE

There are two parts to the software for this project: a modified version of the All_Sensors Arduino sketch and a Python program run on the Raspberry Pi to allow it to communicate with the Arduino.

Before the apocalypse, make sure you’ve downloaded the source code for this book; go to http://www.nostarch.com/zombies/ to get started.

ARDUINO SOFTWARE

The Arduino sketch you will use for this project, Project_13_Control_Center_ USB, is based on the All_Sensors sketch that runs all of the other Arduino projects in this book. Project_13_Control_Center_USB just adds code to allow your Arduino to communicate with other devices over a serial connection (in this case, USB).

NOTE

For instructions on loading sketches onto your Arduino, see Appendix C.

It’s best to test each part of this fairly complex system in isolation on your regular desktop or laptop computer before connecting it to the Raspberry Pi. You can power the Arduino from the USB connection to your laptop while testing, so you don’t need to use your postapocalyptic car battery power supply for preapocalyptic testing.

To begin testing, load the Project_13_Control_Center_USB sketch onto the Arduino and click the magnifying glass in the Arduino IDE to open the serial monitor (Figure 8-4).

Figure 8-4: The serial monitor

Make sure that “9600 baud” is selected in the drop-down list at the bottom right of the serial monitor. This is the baud rate, the speed at which data is sent (measured in bits per second), and it must match the speed set in the sketch.

In the text entry area at the top of the serial monitor, enter the ? command and click Send. The Arduino should display a line of numbers like the 4.27 26.10 1 0 0 shown in Figure 8-4 (your numbers will not match these, exactly). These numbers are the battery voltage, temperature, door status, PIR status, and smoke alarm status, respectively. For the three status values, 0 means everything is okay and 1 indicates an alarm. These are the values that will later be displayed on the control center. By simulating how the Raspberry Pi will fetch the values, you are testing that the Arduino part of the project is working.

If you’re currently holding any zombies captive for research, try putting the temperature sensor up against a zombie’s skin and enter the ? command again. If you’re lacking in test subjects (or feeling less adventurous), just hold the sensor between your fingers. Either way, you should see the temperature part of the message change.

If the responses in the serial monitor indicate that the Arduino side of your control center is working properly, you can unplug the Arduino from the regular computer and attach it to a USB port of the Raspberry Pi.

If the numbers do not appear, then check that the sketch uploaded properly onto the Arduino. If the numbers reported are not what you would expect for one of the projects, then check the wiring for that particular project.

Look at the Arduino code in Project_13_Control_Center_USB, and you will see that unlike in All_Sensors, the setup function includes the following line at the end:

Serial.begin(9600);

This line tells the Arduino to open a serial connection, via its USB-serial interface, at a baud rate of 9600. The value passed to begin must match the value you set in the serial monitor’s baud rate drop-down list.

This sketch also has a change at the top of the loop function:

if (Serial.available() && Serial.read() == '?')

{

  reportStatus();

}

These lines check whether any serial communication over USB is waiting to be processed. If so, when you send the ? message, the reportStatus function is called: