Python Code

The first chunk of our code sets up the Adafruit IO Feeds:

Download: file
tvoc_feed = aio.feeds('tvoc')
eco2_feed = aio.feeds('eco2')
door_feed = aio.feeds('front-door')
motion_feed = aio.feeds('motion-detector')
alarm_feed = aio.feeds('home-alarm')
outdoor_lights_feed = aio.feeds('outdoor-lights')
indoor_lights_Feed = aio.feeds('indoor-lights')
picam_feed = aio.feeds('picam')

Then, it sets up the camera, sensors, and NeoPixel Strip:

Download: file
# set up PiCamera
camera = picamera.PiCamera()
camera.resolution = (320, 240)

# set up door sensor
door_sensor = digitalio.DigitalInOut(D5)
door_sensor.direction = digitalio.Direction.INPUT

# set up motion sensor
pir_sensor = digitalio.DigitalInOut(D6)
pir_sensor.direction = digitalio.Direction.INPUT
prev_pir_value = pir_sensor.value
is_pir_activated = False

# set up sgp30
i2c_bus = I2C(SCL, SDA, frequency=100000)
sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c_bus)

# set up the neopixel strip
pixels = neopixel.NeoPixel(D18, NUM_PIXELS_STRIP)
pixels.fill((0, 0, 0))
pixels.show()

In the loop (while True), we first take measurements for the co2eq and tvoc from the SGP30 and send those values to Adafruit IO.

Download: file
  co2eq, tvoc = sgp30.iaq_measure()
  print("CO2eq = %d ppm \t TVOC = %d ppb" % (co2eq, tvoc))
  # send SGP30 values to Adafruit IO
  aio.send(eco2_feed.key, co2eq)
  aio.send(tvoc_feed.key, tvoc)
  time.sleep(0.5)

Next, the code reads the door and motion sensor. If the door is open, or if motion is detected, a value of 3 is sent the corresponding feed to change the indicator block's color. For the motion detector, we also keep track of the motion using a boolean, is_pir_activated.

Download: file
  # read/send door sensor
  if door_sensor.value:
    print('Door Open!')
    # change indicator block to red
    aio.send(door_feed.key, 3)
  else:
    print('Door Closed.')
    # reset indicator block to green
    aio.send(door_feed.key, 0)
  # read/send motion sensor
  if door_sensor.value:
    if not prev_pir_value:
      print('Motion detected!')
      is_pir_activated = True
      # change indicator block to red
      aio.send(motion_feed.key, 3)
  else:
    if prev_pir_value:
      print('Motion ended.')
      is_pir_activated = False
      # reset indicator block to green
      aio.send(motion_feed.key, 0)

Next, we'll take a picture with the picam (using camera.capture()), convert it to a base64-encoded string (for use with the image block on an Adafruit IO Dashboard), and send it to Adafruit IO (aio.send())

Download: file
  camera.capture('picam_img.jpg')
  print('snap!')
  with open("picam_img.jpg", "rb") as imageFile:
    str = base64.b64encode(imageFile.read())
    try:
      aio.send('picam', str)
    except:
      print('Sending camera image failed...')

Finally, we check if the alarm toggle block on the dashboard has been enabled. If it has been, we take a sample of the current hour (according to the Pi's internal clock. You can set this in raspi-config->Internationalization Options->Change Time Zone). Then, if the hour is later than the ALARM_HOUR variable and if the PIR has been triggered - we call the alarm_trigger() method.

Download: file
  # Alarm System 
  is_alarm = aio.receive(alarm_feed.key)
  if (is_alarm.value == "ON"):
    # sample the current hour
    cur_time = time.localtime()
    cur_hour = time.tm_hour
    if (cur_hour > ALARM_HOUR and is_pir_activated == True):
      alarm_trigger()

The alarm toggle on your Adafruit IO dashboard is dependent on if the alarm set in the ALARM_HOUR variable. You can also set the delay between executing the loop (while True), LOOP_INTERVAL

Download: file
# Set to the hour at which to arm the alarm system, 24hr time
ALARM_HOUR = 16

# Set to the interval between loop execution, in seconds
LOOP_INTERVAL = 2

To run the script, enter:

sudo python3 io_home_security.py

Note: You'll need to prefix the script with sudo to write to the NeoPixels.

If everything went correctly, you should see the following in your terminal:

Download: file
Adafruit IO Home: Security
CO2eq = 400 ppm 	 TVOC = 0 ppb
Door Closed.
Motion detected!
snap!
sent to AIO!

Let's interact with our home from the IO-Home Dashboard! From your Adafruit IO account, select the IO-Home dashboard and push the door open. You should see the Front Door Indicator change from green to red.

Switch the toggle from OFF to ON to arm your house's security system. When you push the front door open (or move past the PIR sensor past the time set by alarmHour), the inside lights will blink red.

Tired of the alarm? Want some peace and quiet? Toggle the alarm OFF.

If you set up the Raspberry Pi Camera, you'll also be able to see the camera feed from the Image Block element created in Python Setup

This guide was first published on Sep 05, 2018. It was last updated on Sep 05, 2018. This page (Python Code) was last updated on Dec 05, 2019.