-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_lora_ble_uart.py
50 lines (35 loc) · 1.38 KB
/
example_lora_ble_uart.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
import utime
import struct
import urandom
from sx127x import TTN, SX127x
from machine import Pin, SPI
from config import *
from esp32ble import *
__DEBUG__ = True
ttn_config = TTN(ttn_config['devaddr'], ttn_config['nwkey'], ttn_config['app'], country=ttn_config['country'])
device_spi = SPI(device_config['spi_unit'], baudrate = 10000000,
polarity = 0, phase = 0, bits = 8, firstbit = SPI.MSB,
sck = Pin(device_config['sck'], Pin.OUT, Pin.PULL_DOWN),
mosi = Pin(device_config['mosi'], Pin.OUT, Pin.PULL_UP),
miso = Pin(device_config['miso'], Pin.IN, Pin.PULL_UP))
lora = SX127x(device_spi, pins=device_config, lora_parameters=lora_parameters, ttn_config=ttn_config)
frame_counter = 0
def on_receive(lora, outgoing):
payload = lora.read_payload()
print(payload)
lora.on_receive(on_receive)
lora.receive()
ble = ESP32_BLE("LoRa-BLE")
while True:
epoch = utime.time()
temperature = urandom.randint(0,30)
payload = struct.pack('@Qh', int(epoch), int(temperature))
if __DEBUG__:
print("%s: %s" % (epoch, temperature))
lora.send_data(data=payload, data_length=len(payload), frame_counter=frame_counter)
if ble.is_ble_connected:
ble.send(f'Time:{epoch}, Temperature:{temperature}')
lora.receive()
frame_counter += 1
for i in range(app_config['loop']):
utime.sleep_ms(app_config['sleep'])