The Official Raspberry Pi Projects Book - Projects_Book_v4

(singke) #1

Tutorial


raspberrypi.org/magpi The Official Raspberry Pi Projects Book 139


MARAUDER’S CLOCK


Left The Arduino,
Raspberry Pi,
and relays are
mounted on
the rear of the
case with long
jumper cables so
that the box can
be opened

Above The coil connectors of the relay are connected to 5V
and output on the Explorer HAT Pro. The switch connectors are
connected to digital input 1 (and 2 for the second relay) and the
ground on the Arduino


!/usr/bin /env python

import subprocess
import time
import explorerhat

occupant = ["Person 1 device","Person 2 device"]

# MAC addresses for our devices - in order of people above
address = ["XX:XX:XX:XX:XX:XX","XX:XX:XX:XX:XX:XX"]

while True:
print("starting loop")
output = subprocess.check_output(
"sudo arp-scan -l", shell=True)
print ("starting scan")
for i in range (len(address)):
if address[i] in output:
print address[i]
print occupant[i]

if "XX:XX:XX:XX:XX:XX:XX" in output:
#Person 1 MAC address
explorerhat.output[ 0 ].on()

else:
explorerhat.output[ 0 ].off()

if "XX:XX:XX:XX:XX:XX:XX" in output:
#Person 2 MAC address
explorerhat.output[ 1 ].on()

else:
explorerhat.output[ 1 ].off()
time.sleep( 60 )

clock.py


Language
>PYTHON

DOWNLOAD:
magpi.cc/2lOfkTJ


STEP-04
Connect the LEDs
Ze will be using four OHGs to light up diļerent parts of
the Marauder’s map. Connect the short leg (cathode)
to a common ground. Connect the long leg (anode) to
digital pins 5,6,7, and 8.
The following LED pins are used:



Digital Pin 5 = Person 2
Digital Pin 6 @ Dway notification
Digital Pin 7 = Person 1
Digital Pin 8 @ Kome notification



STEP-05
Link the Pi and Arduino
Before connecting the relays, we will need to look at
the pin guide and identify the two pins for the coil and
the two pins for the switch. One side of the coil on
both relays should be connected to the 5 V output on
the Explorer HAT Pro. The other side of the of the relay
coil should then be connected to output 1 and 2 on the
Explorer HAT Pro. The common switch connector on
both relays should then be connected to the common
ground on the Arduino. The switch connector on the
first relay is connected to Gigital 4 on the Drduino, and
the switch connector on the second relay is connected
to Digital 2. We will be using the switch connector,
which is activated when the relay receives power.




STEP-06
Assembling the project
We will need to download the code for the Arduino and
upload it to the Uno from here: magpi.cc/2lOfkTJ. We
then need to assemble our project. In our example we
have used a fruit box, but you could use an old clock, a
cardboard box, or a hobby box. We need to (carefully)
screw clock hands to the two servos; take care not to
move the servo as it can cause damage. Mount the
two hands so that that the continuous rotation servo
can move freely and the positional servo can move up
and down.



CONNECTING
THE RELAYS

If the output
from the relay
is reversed
(i.e. on when it
should be off),
try swapping
the pole of
the switch on
the relay.
Free download pdf