Advertisement
silver2row

XBee!

Oct 19th, 2019
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. # Copyright 2017, Digi International Inc.
  2. #
  3. # Permission to use, copy, modify, and/or distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14.  
  15. from digi.xbee.devices import XBeeDevice
  16. from digi.xbee.io import IOLine, IOMode
  17. import time
  18. import threading
  19.  
  20. # TODO: Replace with the serial port where your local module is connected to.
  21. PORT = "/dev/ttyS2"
  22. # TODO: Replace with the baud rate of your local module.
  23. BAUD_RATE = 9600
  24.  
  25. REMOTE_NODE_ID = "Xbee_A"
  26.  
  27. IOLINE_IN = IOLine.DIO3_AD3
  28. IOLINE_OUT = IOLine.DIO4_AD4
  29.  
  30.  
  31. def main():
  32.     print(" +-----------------------------------------------+")
  33.     print(" | XBee Python Library Get/Set Remote DIO Sample |")
  34.     print(" +-----------------------------------------------+\n")
  35.  
  36.     stop = False
  37.     th = None
  38.  
  39.     local_device = XBeeDevice(PORT, BAUD_RATE)
  40.  
  41.     try:
  42.         local_device.open()
  43.  
  44.         # Obtain the remote XBee device from the XBee network.
  45.         xbee_network = local_device.get_network()
  46.         remote_device = xbee_network.discover_device(REMOTE_NODE_ID)
  47.         if remote_device is None:
  48.             print("Could not find the remote device")
  49.             exit(1)
  50.  
  51.         def io_detection_callback():
  52.             while not stop:
  53.                 # Read the digital value from the input line.
  54.                 io_value = remote_device.get_dio_value(IOLINE_IN)
  55.                 print("%s: %s" % (IOLINE_IN, io_value))
  56.                
  57.                 # Set the previous value to the local output line.
  58.                 local_device.set_dio_value(IOLINE_OUT, io_value)
  59.                
  60.                 time.sleep(0.2)
  61.  
  62.         th = threading.Thread(target=io_detection_callback)
  63.  
  64.         remote_device.set_io_configuration(IOLINE_IN, IOMode.DIGITAL_IN)
  65.  
  66.         local_device.set_io_configuration(IOLINE_OUT, IOMode.DIGITAL_OUT_LOW)
  67.  
  68.         time.sleep(0.5)
  69.         th.start()
  70.  
  71.         input()
  72.  
  73.     finally:
  74.         stop = True
  75.         if th is not None and th.is_alive():
  76.             th.join()
  77.         if local_device is not None and local_device.is_open():
  78.             local_device.close()
  79.  
  80.  
  81. if __name__ == '__main__':
  82.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement