add lora sf examples
This commit is contained in:
parent
752f3e25a6
commit
96682237ab
2 changed files with 158 additions and 0 deletions
77
examples/rfm_lora_sf_base.py
Normal file
77
examples/rfm_lora_sf_base.py
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
import time
|
||||
|
||||
import board
|
||||
import busio
|
||||
import digitalio
|
||||
|
||||
# Define radio parameters.
|
||||
RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your
|
||||
# module! Can be a value like 915.0, 433.0, etc.
|
||||
|
||||
# Define pins connected to the chip, use these if wiring up the breakout according to the guide:
|
||||
CS = digitalio.DigitalInOut(board.CE1)
|
||||
RESET = digitalio.DigitalInOut(board.D25)
|
||||
|
||||
# Initialize SPI bus.
|
||||
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
|
||||
|
||||
# Initialze RFM radio
|
||||
# uncommnet the desired import and rfm initialization depending on the radio boards being used
|
||||
|
||||
# Use rfm9x for two RFM9x radios using LoRa
|
||||
|
||||
from adafruit_rfm import rfm9x
|
||||
|
||||
rfm = rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
|
||||
|
||||
rfm.radiohead = False # don't appent RadioHead heade
|
||||
# set spreading factor
|
||||
rfm.spreading_factor = 6
|
||||
print("spreading factor set to :", rfm.spreading_factor)
|
||||
# rfm.ack_wait = 1
|
||||
# rfm.xmit_timeout = 5
|
||||
# rfm.low_datarate_optimize = 1
|
||||
# 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
|
||||
message = bytes(f"startup message from base", "UTF-8")
|
||||
payload[0 : len(message)] = message
|
||||
rfm.send(
|
||||
payload,
|
||||
keep_listening=True,
|
||||
)
|
||||
# Wait to receive packets.
|
||||
print("Waiting for packets...")
|
||||
# initialize flag and timer
|
||||
transmit_delay = 5
|
||||
last_transmit_time = 0
|
||||
packet_received = False
|
||||
while True:
|
||||
if rfm.payload_ready():
|
||||
packet_received = False
|
||||
packet = rfm.receive(timeout=None)
|
||||
if packet is not None:
|
||||
# Received a packet!
|
||||
# Print out the raw bytes of the packet:
|
||||
print(f"Received (raw payload): {packet}")
|
||||
print([hex(x) for x in packet])
|
||||
print(f"RSSI: {rfm.last_rssi}")
|
||||
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,
|
||||
)
|
||||
packet_received = False
|
||||
81
examples/rfm_lora_sf_node.py
Normal file
81
examples/rfm_lora_sf_node.py
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
# Example to send a packet periodically between addressed nodes with ACK
|
||||
# Author: Jerry Needell
|
||||
#
|
||||
import time
|
||||
|
||||
import board
|
||||
import busio
|
||||
import digitalio
|
||||
|
||||
# Define radio parameters.
|
||||
RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your
|
||||
# module! Can be a value like 915.0, 433.0, etc.
|
||||
|
||||
# Define pins connected to the chip, use these if wiring up the breakout according to the guide:
|
||||
CS = digitalio.DigitalInOut(board.CE1)
|
||||
RESET = digitalio.DigitalInOut(board.D25)
|
||||
|
||||
# Initialize SPI bus.
|
||||
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
|
||||
|
||||
# Initialze RFM radio
|
||||
# uncommnet the desired import and rfm initialization depending on the radio boards being used
|
||||
|
||||
# Use rfm9x for two RFM9x radios using LoRa
|
||||
|
||||
from adafruit_rfm import rfm9x
|
||||
|
||||
rfm = rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
|
||||
|
||||
rfm.radiohead = False # Do not use RadioHead Header
|
||||
# set spreading factor
|
||||
rfm.spreading_factor = 6
|
||||
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
|
||||
rfm.enable_crc = True
|
||||
# set the time interval (seconds) for sending packets
|
||||
transmit_interval = 10
|
||||
|
||||
# set node addresses
|
||||
# rfm.node = 1
|
||||
# rfm.destination = 100
|
||||
# initialize counter
|
||||
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)
|
||||
|
||||
# Wait to receive packets.
|
||||
print("Waiting for packets...")
|
||||
# initialize flag and timer
|
||||
time_now = time.monotonic()
|
||||
while True:
|
||||
# Look for a new packet: only accept if addresses to my_node
|
||||
packet = rfm.receive()
|
||||
# If no packet was received during the timeout then None is returned.
|
||||
if packet is not None:
|
||||
# Received a packet!
|
||||
# Print out the raw bytes of the packet:
|
||||
print(f"Received (raw payload): {packet}")
|
||||
print([hex(x) for x in packet])
|
||||
print(f"RSSI: {rfm.last_rssi}")
|
||||
# send reading after any packet received
|
||||
if time.monotonic() - time_now > transmit_interval:
|
||||
# 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)
|
||||
counter += 1
|
||||
Loading…
Reference in a new issue