PROJECTS: Exploring the Microverse
Save this file as keys.py, then create another new
file in the same folder called tweettest.py, with the
code shown below.
In this new file, we are importing Twython from
the twython module as well as the keys and
tokens from keys.py. Next, we use those keys and
tokens to connect to the Twitter API, before
sending a test tweet. Change the text in my
example message into something you’d like to
say, then save your file and run your script. You’ve
sent your first tweet using Python!
4. TEST THE CAMERA
With your Raspberry Pi turned off, connect the
camera module. Gently pull up on the edges of
the ports plastic clip, then insert the ribbon cable
and push the clip back into place (Figure
F
).
Boot up your Raspberry Pi. Navigate to the
main menu, click on Preferences, then open
the Raspberry Pi Configuration tool. Select the
Interfaces tab and ensure that the camera is
enabled. Reboot your Raspberry Pi.
Open a Python 3 editor, such as the Thonny
Python IDE. Create a new file and save it as
cameratest.py. Enter the following code:
NOTE: Do not save any of your files as picamera.
py or you may run into some issues!
If all is well, you should see video from your
camera module on your screen for three seconds,
then be able to navigate to your Desktop and
open up testvideo.h264. You may need to delete
or change line 3 of my code if your camera is
oriented differently: the 180 in that line is the
rotation I needed to stop my video being recorded
upside down!
5. SET UP MOTION DETECTION
There are lots of ways to detect motion, but I
narrowed them to two options: using a PIR sensor
or the camera itself (Figure
G
).
A PIR (passive infrared) motion sensor detects
movement of people or animals by looking for
changes in the infrared radiation it picks up. If
you’ve ever turned on lights by jumping around
and waving your arms, you’ve probably been
waving at a PIR sensor. As cool as these motion
detecting sensors are, they aren’t great at
detecting movement through glass. As I wanted
to keep my Pi indoors for this project, looking out
through the window, I had to rule them out.
Instead, I decided to use the camera itself (plus
a little math courtesy of the numpy module) to
detect motion. The picamera module has a cool
feature called PiMotionAnalysis that compares
Helen Leigh
92 makercampus.com
F
G
M77_088-93_HelenPiece_F1.indd 92M77_088-93_HelenPiece_F1.indd 92 4/11/21 2:08 PM4/11/21 2:08 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.187