Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lora-node-x8/src/at_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ def _run_event(self):
except:
logging.exception('_run_event')

def handle_line(self, line):
"""
Handle input from serial port, check for events.
"""
logging.debug("Method handle_line called")
if line.startswith('+'):
self.events.put(line)
def handle_packet(self, packet):
str = packet.decode(self.ENCODING, self.UNICODE_HANDLING)
if str.startswith('+'):
self.handle_line(packet.decode(self.ENCODING, self.UNICODE_HANDLING))
else:
self.responses.put(line)
self.responses.put(packet)

def handle_line(self, line):
self.events.put(line)

def handle_event(self, event):
"""
Expand Down
29 changes: 22 additions & 7 deletions lora-node-x8/src/lora-node.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,25 @@ def connection_made(self, transport):
self.transport.serial.reset_input_buffer() # Flush input data written by modem during startup

def handle_event(self, event):
if event is None:
return

"""Handle events and command responses starting with '+...'"""
if event.startswith('+RECV='):
print(event)
channel = event[7:7+1]
count = event[8:]
resp = ""
print("receive on channel " + channel + " data : " + count)
if int(count) != 0:
line = self.responses.get(timeout=10)
resp = line[2:]
print("data = ", resp.hex())
self.event_responses.put(resp)

if self._awaiting_response_for is None:
return

if event.startswith('+OK') and self._awaiting_response_for.endswith('AT'):
self.event_responses.put(event.encode())
elif event.startswith('+OK=') and self._awaiting_response_for.startswith('AT+DEV?'):
Expand Down Expand Up @@ -137,10 +155,6 @@ def handle_event(self, event):
elif event.startswith('+ERR'):
logging.error("Modem error!")
self.event_responses.put(event.encode())
elif event.startswith('+RRBDRES') and self._awaiting_response_for.startswith('AT+JRBD'):
rev = event[9:9 + 12]
mac = ':'.join('{:02X}'.format(ord(x)) for x in rev.decode('hex')[::-1])
self.event_responses.put(mac)
else:
logging.warning('unhandled event: {!r}'.format(event))

Expand Down Expand Up @@ -270,7 +284,6 @@ def application(band="EU868"):
print("Firmware version: %s" % lora_module.firmwareVersion())
print("Device EUI: %s" % lora_module.deviceEUI())
print("Setting band %s: %s" % (band, lora_module.configureBand(band)))
time.sleep(5)

lora_module.joinOTAA(SECRET_APP_EUI, SECRET_APP_KEY)
print("Welcome to TTN >>")
Expand All @@ -280,8 +293,10 @@ def application(band="EU868"):
frame.add_temperature(0, data[0])
frame.add_humidity(1, data[1])

payload = bytes(frame)
lora_module.sendBytes(payload, len(payload), False)
while True:
time.sleep(10)
payload = bytes(frame)
lora_module.sendBytes(payload, len(payload), False)

### Main program
if __name__ == '__main__':
Expand Down