-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoRa_transmit_data.py
177 lines (133 loc) · 4.7 KB
/
LoRa_transmit_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# original code provided by core electronics
# Connect to The Things Network (TTN) and publish some test data
#
# How to get started:
# • Create an account on cloud.thethings.network
# • Create an application
# • Run this script to get the JoinEUI and DeviceEUI
# • On your applicaiton, generate an AppKey and paste it as a string in the code below.
# • Run this script again
# • Monitor your applicaiton console and look for a test message being received on TTN
#
# This demo is based off the Getting Started Guide by seeedstudio:
# https://wiki.seeedstudio.com/LoRa-E5_STM32WLE5JC_Module/#getting-started
#
# Refer to the AT Command Specification
# https://files.seeedstudio.com/products/317990687/res/LoRa-E5%20AT%20Command%20Specification_V1.0%20.pdf
# Put your key here (string). This should match the AppKey generated by your application.
#For example: app_key = 'E08B834FB0866939FC94CDCC15D0A0BE'
app_key = None
#Placed My Generated AppKey Here
#app_key = ''
# Regional LoRaWAN settings. You may need to modify these depending on your region.
# If you are using AU915: Australia
band='AU915'
channels='8-15'
# If you are using US915
# band='US915'
# channels='8-15'
#
# If you are using EU868
# band='EU868'
# channels='0-2'
from machine import UART, Pin
from utime import sleep_ms
from sys import exit
uart1 = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))
join_EUI = None # These are populated by this script
device_EUI = None
### Function Definitions
def receive_uart():
'''Polls the uart until all data is dequeued'''
rxData=bytes()
while uart1.any()>0:
rxData += uart1.read(1)
sleep_ms(2)
return rxData.decode('utf-8')
def send_AT(command):
'''Wraps the "command" string with AT+ and \r\n'''
buffer = 'AT' + command + '\r\n'
uart1.write(buffer)
sleep_ms(300)
def test_uart_connection():
'''Checks for good UART connection by querying the LoRa-E5 module with a test command'''
send_AT('') # empty at command will query status
data = receive_uart()
if data == '+AT: OK\r\n' : print('LoRa radio is ready\n')
else:
print('LoRa-E5 detected\n')
exit()
def get_eui_from_radio():
'''Reads both the DeviceEUI and JoinEUI from the device'''
send_AT('+ID=DevEui')
data = receive_uart()
device_EUI = data.split()[2]
send_AT('+ID=AppEui')
data = receive_uart()
join_EUI = data.split()[2]
print(f'JoinEUI: {join_EUI}\n DevEUI: {device_EUI}')
def set_app_key(app_key):
if app_key is None:
print('\nGenerate an AppKey on cloud.thethings.network and enter it at the top of this script to proceed')
exit()
send_AT('+KEY=APPKEY,"' + app_key + '"')
receive_uart()
print(f' AppKey: {app_key}\n')
def configure_regional_settings(band=None, DR='0', channels=None):
''' Configure band and channel settings'''
send_AT('+DR=' + band)
send_AT('+DR=' + DR)
send_AT('+CH=NUM,' + channels)
send_AT('+MODE=LWOTAA')
receive_uart() # flush
send_AT('+DR')
data = receive_uart()
print(data)
def join_the_things_network():
'''Connect to The Things Network. Exit on failure'''
send_AT('+JOIN')
data = receive_uart()
print(data)
status = 'not connected'
while status == 'not connected':
data = receive_uart()
if len(data) > 0: print(data)
if 'joined' in data.split():
status = 'connected'
if 'failed' in data.split():
print('Join Failed')
exit()
sleep_ms(1000)
def send_message(message):
'''Send a string message'''
send_AT('+MSG="' + message + '"')
done = False
while not done:
data = receive_uart()
if 'Done' in data or 'ERROR' in data:
done = True
if len(data) > 0: print(data)
sleep_ms(1000)
def send_hex(message):
send_AT('+MSGHEX="' + message + '"')
done = False
while not done:
data = receive_uart()
if 'Done' in data or 'ERROR' in data:
done = True
if len(data) > 0: print(data)
sleep_ms(1000)
##########################################################
#
# The main program starts here
#
##########################################################
test_uart_connection()
get_eui_from_radio()
set_app_key(app_key)
configure_regional_settings(band=band, DR='0', channels=channels)
join_the_things_network()
# Send example data
print("sending test messages")
send_message("Hello World!")
send_hex("00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF")