From 3a74be0991a0919879abc63601ba74ddaba2b106 Mon Sep 17 00:00:00 2001 From: nathan wagner Date: Wed, 22 Dec 2021 11:50:46 -0500 Subject: [PATCH] initial commit --- .../inspectionProfiles/profiles_settings.xml | 6 + .idea/libraries/XBee_MicroPython.xml | 9 + .idea/misc.xml | 4 + .idea/modules.xml | 8 + .idea/pythonProject.iml | 17 ++ build/main.mpy | Bin 0 -> 1152 bytes main.py | 179 ++++++++++++++++++ 7 files changed, 223 insertions(+) create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/libraries/XBee_MicroPython.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/pythonProject.iml create mode 100644 build/main.mpy create mode 100644 main.py diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/libraries/XBee_MicroPython.xml b/.idea/libraries/XBee_MicroPython.xml new file mode 100644 index 0000000..d697a76 --- /dev/null +++ b/.idea/libraries/XBee_MicroPython.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..13fecf3 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e15ec35 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/pythonProject.iml b/.idea/pythonProject.iml new file mode 100644 index 0000000..fe98e73 --- /dev/null +++ b/.idea/pythonProject.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/main.mpy b/build/main.mpy new file mode 100644 index 0000000000000000000000000000000000000000..8a95021dc80bb9f6899652092dd6c2415ba2ab56 GIT binary patch literal 1152 zcmaJ5uz*-Y#6;SC01THB8!ZXe?RJ3C?IpVlfsn{- zK|**i5gzox#2@8@X=3on_~vi$OcyjtG@0ytbLKnyo%79^_akNKW6%4(6Auaurq>M3 z+DKP5TD#rYc|JTmJUV*R`*Ljf&R8m*jw>U|EgFkO<4e(~G7?+4n~Eg>rD7`&;?ZP0 zHJ(mRDM`L5iT0jIA(kKLv}u*d0!Q0r;-bFIGOcNrPAOc79kb)PgfI}SYWfDPkwLF4 z6d}4qYY_Fj$NWH#Wt*G2EerUH)cG_^NCwHCZH#uYU+UIuy;>1{^TmuXArv7PMeY3= zT>3>nkIF&I)U`^*RA>9oQt9-7>#~auQS<0X1fqKpSzUL%R*RK zofFWin|91vxsyHK6P}d1ErXDT1D0$4r4_Ov-Uc5!6oSzb+Gl4bogA^ z^QSTf16#Rwd%WmA*E}~jOC21NqA)|Q2CHvc={Za@-Lm(Prd~s2qi+_o-mU{31qC)A> zRU}Wg#i7OQ4{jq40Cd=w+}l@q-HWWAi;1(rSW7-1jG>F@5HZcVY02lW_E-ETd0QQL literal 0 HcmV?d00001 diff --git a/main.py b/main.py new file mode 100644 index 0000000..86ccbc1 --- /dev/null +++ b/main.py @@ -0,0 +1,179 @@ +# xbee python docs: http://cms.digi.com/resources/documentation/digidocs/90002219/#concepts/c_90002219_start.htm?TocPath=Digi%2520MicroPython%2520Programming%2520Guide%257C_____0 +# using pycharm w/ xbee: https://www.digi.com/resources/documentation/digidocs/90002445/default.htm +# xbee hardware pdf: https://www.digi.com/resources/documentation/digidocs/pdfs/90001543.pdf +import time +import xbee +import ubinascii +from machine import I2C +from machine import Pin +import struct +# print(" +-------------------------------------+") +# print(" +-------------------------------------+\n") + +# print("Waiting for data...\n") +# default to everything off and 0 at power on. +# in the future, we can try to reclaim previous state. +# Hopefully this lamp won't turn off very often + + +# arduino_addr = 0x48 +# senddata = 0 + +x = xbee.XBee() +#xbee.atcmd('NT', 0xFF) +#tp = xbee.atcmd('TP')1A +print(xbee.atcmd('MY')) +print("transmitting") +srcaddr = int(xbee.atcmd('MY')) +print(xbee.atcmd('AI')) +xbee.atcmd('CN') +print(srcaddr) +srcarry = srcaddr.to_bytes(2,"big") +print(srcarry) +initial_payload=bytes([171, srcarry[1], srcarry[0], 141, 194, 209, 65, 0, 162, 19, 0, 142]) +leave_load=bytes([171, 141, 194, 209, 65, 0, 162, 19, 0, 2]) +print(initial_payload) +#xbee.transmit(xbee.ADDR_COORDINATOR, b'\xAB\xF7\x6A\x8D\xC2\xD1\x41\x00\xA2\x13\x00\x8E', source_ep=0, dest_ep=0, cluster=19, profile=0, tx_options=0) +#xbee.transmit(xbee.ADDR_COORDINATOR, b'\xAB\xF7\x6A\x8D\xC2\xD1\x41\x00\xA2\x13\x00\x8E', source_ep=0, dest_ep=0, cluster=19, profile=0, tx_options=0) +send=0 +lame = 0 +while lame < 3: + xbee.receive() + lame += 1 + +while send==0: + try: + if xbee.transmit(xbee.ADDR_COORDINATOR, leave_load, source_ep=0, dest_ep=0, cluster=52, profile=0, tx_options=0) is None: + send = 1 + print("leaving") + except OSError as e: + print("leaving transmit error") + send=1 +send=0 +time.sleep(1) +while send==0: + try: + if xbee.transmit(xbee.ADDR_COORDINATOR, initial_payload, source_ep=0, dest_ep=0, cluster=19, profile=0, tx_options=0) is None: + send = 1 + print("joining") + except OSError as e: + print("joining transmit error") +print("receiving") +while 1 != 0: + blorp = xbee.receive() + if blorp is not None: + if blorp['cluster']==5: + print(bytes(blorp['payload'])) + b = bytearray(blorp['payload']) + print(b[0]) + payload=bytes([b[0], 00, b[1], b[2], 1, 8]) + xbee.transmit(xbee.ADDR_COORDINATOR,payload,source_ep=0,dest_ep=0,cluster=32773, profile=0, tx_options=0) + if blorp['cluster']==4: + print(bytes(blorp['payload'])) + b = bytearray(blorp['payload']) + print(b[0]) + payload = bytes([b[0], 00, b[1], b[2], 14, 8, 4, 1, 2, 0, 6, 3, 0, 0, 3, 0, 6, 0, 0]) + xbee.transmit(xbee.ADDR_COORDINATOR, payload, source_ep=0,dest_ep=0,cluster=32772, profile=0, tx_options=0) + if blorp['cluster'] == 0: + print(bytes(blorp['payload'])) + b = bytearray(blorp['payload']) + for x in b: + print(x) + print(blorp) + +#print(xbee.receive()) +#print("temperature") +#print(tp) +#if tp > 0x7FFF: +# tp = tp - 0x10000 +# print("The XBee is %.1F degrees" % (tp * 9.0 / 5.0 + 32.0)) +#for i in list(xbee.discover()): +# print(i) +#while True: + # Check if the XBee has any message in the queue. + # received_msg = xbee.receive() +# ad0 = Pin("D0", Pin.IN, Pin.PULL_UP) +# ad1 = Pin("D1", Pin.IN, Pin.PULL_UP) +# ad2 = Pin("D2", Pin.IN, Pin.PULL_UP) +# if ad0.value() == 0: +# print("input pin 0") +# if ad1.value() == 0: +# print("input pin 1") +# if ad2.value() == 0: +# print("input pin 2") +# #print("just a print, lots and lots") +# # if received_msg: + # Get the sender's 64-bit address and payload from the received message. +# sender = received_msg['sender_eui64'] +# payload = received_msg['payload'] +# Check to make sure data is coming from zigbee controller +# if sender == b'\x00!.\xff\xff\x06\rW': +# is an on/off command +# if len(payload) == 3: +# if payload[2] == 0: +# turn lamp off +# lamp_ha_state['on_off'] = 0 +# lamp_ha_bytes[1] = 0 +# lamp_ha_bytes[0] = 1 +# senddata = 1 +# elif payload[2] == 1: +# # turn lamp on +# lamp_ha_state['on_off'] = 1 +# lamp_ha_bytes[1] = 1 +# lamp_ha_bytes[0] = 1 +# senddata = 1 +# else: +# did not receive 1 or 0 +# print("did not receive 1 or 0") +# pass +# brightness, color, or color temp command +# if len(payload) > 3: +# is brightness command +# if payload[2] == 4: +# Home Assistant automatically turns the switch 'on' if brightness +# is edited. Therefore, also set on_off to 1 to make sure to match +# Home Assistant +# print("brightness command") +# lamp_ha_state['on_off'] = 1 +# lamp_ha_state['brightness'] = payload[3] +# lamp_ha_bytes[1] = 1 +# lamp_ha_bytes[2] = payload[3] +# lamp_ha_bytes[0] = 2 +# senddata = 1 +# is color command +# elif payload[2] == 7: +# print("color command") +# lamp_ha_state['color_x_raw'] = (payload[4] << 8) | payload[3] +# lamp_ha_bytes[4] = payload[4] +# lamp_ha_bytes[5] = payload[3] +# lamp_ha_state['color_y_raw'] = (payload[6] << 8) | payload[5] +# lamp_ha_bytes[6] = payload[6] +# lamp_ha_bytes[7] = payload[5] +# lamp_ha_bytes[0] = 2 +# turn lamp on +# lamp_ha_state['on_off'] = 1 +# lamp_ha_bytes[1] = 1 +# lamp_ha_bytes[0] = 1 # not necessary +# senddata = 1 +# is color temp command +# elif payload[2] == 10: +# print("color temp command") +# pass +# is unknown data +# else: +# print("unknown data received %s" % ubinascii.hexlify(payload)) +# pass +# print("good sender found") +# print(lamp_ha_state) +# print(lamp_ha_bytes) +# print(lamp_ha_state['color_x_raw']/65535) +# print(lamp_ha_state['color_y_raw'] / 65535) +# print("===================") +# if senddata == 1: +# i2c.writeto(arduino_addr, bytearray(lamp_ha_bytes), True) +# lamp_ha_bytes[0] = 0 +# senddata = 0 +# print(len(payload)) +# print(ubinascii.hexlify(payload)) +# print("Data received from %s >> %s" % (''.join('{:02x}'.format(x).upper() for x in sender), +# payload.decode()))