Skip to content

Commit 25dbc12

Browse files
lora: Add example
Signed-off-by: Francois Berder <francois.berder@imgtec.com>
1 parent bf2c44d commit 25dbc12

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

examples/lora_example.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
"""This example shows how to use the Lora Click wrapper of the LetMeCreate to
3+
send or receive data.
4+
5+
Depending on the mode selected, it either continuously sends a message every
6+
second, or it waits for data and prints what it receives.
7+
The user has to interrupt the program to exit it by pressing Ctrl+C.
8+
9+
To run the example in send mode:
10+
$ ./lora_example.py s
11+
12+
To run the example in receive mode:
13+
$ ./lora_example.py r
14+
15+
The Lora Click must be inserted in Mikrobus 1 before running this program.
16+
"""
17+
18+
from letmecreate.core.common import MIKROBUS_1
19+
from letmecreate.click import lora
20+
from letmecreate.core import uart
21+
from letmecreate.core.uart import UART_BD_57600
22+
from time import sleep
23+
import sys
24+
25+
def receive():
26+
buffer = lora.receive(16)
27+
buffer = bytes(buffer).decode('utf-8')
28+
print(buffer)
29+
30+
def send(n):
31+
buffer = 'Hello, World! {}'.format(n)
32+
lora.send(buffer.encode('utf-8'))
33+
print(buffer)
34+
35+
if len(sys.argv) < 2:
36+
print('{} (r|s)'.format(sys.argv[0]))
37+
sys.exit(-1)
38+
39+
mode = sys.argv[1]
40+
if mode != 'r' and mode != 's':
41+
print('Invalid mode.')
42+
sys.exit(-1)
43+
44+
print('Press Ctrl+C to exit program')
45+
46+
uart.init()
47+
uart.select_bus(MIKROBUS_1)
48+
uart.set_baudrate(UART_BD_57600)
49+
50+
config = lora.get_default_configuration()
51+
lora.init(MIKROBUS_1, config)
52+
53+
n = 0
54+
while True:
55+
if mode == 's':
56+
send(n)
57+
sleep(1)
58+
elif mode == 'r':
59+
receive()
60+
61+
n += 1
62+
63+
64+
uart.release()

0 commit comments

Comments
 (0)