def led_red():

    GPIO.output(red_pin, True)

    GPIO.output(green_pin, False)

def led_green():

    GPIO.output(red_pin, False)

    GPIO.output(green_pin, True)

The Raspberry Squid is just an RGB LED, and as with most RGB LEDs, you can select the color it glows by outputting certain combinations of high (True) and low (False) on the GPIO pins the LED is connected to. In this case, you want red and green, so the code just sets the appropriate pin to True and the other to False. The blue takes no part in this project, so you don’t have to deal with it in the code.

Finally, we come to the main loop of the program, where the new image is fetched and scaled so it’s ready to display in the window.

count = 0

led_green()

while True:

    count = count + 1

    new_image = webcam.get_image()

    # Set old_image the first time around the loop.

    if not old_image:

        old_image = new_image

    scaled_image = pygame.transform.scale(new_image, window_size)

    # Only check one frame in 10.

    if count == 10 :

        if check_for_movement(old_image, new_image):

            led_red()

        count = 0

    old_image = new_image

    screen.blit(scaled_image, (0, 0))

    pygame.display.update()

The count variable keeps track of how many times the loop has run. When count gets to 10, the last two images are compared. Sampling only one-tenth of the time also speeds up the program, which would otherwise be too slow. If there was movement, meaning check_for_movement returns True, the LED turns red.

The last part of the main loop checks for the close window event (which stops the program).

# Check for events.

for event in pygame.event.get():

    if event.type == pygame.QUIT:

        webcam.stop()

        pygame.quit()

        sys.exit()

    if event.type == pygame.KEYDOWN:

        print(event.key)

        if event.key == 32: # Space

            led_green()

The event checking also catches any key press event (KEYDOWN), and if the spacebar is pressed, the program sets the LED back to green.

USING THE WEBCAM

To get the webcam started, run monitor.py by entering the following commands in a terminal window on your Raspberry Pi. A window should open showing a view from the webcam (Figure 5-8).

$ cd "/home/pi/zombies/Raspberry Pi/usb_webcam"

$ sudo python monitor.py

At this point, the Raspberry Squid LED should be green. To test the movement detection, wave your hand in front of the webcam. The LED should go red and stay red until you press the spacebar on the Raspberry Pi’s keyboard.

When everything is working with the webcam connected directly to the Raspberry Pi, you can use the USB extension lead to place the camera further away. Place the camera somewhere overlooking your base’s entrance, and then you’ll know when the coast is clear to go outside.

There will be a limit on how far you can move the webcam before the signal degrades and you start getting errors, so keep the lead under 30 m.

Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi _70.jpg

Figure 5-8: The USB webcam in operation

PROJECT 8: A WIRELESS ZOMBIE SURVEILLANCE SYSTEM

There may be no Internet after the apocalypse, but that doesn’t mean you can’t set up your own wireless network and attach a Wi-Fi webcam to it. You can use a low-cost webcam for this project (Figure 5-9). With a wireless webcam, you can put even more distance between you and the zombies you’re monitoring, making you safer than ever.

Once you set up the camera and a local network, you can view the camera video from the browser on your Raspberry Pi (Figure 5-10) or even a Wi-Fi-equipped tablet or smartphone. What’s more, if you buy the right sort of webcam, you’ll be able to use software to change the direction the webcam is pointing.

Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi _71.jpg

Figure 5-9: A low-cost Wi-Fi webcam

Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi _72.jpg

Figure 5-10: Using a Wi-Fi webcam with the Raspberry Pi

All this comes at a cost, of course: Wi-Fi uses quite a lot of power. The wireless router and Wi-Fi webcam are likely to both use between 5W and 10W of power each. You want to turn them on only when needed.

Note that the Raspberry Pi in Figure 5-8 still has the Raspberry Squid attached, even though this project doesn’t use the Squid. Leave Project 7’s hardware connected, and you can monitor zombies from both cameras!

WHAT YOU WILL NEED

To setup this Wi-Fi webcam, you’ll need the Raspberry Pi setup described in “The Raspberry Pi System” on page 83 and these additional items.

ITEM

NOTES

SOURCE

Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi _18.jpg
Wi-Fi webcam

Preferably a unit that can rotate ($50)

Computer store, eBay

Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi _18.jpg
Wi-Fi router

Low-end unit ($20) operating from 12V DC supply

Computer store, eBay

Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi _18.jpg
2x Ethernet cable

Any length will do.

Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi _18.jpg
2x 12V adapter lead

2.1 mm jack plug-to-cigarette lighter adapter

Auto parts store

Wi-Fi webcams are available at a wide range of costs. The device I chose is at the low-cost end and while the image isn’t fantastic, it’s plenty good enough to spot zombies.

The Wi-Fi router is just a normal household router; most homes with Internet access probably have several, and I’ll bet you have a spare lying around, too. These devices serve two purposes: first, to connect your devices to the Internet (not going to happen with zombies all over the place) and, second, to make a local area network (LAN) to which you can attach wired and wireless devices. We’ll use the second function of the Wi-Fi router here.

CONSTRUCTION

This project uses ready-made components, so you don’t really have any electronics construction to do. You’ll just be connecting components (Figure 5-11).

Maker's Guide to the Zombie Apocalypse: Defend Your Base with Simple Circuits, Arduino, and Raspberry Pi _73.jpg

Figure 5-11: Schematic for the Wi-Fi camera system


Перейти на страницу:
Изменить размер шрифта: