This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
iotloranode.py
251 lines (196 loc) · 6.78 KB
/
iotloranode.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
# IOT LoRa Node Python Library - Used to interface to RAK811 Boards
# with Micropython/Python 3.X
# Copyright (C) 2018 Pi Supply
# Written by Ryan Walmsley (Ryan@pi-supply.com)
# Designed for Raspberry Pi
import binascii
from time import sleep
class loraNode:
"""RAK811 Interface Library - Converts inputs"""
"""given to serial commands for RAK811"""
loraNodeSerialBaud = 115200
serialLib = 0
serial_write = None
serial_read = None
serLib = None
abp = "abp"
otaa = "otaa"
EU868 = "EU868"
US915 = "US915"
KR920 = "KR920"
AU915 = "AU915"
AS923 = "AS923"
IN865 = "IN865"
SpreadFactors = {7:5,8:4,9:3,10:2,11:1,12:0}
def __init__(self, region=1):
"""Initialise The Library and connect to the module"""
# Import the serial library for the respective platform
import serial
from RPi import GPIO
GPIO.setwarnings(False)
self.serLib = serial.Serial("/dev/serial0", self.loraNodeSerialBaud)
self.serial_write = self.serLib.write
self.serial_read = self.serLib.readline
self.serialLib = 1
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.OUT)
GPIO.output(17,0)
sleep(0.01)
GPIO.output(17,1)
self.serLib.readline()
self.serLib.readline()
self.serLib.readline()
self.set_spreadingFactor(7)
self.serLib.readline()
############
# UART Functions
############
def uart_tx(self, command):
"""Takes the command and sends it via UART via the correct library"""
# First add at to the beginning
command = "at+%s\r\n" % command
#print(command)
self.serial_write(str.encode(command))
line = self.serLib.readline()
#print(line)
return line
def uart_rx(self):
line = self.serLib.readline()
#print(line)
return line
"""Returns serial data"""
############
# Set Settings Functions
############
def set_devAddr(self, devAddr):
"""Set Device Address"""
command = "set_config=dev_addr:%s" % devAddr
self.uart_tx(command)
def set_devEUI(self, devEui):
"""Set Device EUI"""
command = "set_config=dev_eui:%s" % devEui
self.uart_tx(command)
def set_appEUI(self, appEui):
"""Set Application EUI"""
command = "set_config=app_eui:%s" % appEui
self.uart_tx(command)
def set_appKey(self, appKey):
"""Set Application Key"""
command = "set_config=app_key:%s" % appKey
self.uart_tx(command)
def set_networkKey(self, nwsk):
"""Set Network Key"""
command = "set_config=nwks_key:%s" % nwsk
self.uart_tx(command)
def set_appSessionKey(self, apsk):
"""Set Application Session Key"""
command = "set_config=apps_key:%s" % apsk
self.uart_tx(command)
def set_loraPower(self, pwr):
"""Set Lora Power Level"""
command = "set_config=pwr_level:%s" % pwr
self.uart_tx(command)
def set_adrMode(self, adr):
"""Set Adaptive Data Rate"""
command = "set_config=adr:%s" % adr
self.uart_tx(command)
def set_spreadingFactor(self, sf):
"""Set Spreading Factor"""
command = "dr=%s" % self.SpreadFactors[sf]
self.uart_tx(command)
def set_region(self, sf):
"""Set LoRaWAN Region"""
command = "band=%s" % sf
self.uart_tx(command)
############
# Get Settings Functions
############
def get_devAddr(self):
"""Get Device Address"""
command = "get_config=dev_addr"
return str(self.uart_tx(command)).split("OK")[1].split("\\")[0]
def get_devEUI(self):
"""Get Device EUI"""
command = "get_config=dev_eui"
return str(self.uart_tx(command)).split("OK")[1].split("\\")[0]
def get_appEUI(self):
"""Get Application EUI"""
command = "get_config=app_eui"
return str(self.uart_tx(command)).split("OK")[1].split("\\")[0]
def get_appKey(self):
"""Get Application Key"""
command = "get_config=app_key"
return str(self.uart_tx(command)).split("OK")[1].split("\\")[0]
def get_networkKey(self):
"""Get Network Key"""
command = "get_config=nwks_key"
return str(self.uart_tx(command)).split("OK")[1].split("\\")[0]
def get_appSessionKey(self):
"""Get Application Session Key"""
command = "get_config=apps_key"
return str(self.uart_tx(command)).split("OK")[1].split("\\")[0]
def get_loraPower(self):
"""Get Lora Power Level"""
command = "get_config=pwr_level"
return str(self.uart_tx(command)).split("OK")[1].split("\\")[0]
def get_adrMode(self):
"""Get Adaptive Data Rate"""
command = "get_config=adr"
return str(self.uart_tx(command)).split("OK")[1].split("\\")[0]
def get_spreadingFactor(self):
"""Get Spreading Factor"""
command = "get_config=dr"
return str(self.uart_tx(command)).split("OK")[1].split("\\")[0]
############
# LoRa Functions
############
def join(self, mode):
"""Join"""
if(mode == self.abp):
command = "join=abp"
self.uart_tx(command)
elif(mode == self.otaa):
command = "join=otaa"
self.uart_tx(command)
# Extra response from otaa join
line = self.serLib.readline()
return True
def send_raw_packet(self, packet, port):
"""Send raw bytes packet"""
def send_string_packet(self, string, port=1, pktType=0):
"""Send a string packet"""
command = "send=%s,%s,%s" % (pktType, port,
binascii.hexlify(string.encode())
.decode("utf-8"))
self.uart_tx(command)
# There will be an extra response
line = self.serLib.readline()
return line
def send_int_packet(self, int, port):
"""Send integer packet"""
def recieve_packet(self):
"""Check To See if there is any response"""
def reset_radio(self):
"""Reset the RAK811 Radio Module"""
command = "reset=0"
self.uart_tx(command)
self.uart_rx()
self.uart_rx()
self.uart_rx()
def lora_mode(self, mode):
"""Change between LoRaWAN & LoRaP2P Modes"""
def lora_band(self, band):
"""Set LoRaWAN Region"""
command = "band="+band
self.uart_tx(command)
############
# GPIO Functions
############
def gpio_setmode(self, pin, mode):
"""Set Pin GPIO Mode"""
def gpio_read(self, pin):
"""Read RAK811 GPIO"""
def gpio_write(self, pin, state):
"""RAK811 GPIO Write Command"""
def gpio_adc(self, pin):
"""Read RAK811 ADC"""