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

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:

# 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))

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

  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)

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.

  # read/send door sensor
  if door_sensor.value:
    print('Door Open!')
    # change indicator block to red
    aio.send(door_feed.key, 3)
    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)
    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())

  with open("picam_img.jpg", "rb") as imageFile:
    str = base64.b64encode(
      aio.send('picam', str)
      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.

  # 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):

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

# Set to the hour at which to arm the alarm system, 24hr time

# Set to the interval between loop execution, in seconds

To run the script, enter:

sudo python3

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:

Adafruit IO Home: Security
CO2eq = 400 ppm 	 TVOC = 0 ppb
Door Closed.
Motion detected!
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 Aug 27, 2018.

Text editor powered by tinymce.