-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcr02_lorawan.py
111 lines (88 loc) · 3.6 KB
/
cr02_lorawan.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
"""
.. module:: cr02_lorawan
********************
CR02_LoRaWAN Module
********************
This is a module for the XinaBox LoRaWAN implementation using `CR02 <https://wiki.xinabox.cc/CR02_-_LoRa_with_ATmega328P_Core>`_ as an I2C slave device with `CW02 <https://docs.zerynth.com/latest/official/board.zerynth.xinabox_esp32/docs/index.html?highlight=cw02>`_ as its master. CR02 operates in the 868MHz range.
==============================
Using CR02_LoRaWAN Module
==============================
CR02_LoRaWAN module is dependant on a *.hex* file which must be uploaded to CR02 via IP01 before the module can be utilized. The file can be found `here <https://github.com/xinabox/arduino-LMIC/releases/tag/0.5.0>`_ accompanied with instructions on flashing it to CR02.
Once flashed, CR02 can be used as a node to connect to The Things Network.
"""
import i2c
class CR02_TTN(i2c.I2C):
"""
==================
CR02_TTN class
==================
.. class:: CR02_TTN(drvname=I2C0, addr=8, clk=100000)
Creates an intance of the CR02_TTN class.
:param drvname: I2C Bus used '( I2C0, ... )'
:param addr: Slave address, default 8
:param clk: Clock speed, default 100kHz
"""
def __init__(self, drvname=I2C0, addr=8 , clk=100000):
i2c.I2C.__init__(self, drvname, addr, clk)
self._addr = addr
try:
self.start()
except PeripheralError as e:
print(e)
raise e
def init(self, appeui, deveui, appkey, debug=False):
'''
.. method:: init()
Sends the keys generated by The Things Network (TTN) application to CR02. CR02 then establishes a connection with TTN.
Raises an exeption if any error occurs during initialization.
:param appeui: The appeui generated by TTN application
:param deveui: The deveui generated by TTN application
:param appkey: The appkey generated by TTN application
:param debug: Set True to view debugging information on CR02. Can only be used with MD03 whereby the serial communication between CW02 and CR02 is seperated.
'''
keys=[appeui,deveui,appkey]
try:
if debug == False:
self.write('0')
elif debug == True:
self.write('1')
ack = 0
for keyx, key in enumerate(keys, 0x61):
self.write_bytes(keyx)
sleep(50)
self.write(key)
sleep(100)
ack = self.read(1)
if ack == str(1):
print('keys received')
else:
print('keys not received')
except Exception as e:
print(e)
raise e
def sendString(self, dataToSend=""):
'''
.. method:: sendString(dataToSend="")
Sends a string of limit 30 bytes over the I2C bus to CR02. CR02 in turn sends the string to TTN.
Exception raised if unsuccessful.
:param dataToSend: string to be sent to TTN.
'''
try:
newData = '0' + dataToSend
self.write(newData)
except Exception as e:
print(e)
raise e
def sendNumber(self, numberToSend=0):
'''
.. method:: sendNumber(numberToSend=0)
Sends a number of limit 30 bytes over the I2C bus to CR02. CR02 in turn sends the number to TTN.
Exception raised if unsuccessful.
:param sendNumber: number to be sent to TTN.
'''
try:
newData = '0' + str(numberToSend)
self.write(newData)
except Exception as e:
print(e)
raise e