Home/ Blog/Examples & Guides/Simple Garage Door Sensor

Simple Garage Door Sensor

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.
Tags:
Posted on:


Comments are closed.

Contact a Digi expert and get started today! Contact Us
Have a Question?