TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Raspberry Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • Pi-Hacks 2
    • The Joker
    • Hologram Machine
    • Google Vision: Camera Tell
    • Yoda Tweets
    • Pi Phone
    • Darth Beats
    • Twitter Keyword Finder
    • Crimbo Lights Hack
    • Xmas Elf
    • Halloween 2016
    • Halloween Hack 2015
    • Socrative Zombie
    • Voice Translation
    • The Blue-Who Finder
    • GPIO, Twitter
    • Pi Chat Bot >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • Ferminal
    • Crypto Tracker
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me

What is it?


As a child I learnt that to see if  a Ghost is present there are a number of elements that can be checked. 1) Is there a sudden drop in the temperature? 2) Are objects moving or being moved? 3) Are the lights turned on and off ? 
​
Pimoroni’s EnviropHAT packs 4 different sensors, letting you measure temperature, pressure, light level, colour, 3-axis motion, compass heading, and analog inputs. They state that “It's ideal for monitoring conditions in your house, garage or galleon. Set up a web server with Flask and remotely monitor everything from anywhere.”

So, you can use the BMP280 temperature/pressure sensor to measure a drop in temperature, the TCS3472 light and RGB colour sensor to pick up any changes in light and then the LSM303D accelerometer/magnetometer sensor for movement, that book being flown across the room.

1) Setting up the Software


Installation is very simple, boot up your Pi, open the LX Terminal and type:

sudo apt-get update
sudo apt-get upgrade
curl -sS https://get.pimoroni.com/envirophat | bash

This downloads and installs the required software and a number of starter programs and examples to get you started.  It also includes an example program folders which you can use to get started with the EnviroPhat.

2) Capturing the Halloween Spectre


The first part of the hack is to create a function to trigger the Pi Camera.  I am using a PiNoIR Camera which enables you to capture images in the dark, perfect for spooky Halloween happenings.  To capture a good image you can use an InfaRed LED which emits IR which is reflected off of the object and then bounced back into the camera.  This means that any Werewolves, clowns or monsters will also be redcap on the camera.  I used a LISPAROI, a ring of IR LEDs which slot nicely onto the camera front.  You can read more about here.   You can buy one from here.

Create a file name as a variable which can be incremented, this is just in case you get more than one picture and saves the original file from being over written.  Set up the camera and then trigger the LISPAROI on, take the picture, save it and then turn it off.  Update the file name number so that it saves the next image with a different file name.

​def catch_a_ghost():
    global file_name
    with picamera.PiCamera() as camera:
            GPIO.output(10, GPIO.HIGH)
            #camera.start_preview() #remove
            #time.sleep(2) #remove
            print (file_name)
            camera.capture(file_name + ".jpg")
            GPIO.output(10, GPIO.LOW)
            file_name = int(file_name) + int(1)
            file_name = str(file_name)

3) Taking a Temperature Reading


A ghost is said to be present if there is a sudden drop in the temperature of the room.  Use the EnviropHAT's sensors to take a temperature reading and then compare it to the previous reading.  If there is a drop then trigger the camera, which you set up in the previous step.  Take a temperature reading with the code current_temp = weather.temperature().

​###establish the temperature of the room###
start_temp = weather.temperature()
print ("The room is ", start_temp)

try:
    while True:
        current_temp = weather.temperature()
        print ("Current Temperature is", current_temp)
        #time.sleep(8)


Later on in the code use a conditional to respond to the change in temperature, not that you are looking for a change in temperature, line 2.  For example if the room is already a chilled 12 degrees, (maybe you left the window open) then a significant drop in temp may be down to 7 degrees.  Now consider a warm room, say 24 degrees,  a noticeable drop would be 18 degrees.  

if current_temp - start_temp > 5:
            print ("GHOST")
            catch_a_ghost()
            start_temp = current_temp

Who Ya Gonna Call? 



Picture

4) Checking for Light


​If a Ghost does decide to make an appearance then it may give off some light or EMF.  Pimoroni keep the code simple and you can take a reading with the line:

amount_of_light = light.light()

This returns the measure of how much light is present in the room, a well light room is around 362 where as a dark room is around 6.  Then this value can be used use to check against a particular value, in this program 30.  If this occurs then the camera is triggered using the function you set up in step 2 

​   if amount_of_light > 30:
            print ("GHOST")
            catch_a_ghost()

5) Checking for Movement


Again checking for motion or movement is simple and can be set up with the example code from Pimoroni.

 
readings.append(motion.accelerometer().z)
        readings = readings[-4:]
        z = sum(readings) / len(readings)
        time.sleep(0.3)


Then use an IF statement to respond to the motion and trigger the camera to take a picture.  The actual EnviropHAT needs to move and this would require the ghost knocking it, but ghost float through objects so it may need to be a monster or beast which knocks the sensor.

​if last_z > 0 and abs(z-last_z) > threshold:
            print("Motion Detected!!!")
            catch_a_ghost()
            leds.on()
        last_z = z
        time.sleep(0.01)
        leds.off()

We got one!!!


The final part of the hack is to set a small sound notification if the Ghost or Monster triggers the project.  Some great ideas from Twitter users what to use, but in the end I settled for the classic Ghost Busters, "We got one!"  First install mp3123, in the LX Terminal type: sudo apt-get install mpg321 downloads and installs the required files.  Then transfer over an MP3 file to the Pi and use the simple code to stop and start the sound file.

import os
os.system('mpg321 foo.mp3 &')
time.sleep(5)
os.system(‘sudo killall mpg321’)



Download the Code from Github


Picture
Powered by Create your own unique website with customizable templates.