rfm9x set low_datarate_optimize automatically - revize sf examples

This commit is contained in:
jerryneedell 2025-01-28 08:27:57 -05:00
parent 96682237ab
commit f2ff1e48ab
3 changed files with 91 additions and 32 deletions

View file

@ -426,6 +426,11 @@ class RFM9x(RFMSPI):
else:
self.write_u8(0x2F, 0x44)
self.write_u8(0x30, 0)
# set low_datarate_optimize for signal duration > 16 ms
if 1000 / (self.signal_bandwidth / (1 << self.spreading_factor)) > 16:
self.low_datarate_optimize = 1
else:
self.low_datarate_optimize = 0
@property
def coding_rate(self) -> Literal[5, 6, 7, 8]:
@ -473,6 +478,11 @@ class RFM9x(RFMSPI):
_RF95_REG_1E_MODEM_CONFIG2,
((self.read_u8(_RF95_REG_1E_MODEM_CONFIG2) & 0x0F) | ((val << 4) & 0xF0)),
)
# set low_datarate_optimize for signal duration > 16 ms
if 1000 / (self.signal_bandwidth / (1 << self.spreading_factor)) > 16:
self.low_datarate_optimize = 1
else:
self.low_datarate_optimize = 0
@property
def enable_crc(self) -> bool:

View file

@ -29,25 +29,39 @@ rfm = rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
rfm.radiohead = False # don't appent RadioHead heade
# set spreading factor
rfm.spreading_factor = 6
rfm.spreading_factor = 7
print("spreading factor set to :", rfm.spreading_factor)
# rfm.ack_wait = 1
# rfm.xmit_timeout = 5
# rfm.low_datarate_optimize = 1
print("low_datarate_optimize set to: ", rfm.low_datarate_optimize)
# rfm.signal_bandwidth = 500000
print("signal_bandwidth set to :", rfm.signal_bandwidth)
print("low_datarate_optimize set to: ", rfm.low_datarate_optimize)
if rfm.spreading_factor == 12:
rfm.xmit_timeout = 5
print("xmit_timeout set to: ", rfm.xmit_timeout)
if rfm.spreading_factor == 12:
rfm.receive_timeout = 5
elif rfm.spreading_factor > 7:
rfm.receive_timeout = 2
print("receive_timeout set to: ", rfm.receive_timeout)
# set node addresses
# rfm.node = 100
# rfm.destination = 0xFF
rfm.enable_crc = True
payload = bytearray(40)
# rfm.payload_length = len(payload) + 4 # add 4 for RadioHEad header
rfm.payload_length = len(payload)
# send startup message from my_node
# send startup message
message = bytes(f"startup message from base", "UTF-8")
payload[0 : len(message)] = message
rfm.send(
payload,
keep_listening=True,
)
if rfm.spreading_factor == 6:
payload = bytearray(40)
rfm.payload_length = len(payload)
payload[0 : len(message)] = message
rfm.send(
payload,
keep_listening=True,
)
else:
rfm.send(
message,
keep_listening=True,
)
# Wait to receive packets.
print("Waiting for packets...")
# initialize flag and timer
@ -67,11 +81,18 @@ while True:
packet_received = True
last_transmit_time = time.monotonic()
if packet_received and (time.monotonic() - last_transmit_time) > transmit_delay:
payload = bytearray(40)
# message = bytes(f"packet received","UTF-8")
payload[0 : len(packet)] = packet
rfm.send(
payload,
keep_listening=True,
)
# send back the received packet
if rfm.spreading_factor == 6:
payload = bytearray(40)
rfm.payload_length = len(payload)
payload[0 : len(packet)] = packet
rfm.send(
payload,
keep_listening=True,
)
else:
rfm.send(
packet,
keep_listening=True,
)
packet_received = False

View file

@ -32,12 +32,20 @@ rfm = rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
rfm.radiohead = False # Do not use RadioHead Header
# set spreading factor
rfm.spreading_factor = 6
rfm.spreading_factor = 7
print("spreading factor set to :", rfm.spreading_factor)
# rfm.ack_wait = 1
# rfm.xmit_timeout = 5
# rfm.low_datarate_optimize = 1
# rfm.receive_timeout = 5
print("low_datarate_optimize set to: ", rfm.low_datarate_optimize)
# rfm.signal_bandwidth = 500000
print("signal_bandwidth set to :", rfm.signal_bandwidth)
print("low_datarate_optimize set to: ", rfm.low_datarate_optimize)
if rfm.spreading_factor == 12:
rfm.xmit_timeout = 5
print("xmit_timeout set to: ", rfm.xmit_timeout)
if rfm.spreading_factor == 12:
rfm.receive_timeout = 5
elif rfm.spreading_factor > 7:
rfm.receive_timeout = 2
print("receive_timeout set to: ", rfm.receive_timeout)
rfm.enable_crc = True
# set the time interval (seconds) for sending packets
transmit_interval = 10
@ -49,11 +57,20 @@ transmit_interval = 10
counter = 0
ack_failed_counter = 0
# send startup message from my_node
payload = bytearray(40)
rfm.payload_length = len(payload)
message = bytes(f"startup message from node", "UTF-8")
payload[0 : len(message)] = message
rfm.send(payload)
if rfm.spreading_factor == 6:
payload = bytearray(40)
rfm.payload_length = len(payload)
payload[0 : len(message)] = message
rfm.send(
payload,
keep_listening=True,
)
else:
rfm.send(
message,
keep_listening=True,
)
# Wait to receive packets.
print("Waiting for packets...")
@ -74,8 +91,19 @@ while True:
# reset timeer
time_now = time.monotonic()
# send a mesage to destination_node from my_node
payload = bytearray(40)
message = bytes(f"message from node {counter}", "UTF-8")
payload[0 : len(message)] = message
rfm.send(payload)
if rfm.spreading_factor == 6:
payload = bytearray(40)
rfm.payload_length = len(payload)
payload[0 : len(message)] = message
rfm.send(
payload,
keep_listening=True,
)
else:
rfm.send(
message,
keep_listening=True,
)
counter += 1