Skip to content

Instantly share code, notes, and snippets.

@donghee
Forked from clive2000/RN2903.py
Created November 16, 2017 07:47
Show Gist options
  • Save donghee/eb62c63550b6d0e9c5b9028d11e830e0 to your computer and use it in GitHub Desktop.
Save donghee/eb62c63550b6d0e9c5b9028d11e830e0 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
#
# Example using python and serial to control Microchip Tech. Mote
#
# requires pyserial: pip install pyserial
#
import serial
import sys
from time import sleep
import platform
if platform.system() == 'Windows':
serialMC = serial.Serial('COM4', 57600, timeout=1.0)
else:
serialMC = serial.Serial('/dev/ttyACM0', 57600, timeout=1.0)
def sendCmd(cmd):
#print(" flush receive\r\n")
waitForResp()
print((" sendCmd -> %s\r\n" % repr(cmd)))
serialMC.write(b'%s\r\n' % cmd)
serialMC.flush()
return waitForResp()
def setValue(cmd):
status = False
data = sendCmd(cmd)
if len(data) > 0:
if data[0] == "ok":
print(" setValue: ok")
status = True
else:
print(" setValue: failed")
return status
def waitForResp(sizeBytes=100):
rcv = serialMC.read(sizeBytes)
#print " waitForResp -> %s" % repr(rcv)
data = rcv.split("\r\n")
while '' in data:
data.remove('')
if len(data) > 0:
print((" waitForResp (%d) -> %s" % (len(data), repr(data))))
return data
def setSubBand(subBand):
if subBand >= 1 and subBand <= 8:
for channel in range(0, 64):
chConfig = "off"
if channel >= (subBand - 1) * 8 and channel < (subBand * 8):
chConfig = "on"
status = setValue("mac set ch status %d %s" % (channel, chConfig))
if status is False:
print("Error, was not set")
def join():
status = False
while status is False:
#setValue("mac set dr 1") # (125kHz, SF9)
status = setValue("mac join otaa")
if status: # got ok back
status = False
data = []
while len(data) < 1:
data = waitForResp()
if len(data) > 0:
if data[0] == "accepted":
print("joined!")
status = True
break
print "Join Failed - Sleep 20s and try again"
sleep(20)
def sendData(data, port=1, confirmed=False):
cnfString = "uncnf"
if confirmed:
cnfString = "cnf"
status = setValue("mac tx %s %d %x" % (cnfString, port, data))
if status:
status = False
data = []
while len(data) < 1:
data = waitForResp()
if len(data) > 0:
if data[0] == "mac_tx_ok": # successful
status = True
break
elif data[0].find("max_rx") >= 0: # data - TODO handle...
continue
# Failed
# mac_err if unsuccessful, ACK not received from server.
# or other errors (see reference)
else:
status = False
break
return status
def setupNetwork():
data = sendCmd('sys get hweui')
if len(data) >= 0:
devEUI = int(data[0], 16)
print(("devEUI = 0x%016x" % devEUI))
sendCmd('mac set deveui %016X' % devEUI)
else:
print("No devEUI")
sys.exit(1)
data = sendCmd('mac get deveui')
if len(data) >= 0:
devEUI = int(data[0], 16)
print(("devEUI = 0x%016x" % devEUI))
else:
print("No devEUI")
sys.exit(1)
setValue("mac set appeui E8BE21C0DA000001")
setValue("mac set appkey 42148B6A74AAAAABBDEB74958B555554")
setValue("mac save")
setSubBand(7)
def init():
#sendCmd('sys reset') # sys reset seems to cause issues
sendCmd('mac reset')
def main():
init()
setupNetwork()
setValue("mac set pwridx 8") # highest power valid 5-10 (calculated
# 30dBm-2*poweridx
#setValue("mac set dr 0") # slowest datarate (125kHz, SF10)
setValue("mac set dr 1") # slowest datarate (125kHz, SF9)
join()
sleep(10)
setValue("mac set retx 10") # 10 retries
#setValue("mac set retx 2") # 2 retries
sleepTime = 0
while 1:
setValue("mac set dr 2") # datarate (125kHz, SF8)
status = sendData(0x150D20, port=1, confirmed=True)
if status:
print("Successfully Sent Data!")
sleepTime = 5 * 60 # 5 minutes on success
#sleepTime = 30 # 30 seconds on success
else:
print("Failed to Send Data!")
sleepTime = 20 # 20 seconds on failure
sleep(sleepTime)
###############################################################################
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment