-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathreceiver_timeout.py
73 lines (58 loc) · 2.54 KB
/
receiver_timeout.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
import os, sys
currentdir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.dirname(os.path.dirname(currentdir)))
from LoRaRF import SX127x, LoRaSpi, LoRaGpio
import time
# Begin LoRa radio with connected SPI bus and IO pins (cs and reset) on GPIO
# SPI is defined by bus ID and cs ID and IO pins defined by chip and offset number
spi = LoRaSpi(0, 0)
cs = LoRaGpio(0, 8)
reset = LoRaGpio(0, 24)
LoRa = SX127x(spi, cs, reset)
print("Begin LoRa radio")
if not LoRa.begin() :
raise Exception("Something wrong, can't begin LoRa radio")
# Set frequency to 915 Mhz
print("Set frequency to 915 Mhz")
LoRa.setFrequency(915000000)
# Set RX gain to boosted gain
print("Set RX gain to boosted gain")
LoRa.setRxGain(LoRa.RX_GAIN_BOOSTED, LoRa.RX_GAIN_AUTO)
# Configure modulation parameter including spreading factor (SF), bandwidth (BW), and coding rate (CR)
print("Set modulation parameters:\n\tSpreading factor = 7\n\tBandwidth = 125 kHz\n\tCoding rate = 4/5")
LoRa.setSpreadingFactor(7)
LoRa.setBandwidth(125000)
LoRa.setCodeRate(5)
# Configure packet parameter including header type, preamble length, payload length, and CRC type
print("Set packet parameters:\n\tExplicit header type\n\tPreamble length = 12\n\tPayload Length = 15\n\tCRC on")
LoRa.setHeaderType(LoRa.HEADER_EXPLICIT)
LoRa.setPreambleLength(12)
LoRa.setPayloadLength(15)
LoRa.setCrcEnable(True)
# Set syncronize word for public network (0x34)
print("Set syncronize word to 0x34")
LoRa.setSyncWord(0x34)
print("\n-- LoRa Receiver Timeout --\n")
# Receive message continuously
while True :
# Request for receiving new LoRa packet within 1000 ms
LoRa.request(1000)
# Wait for incoming LoRa packet
LoRa.wait()
# Only show message if receive process is done
status = LoRa.status()
if status == LoRa.STATUS_RX_DONE :
# Put received packet to message and counter variable
message = ""
while LoRa.available() > 1 :
message += chr(LoRa.read())
counter = LoRa.read()
# Print received message and counter in serial
print(f"{message} {counter}")
# Print packet/signal status including RSSI, SNR, and signalRSSI
print("Packet status: RSSI = {0:0.2f} dBm | SNR = {1:0.2f} dB".format(LoRa.packetRssi(), LoRa.snr()))
else :
# Show received status in case timeout or CRC or header error occur
if status == LoRa.STATUS_RX_TIMEOUT : print("Receive timeout")
elif status == LoRa.STATUS_CRC_ERR : print("CRC error")
elif status == LoRa.STATUS_HEADER_ERR : print("Packet header error")