Simple Garage Door Sensor

Digi International
July 26, 2017

Table of Contents

 

  1. Introduction
  2. Assembly of Parts
  3. Configure the Radio
  4. View It
  5. Use It
  6. Intern Spotlight

1)Introduction

Whether you’re pulling into work after a busy morning, driving to the family cabin, or just laying in bed after a long day, the last thing you want to worry about is your car garage door. This example will show you how to install a simple garage door monitor so you never have to worry if your garage door is left open. Using the Digi XBee LTE Cat1 Development Kit and a magnetic reed switch, an SMS can be sent to your phone notifying you if your garage door is opened or closed.

2)Assembly of Parts

3)Connect the Hardware

  1. Make sure your device is setup correctly and you have downloaded XCTU or have access to a different terminal. (For instructions on setting up your XBee Cellular device click here. To download XCTU click here.)
  2. Connect the reed switch to the Digi XBee using a header or another method. Do so by connecting one of its leads to ground and the other to DIO8 (Note: DIO8 was chosen arbitrarily, you can connect the wire to a different DIO but you must remember to modify door_pin in the code below if you do so).
  3. Attach the reed switch magnet to the side of your garage door. Then secure the reed switch to the wall, so that it is even with the magnet attached to the garage door. The reed switch should be affixed such that the magnet and the sensor are aligned when the door is closed and separated when the door is open.
  4. Connect the Digi XBee to your computer and open XCTU.
  5. Click the Add Radio Device  button and then select the XBC LTE Cat1 device from the Radio Modules list. Make sure the device is updated to the most current firmware settings.
  6. Click the Configuration Working Mode  button and change AP to [4]. Write this setting onto your Digi XBee by clicking the Write  button.
  7. In the Tools  drop-down menu select MicroPython Terminal and then click Open  to enable communication with your XBC device.
  8. Copy the code below into the MicroPython terminal using paste mode. (Note: to paste text in text into the terminal while in paste mode type Ctrl+shift+v)
    import time
    import network
    from machine import Pin
    door_pin = Pin('D8', Pin.IN, Pin.PULL_UP)
    cell = network.Cellular()
    def isclosed():
      return not door_pin()
    def timestamp():
      return '{:04}-{:02}-{:02} {:02}:{:02}:{:02}'.format(*time.localtime()[:6])
    print('Waiting for SMS from remote number...')
    sms = None
    while not sms:
      sms = cell.sms_receive()
      time.sleep(1)
    phone = sms["sender"]
    def send(msg):
      print(msg)
      cell.sms_send(phone, msg)
    def notify(isclosed):
      state = 'closed' if isclosed else 'opened' 
      msg = '[{}]\nThe garage door {}.'.format(timestamp(), state)
      send(msg)
    send('Connected to {}.'.format(phone))
    wasclosed = isclosed()
    while True:
      notify(wasclosed)
      while isclosed() == wasclosed:
        time.sleep(1)
      wasclosed = not wasclosed
  9. When you run the program “Waiting for SMS from remote number…” will be printed to the terminal. At this prompt, use your phone to send a SMS message to the phone number associated with your Digi XBee. (To find your Digi XBee’s phone number click the Configuration Working Mode button and phone number is listed under PH in the Cellular section.)
  10. The garage door sensor will now text you anytime the door is opened or closed.

4)View It

Watch the video below to see the garage door monitor in action!

Note: This project was built as a proof of concept. If you plan to install this device into your own garage, it is recommended that you mount your Digi XBee onto the wall and put an enclosure around the device for protection.

5)Use It

This project is intended to be a simple use-case example to highlight features of the Digi XBee. Click here to see how the Digi Xbee is used for large-scale industrial applications.

6)Intern Spotlight

This project was created by Alex Robbins, an Engineering Intern at Digi. Alex is a University of Minnesota Twin Cities student who will graduate with a bachelor’s degree in Computer Engineering next spring. After he graduates, Alex hopes to work with embedded systems or computer architecture.

Alex heard about his internship at Digi when attending the University of Minnesota’s College of Science and Engineering career fair. This summer Alex has spent most of his time contributing to Digi’s XBee host library and working with MicroPython. He has most enjoyed being able to do work in a field that interests him.