-
Notifications
You must be signed in to change notification settings - Fork 1
/
sx126x.py
259 lines (227 loc) · 8.1 KB
/
sx126x.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import RPi.GPIO as GPIO
import serial
import time
import re
class sx126x:
M0 = 22
M1 = 27
# if the header is 0xC0, then the LoRa register settings dont lost when it poweroff, and 0xC2 will be lost.
# cfg_reg = [0xC0,0x00,0x09,0x00,0x00,0x00,0x62,0x00,0x17,0x43,0x00,0x00]
cfg_reg = [0xC2,0x00,0x09,0x00,0x00,0x00,0x62,0x00,0x12,0x43,0x00,0x00]
get_reg = bytes(12)
rssi = False
addr = 65535
serial_n = ""
addr_temp = 0
#
# start frequency of two lora module
#
# E22-400T22S E22-900T22S
# 410~493MHz or 850~930MHz
start_freq = 850
#
# offset between start and end frequency of two lora module
#
# E22-400T22S E22-900T22S
# 410~493MHz or 850~930MHz
offset_freq = 18
# power = 22
# air_speed =2400
SX126X_UART_BAUDRATE_1200 = 0x00
SX126X_UART_BAUDRATE_2400 = 0x20
SX126X_UART_BAUDRATE_4800 = 0x40
SX126X_UART_BAUDRATE_9600 = 0x60
SX126X_UART_BAUDRATE_19200 = 0x80
SX126X_UART_BAUDRATE_38400 = 0xA0
SX126X_UART_BAUDRATE_57600 = 0xC0
SX126X_UART_BAUDRATE_115200 = 0xE0
SX126X_PACKAGE_SIZE_240_BYTE = 0x00
SX126X_PACKAGE_SIZE_128_BYTE = 0x40
SX126X_PACKAGE_SIZE_64_BYTE = 0x80
SX126X_PACKAGE_SIZE_32_BYTE = 0xC0
SX126X_Power_22dBm = 0x00
SX126X_Power_17dBm = 0x01
SX126X_Power_13dBm = 0x02
SX126X_Power_10dBm = 0x03
lora_air_speed_dic = {
1200:0x01,
2400:0x02,
4800:0x03,
9600:0x04,
19200:0x05,
38400:0x06,
62500:0x07
}
lora_power_dic = {
22:0x00,
17:0x01,
13:0x02,
10:0x03
}
lora_buffer_size_dic = {
240:SX126X_PACKAGE_SIZE_240_BYTE,
128:SX126X_PACKAGE_SIZE_128_BYTE,
64:SX126X_PACKAGE_SIZE_64_BYTE,
32:SX126X_PACKAGE_SIZE_32_BYTE
}
def __init__(self,serial_num,freq,addr,power,rssi,air_speed=2400,\
net_id=0,buffer_size = 240,crypt=0,\
relay=False,lbt=False,wor=False):
self.rssi = rssi
self.addr = addr
self.freq = freq
self.serial_n = serial_num
self.power = power
# Initial the GPIO for M0 and M1 Pin
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(self.M0,GPIO.OUT)
GPIO.setup(self.M1,GPIO.OUT)
GPIO.output(self.M0,GPIO.LOW)
GPIO.output(self.M1,GPIO.HIGH)
# The hardware UART of Pi3B+,Pi4B is /dev/ttyS0
self.ser = serial.Serial(serial_num,9600)
self.ser.flushInput()
self.set(freq,addr,power,rssi,air_speed,net_id,buffer_size,crypt,relay,lbt,wor)
def set(self,freq,addr,power,rssi,air_speed=2400,\
net_id=0,buffer_size = 240,crypt=0,\
relay=False,lbt=False,wor=False):
self.send_to = addr
self.addr = addr
# We should pull up the M1 pin when sets the module
GPIO.output(self.M0,GPIO.LOW)
GPIO.output(self.M1,GPIO.HIGH)
time.sleep(0.1)
low_addr = addr & 0xff
high_addr = addr >> 8 & 0xff
net_id_temp = net_id & 0xff
if freq > 850:
freq_temp = freq - 850
self.start_freq = 850
self.offset_freq = freq_temp
elif freq >410:
freq_temp = freq - 410
self.start_freq = 410
self.offset_freq = freq_temp
air_speed_temp = self.lora_air_speed_dic.get(air_speed,None)
# if air_speed_temp != None
buffer_size_temp = self.lora_buffer_size_dic.get(buffer_size,None)
# if air_speed_temp != None:
power_temp = self.lora_power_dic.get(power,None)
#if power_temp != None:
if rssi:
# enable print rssi value
rssi_temp = 0x80
else:
# disable print rssi value
rssi_temp = 0x00
# get crypt
l_crypt = crypt & 0xff
h_crypt = crypt >> 8 & 0xff
if relay==False:
self.cfg_reg[3] = high_addr
self.cfg_reg[4] = low_addr
self.cfg_reg[5] = net_id_temp
self.cfg_reg[6] = self.SX126X_UART_BAUDRATE_9600 + air_speed_temp
#
# it will enable to read noise rssi value when add 0x20 as follow
#
self.cfg_reg[7] = buffer_size_temp + power_temp + 0x20
self.cfg_reg[8] = freq_temp
#
# it will output a packet rssi value following received message
# when enable eighth bit with 06H register(rssi_temp = 0x80)
#
self.cfg_reg[9] = 0x43 + rssi_temp
self.cfg_reg[10] = h_crypt
self.cfg_reg[11] = l_crypt
else:
self.cfg_reg[3] = 0x01
self.cfg_reg[4] = 0x02
self.cfg_reg[5] = 0x03
self.cfg_reg[6] = self.SX126X_UART_BAUDRATE_9600 + air_speed_temp
#
# it will enable to read noise rssi value when add 0x20 as follow
#
self.cfg_reg[7] = buffer_size_temp + power_temp + 0x20
self.cfg_reg[8] = freq_temp
#
# it will output a packet rssi value following received message
# when enable eighth bit with 06H register(rssi_temp = 0x80)
#
self.cfg_reg[9] = 0x03 + rssi_temp
self.cfg_reg[10] = h_crypt
self.cfg_reg[11] = l_crypt
self.ser.flushInput()
for i in range(2):
self.ser.write(bytes(self.cfg_reg))
r_buff = 0
time.sleep(0.2)
if self.ser.inWaiting() > 0:
time.sleep(0.1)
r_buff = self.ser.read(self.ser.inWaiting())
if r_buff[0] == 0xC1:
pass
# print("parameters setting is :",end='')
# for i in self.cfg_reg:
# print(hex(i),end=' ')
# print('\r\n')
# print("parameters return is :",end='')
# for i in r_buff:
# print(hex(i),end=' ')
# print('\r\n')
else:
pass
#print("parameters setting fail :",r_buff)
break
else:
print("setting fail,setting again")
self.ser.flushInput()
time.sleep(0.2)
print('\x1b[1A',end='\r')
if i == 1:
print("setting fail,Press Esc to Exit and run again")
# time.sleep(2)
# print('\x1b[1A',end='\r')
GPIO.output(self.M0,GPIO.LOW)
GPIO.output(self.M1,GPIO.LOW)
time.sleep(0.1)
# the data format like as following
# "node address,frequency,payload"
# "20,868,Hello World"
def send(self,data):
GPIO.output(self.M1,GPIO.LOW)
GPIO.output(self.M0,GPIO.LOW)
time.sleep(0.1)
self.ser.write(data)
# if self.rssi == True:
# self.get_channel_rssi()
time.sleep(0.1)
def receive(self):
if self.ser.inWaiting() > 0:
time.sleep(0.5)
r_buff = self.ser.read(self.ser.inWaiting())
return (r_buff[3:-1])
def get_rssi(self):
# if self.ser.inWaiting() > 0:
# time.sleep(0.5)
# r_buff = self.ser.read(self.ser.inWaiting())
return self.get_channel_rssi()
def get_channel_rssi(self):
GPIO.output(self.M1,GPIO.LOW)
GPIO.output(self.M0,GPIO.LOW)
time.sleep(0.1)
self.ser.flushInput()
self.ser.write(bytes([0xC0,0xC1,0xC2,0xC3,0x00,0x02]))
time.sleep(0.5)
re_temp = bytes(5)
if self.ser.inWaiting() > 0:
time.sleep(0.1)
re_temp = self.ser.read(self.ser.inWaiting())
if re_temp[0] == 0xC1 and re_temp[1] == 0x00 and re_temp[2] == 0x02:
#print("the current noise rssi value: -{0}dBm".format(256-re_temp[3]))
return (256-re_temp[4])
else:
pass
# print("receive rssi value fail")
# # print("receive rssi value fail: ",re_temp)