more example revisions

This commit is contained in:
jerryneedell 2024-08-01 07:12:00 -04:00
parent 123be5abca
commit b4ab57e44f
12 changed files with 70 additions and 851 deletions

View file

@ -40,10 +40,6 @@ or individual libraries can be installed using
Installing from PyPI
=====================
.. note:: This library is not available on PyPI yet. Install documentation is included
as a standard element. Stay tuned for PyPI availability!
.. todo:: Remove the above note if PyPI version is/will be available at time of release.
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
PyPI <https://pypi.org/project/adafruit-circuitpython-rfm/>`_.

View file

@ -1,79 +0,0 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Example to send a packet periodically
# Author: Jerry Needell
#
import time
import board
import busio
import digitalio
from adafruit_rfm import rfm69
# 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
rfm69 = rfm69.RFM69(spi, CS, RESET, RADIO_FREQ_MHZ)
# set the time interval (seconds) for sending packets
node = 1
destination = 2
rfm69.radiohead = False
print(rfm69.modulation_type)
rfm69.modulation_type = 1
print(rfm69.modulation_type)
# Optionally set an encryption key (16 byte AES key). MUST match both
# on the transmitter and receiver (or be set to None to disable/the default).
rfm69.encryption_key = None
# rfm69.encryption_key = (
# b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
# )
# set the time interval (seconds) for sending packets
transmit_interval = 5
# Note that the radio is configured in LoRa mode so you can't control sync
# word, encryption, frequency deviation, or other settings!
# You can however adjust the transmit power (in dB). The default is 13 dB but
# high power radios like the RFM69 can go up to 20 dB:
# rfm69.tx_power = 20
# initialize counter
counter = 0
# send a broadcast mesage
rfm69.send(bytes(f"message number {counter}", "UTF-8"))
# Wait to receive packets.
print("Waiting for packets...")
# initialize flag and timer
send_reading = False
time_now = time.monotonic()
while True:
# Look for a new packet - wait up to 2 seconds:
packet = rfm69.receive(timeout=2.0)
# 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 bytes): {packet}")
# send reading after any packet received
if time.monotonic() - time_now > transmit_interval:
# reset timeer
time_now = time.monotonic()
# clear flag to send data
send_reading = False
counter = counter + 1
rfm69.send(bytes(f"message number {counter}", "UTF-8"))

View file

@ -1,161 +0,0 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Example using Interrupts to send a message and then wait indefinitely for messages
# to be received. Interrupts are used only for receive. sending is done with polling.
# This example is for systems that support interrupts like the Raspberry Pi with "blinka"
# CircuitPython does not support interrupts so it will not work on Circutpython boards
# Author: Tony DiCola, Jerry Needell
import asyncio
import time
import adafruit_gps
import board
import busio
import digitalio
from adafruit_rfm import rfm9x
uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=10)
# Create a GPS module instance.
gps = adafruit_gps.GPS(uart, debug=False) # Use UART/pyserial
# Turn on the basic GGA and RMC info (what you typically want)
gps.send_command(b"PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0")
# Set update rate to once a second (1hz) which is what you typically want.
gps.send_command(b"PMTK220,1000")
# 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.D10)
RESET = digitalio.DigitalInOut(board.D11)
# Initialize SPI bus.
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
# Initialze RFM radio
rfm9x = rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
# set node addresses
rfm9x.node = 2
rfm9x.destination = 100
# send startup message from my_node
# rfm9x.send_with_ack(bytes("startup message from node {}".format(rfm9x.node), "UTF-8"))
rfm9x.listen()
# rfm9x.xmit_timeout = 5.
# Wait to receive packets.
print("Waiting for packets...")
# initialize flag and timer
# pylint: disable=too-few-public-methods
class Packet:
"""Simple class to hold an value. Use .value to to read or write."""
def __init__(self):
self.received = False
self.gps_time = "Unknown"
self.gps_latitude = 0.0
self.gps_longitude = 0.0
async def read_gps(packet_status, wait):
# Main loop runs forever printing the location, etc. every second.
last_print = time.monotonic()
while True:
# Make sure to call gps.update() every loop iteration and at least twice
# as fast as data comes from the GPS unit (usually every second).
# This returns a bool that's true if it parsed new data (you can ignore it
# though if you don't care and instead look at the has_fix property).
gps.update()
# Every second print out current location details if there's a fix.
current = time.monotonic()
if current - last_print >= 1.0:
last_print = current
if not gps.has_fix:
# Try again if we don't have a fix yet.
print("Waiting for fix...")
packet_status.gps_time = "Unknown"
packet_status.gps_latitude = 0.0
packet_status.gps_longitude = 0.0
continue
# We have a fix! (gps.has_fix is true)
# Print out details about the fix like location, date, etc.
print("=" * 40) # Print a separator line.
packet_status.gps_time = f"Fix timestamp: {gps.timestamp_utc.tm_mon}/{gps.timestamp_utc.tm_mday}/{gps.timestamp_utc.tm_year} {gps.timestamp_utc.tm_hour:02}:{gps.timestamp_utc.tm_min:02}:{gps.timestamp_utc.tm_sec:02}" # noqa: E501
print(packet_status.gps_time)
print(f"Latitude: {gps.latitude:.6f} degrees")
print(f"Longitude: {gps.longitude:.6f} degrees")
packet_status.gps_latitude = gps.latitude
packet_status.gps_longitude = gps.longitude
if gps.satellites is not None:
print(f"# satellites: {gps.satellites}")
if gps.altitude_m is not None:
print(f"Altitude: {gps.altitude_m} meters")
await asyncio.sleep(wait)
async def wait_for_packets(packet_status, lock):
while True:
if rfm9x.payload_ready():
if lock.locked():
print("locked waiting for receive")
async with lock:
packet = await rfm9x.asyncio_receive_with_ack(with_header=True, timeout=None)
if packet is not None:
packet_status.received = True
# Received a packet!
# Print out the raw bytes of the packet:
print(f"Received (raw bytes): {packet}")
print([hex(x) for x in packet])
print(f"RSSI: {rfm9x.last_rssi}")
await asyncio.sleep(0.001)
async def send_packets(packet_status, lock):
# initialize counter
counter = 0
ack_failed_counter = 0
counter = 0
transmit_interval = 5
time_now = time.monotonic()
while True:
# If no packet was received during the timeout then None is returned.
if packet_status.received:
packet_status.received = False
if time.monotonic() - time_now > transmit_interval:
# reset timeer
time_now = time.monotonic()
counter += 1
# send a mesage to destination_node from my_node
if lock.locked():
print("locked waiting for send")
async with lock:
if not await rfm9x.asyncio_send_with_ack(
bytes(
f"message from node {rfm9x.node} {counter} {ack_failed_counter} {packet_status.gps_time} {packet_status.gps_latitude:.6f} {packet_status.gps_longitude:.6f}", # noqa: E501
"UTF-8",
)
):
ack_failed_counter += 1
print(" No Ack: ", counter, ack_failed_counter)
await asyncio.sleep(0.1)
async def main():
packet_status = Packet()
lock = asyncio.Lock()
task1 = asyncio.create_task(wait_for_packets(packet_status, lock))
task2 = asyncio.create_task(send_packets(packet_status, lock))
task3 = asyncio.create_task(read_gps(packet_status, 0.25))
await asyncio.gather(task1, task2, task3) # Don't forget "await"!
asyncio.run(main())

View file

@ -1,162 +0,0 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Example using Interrupts to send a message and then wait indefinitely for messages
# to be received. Interrupts are used only for receive. sending is done with polling.
# This example is for systems that support interrupts like the Raspberry Pi with "blinka"
# CircuitPython does not support interrupts so it will not work on Circutpython boards
# Author: Tony DiCola, Jerry Needell
import asyncio
import time
import board
import busio
import digitalio
import neopixel
from adafruit_rfm import rfm9x
# On CircuitPlayground Express, and boards with built in status NeoPixel -> board.NEOPIXEL
# Otherwise choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D1
pixel_pin = board.D5
# On a Raspberry pi, use this instead, not all pins are supported
# pixel_pin = board.D18
# The number of NeoPixels
num_pixels = 32
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(
pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
)
# 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.D10)
RESET = digitalio.DigitalInOut(board.D11)
# Initialize SPI bus.
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
# Initialze RFM radio
rfm9x = rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
# set node addresses
rfm9x.node = 2
rfm9x.destination = 100
# send startup message from my_node
# rfm9x.send_with_ack(bytes("startup message from node {}".format(rfm9x.node), "UTF-8"))
rfm9x.listen()
# rfm9x.xmit_timeout = 5.
# Wait to receive packets.
print("Waiting for packets...")
# initialize flag and timer
# pylint: disable=too-few-public-methods
class Packet:
"""Simple class to hold an value. Use .value to to read or write."""
def __init__(self):
self.received = False
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos * 3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos * 3)
g = 0
b = int(pos * 3)
else:
pos -= 170
r = 0
g = int(pos * 3)
b = int(255 - pos * 3)
return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)
async def rainbow_cycle(wait, lock):
while True:
if not lock.locked():
for j in range(255):
for i in range(num_pixels):
pixel_index = (i * 256 // num_pixels) + j
pixels[i] = wheel(pixel_index & 255)
pixels.show()
await asyncio.sleep(wait)
async def wait_for_packets(packet_status, lock):
while True:
if rfm9x.payload_ready():
if lock.locked():
print("locked waiting for receive")
async with lock:
packet = await rfm9x.asyncio_receive_with_ack(with_header=True, timeout=None)
if packet is not None:
packet_status.received = True
# Received a packet!
# Print out the raw bytes of the packet:
print(f"Received (raw bytes): {packet}")
print([hex(x) for x in packet])
print(f"RSSI: {rfm9x.last_rssi}")
await asyncio.sleep(0.001)
async def send_packets(packet_status, lock):
# initialize counter
counter = 0
ack_failed_counter = 0
counter = 0
transmit_interval = 5
time_now = time.monotonic()
while True:
# If no packet was received during the timeout then None is returned.
if packet_status.received:
packet_status.received = False
if time.monotonic() - time_now > transmit_interval:
# reset timeer
time_now = time.monotonic()
counter += 1
# send a mesage to destination_node from my_node
if lock.locked():
print("locked waiting for send")
async with lock:
if not await rfm9x.asyncio_send_with_ack(
bytes(
f"message from node {rfm9x.node} {counter} {ack_failed_counter}",
"UTF-8",
)
):
ack_failed_counter += 1
print(" No Ack: ", counter, ack_failed_counter)
await asyncio.sleep(0.1)
async def main():
packet_status = Packet()
lock = asyncio.Lock()
task1 = asyncio.create_task(wait_for_packets(packet_status, lock))
task2 = asyncio.create_task(send_packets(packet_status, lock))
task3 = asyncio.create_task(rainbow_cycle(0.001, lock)) # rainbow cycle with 1ms delay per step
await asyncio.gather(task1, task2, task3) # Don't forget "await"!
asyncio.run(main())

View file

@ -1,40 +0,0 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Example to display raw packets including header
# Author: Jerry Needell
#
import board
import busio
import digitalio
from adafruit_rfm import rfm9x
# 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
rfm9x = rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
# Wait to receive packets.
print("Waiting for packets...")
# initialize flag and timer
while True:
# Look for a new packet: only accept if addresses to my_node
packet = rfm9x.receive(with_header=True)
# 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("Received (raw header):", [hex(x) for x in packet[0:]])
print(f"Received (raw payload): {packet[4:]}")
print(f"RSSI: {rfm9x.last_rssi}")
# send reading after any packet received

View file

@ -1,66 +0,0 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Example to send a packet periodically
# Author: Jerry Needell
#
import time
import board
import busio
import digitalio
from adafruit_rfm import rfm9x
# 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
rfm9x = rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
# set the time interval (seconds) for sending packets
transmit_interval = 5
# Note that the radio is configured in LoRa mode so you can't control sync
# word, encryption, frequency deviation, or other settings!
# You can however adjust the transmit power (in dB). The default is 13 dB but
# high power radios like the RFM95 can go up to 23 dB:
rfm9x.tx_power = 23
# initialize counter
counter = 0
# send a broadcast mesage
rfm9x.send(bytes(f"message number {counter}", "UTF-8"))
# Wait to receive packets.
print("Waiting for packets...")
# initialize flag and timer
send_reading = False
time_now = time.monotonic()
while True:
# Look for a new packet - wait up to 2 seconds:
packet = rfm9x.receive(timeout=2.0)
# 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 bytes): {packet}")
# send reading after any packet received
if time.monotonic() - time_now > transmit_interval:
# reset timeer
time_now = time.monotonic()
# clear flag to send data
send_reading = False
counter = counter + 1
rfm9x.send(bytes(f"message number {counter}", "UTF-8"))

View file

@ -1,66 +0,0 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Example to send a packet periodically
# Author: Jerry Needell
#
import random
import time
import board
import busio
import digitalio
from adafruit_rfm import rfm9xfsk
# 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
rfm9xfsk = rfm9xfsk.RFM9xFSK(spi, CS, RESET, RADIO_FREQ_MHZ)
node = 1
destination = 2
rfm9xfsk.radiohead = False
rfm9xfsk.enable_address_filter = True
rfm9xfsk.fsk_node_address = node
rfm9xfsk.fsk_broadcast_address = 0xFF
# set the time interval (seconds) for sending packets
transmit_interval = 2
# Note that the radio is configured in LoRa mode so you can't control sync
# word, encryption, frequency deviation, or other settings!
# You can however adjust the transmit power (in dB). The default is 13 dB but
# high power radios like the RFM95 can go up to 23 dB:
# rfm9xfsk.tx_power = 23
# initialize counter
counter = 0
# send a startup mesage
rfm9xfsk.send(bytes(f"message number {counter}", "UTF-8"), destination=destination)
# Wait to receive packets.
print("Waiting for packets...")
# initialize flag and timer
time_now = time.monotonic()
while True:
# Look for a new packet - wait up to 2 seconds:
packet = rfm9xfsk.receive(timeout=2.0)
# 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 bytes): {packet}", rfm9xfsk.last_rssi)
# clear flag to send data
time.sleep(random.random())
counter = counter + 1
rfm9xfsk.send(bytes(f"message number {counter}", "UTF-8"), destination=destination)

View file

@ -1,66 +0,0 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Example to send a packet periodically
# Author: Jerry Needell
#
import random
import time
import board
import busio
import digitalio
from adafruit_rfm import rfm9xfsk
# 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
rfm9xfsk = rfm9xfsk.RFM9xFSK(spi, CS, RESET, RADIO_FREQ_MHZ)
node = 2
destination = 1
rfm9xfsk.radiohead = False
rfm9xfsk.enable_address_filter = True
rfm9xfsk.fsk_node_address = node
rfm9xfsk.fsk_broadcast_address = 0xFF
# set the time interval (seconds) for sending packets
transmit_interval = 2
# Note that the radio is configured in LoRa mode so you can't control sync
# word, encryption, frequency deviation, or other settings!
# You can however adjust the transmit power (in dB). The default is 13 dB but
# high power radios like the RFM95 can go up to 23 dB:
# rfm9xfsk.tx_power = 23
# initialize counter
counter = 0
# send a startup mesage
rfm9xfsk.send(bytes(f"message number {counter}", "UTF-8"), destination=destination)
# Wait to receive packets.
print("Waiting for packets...")
# initialize flag and timer
time_now = time.monotonic()
while True:
# Look for a new packet - wait up to 2 seconds:
packet = rfm9xfsk.receive(timeout=0.5)
# 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 bytes): {packet}", rfm9xfsk.last_rssi)
if time.monotonic() - time_now > transmit_interval + random.random():
counter = counter + 1
rfm9xfsk.send(bytes(f"message number {counter}", "UTF-8"), destination=destination)
time_now = time.monotonic()

View file

@ -1,80 +0,0 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Example to send a packet periodically
# Author: Jerry Needell
#
import time
import board
import busio
import digitalio
from adafruit_rfm import rfm9xfsk
# 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
rfm9xfsk = rfm9xfsk.RFM9xFSK(spi, CS, RESET, RADIO_FREQ_MHZ)
print(
rfm9xfsk.fsk_node_address,
rfm9xfsk.fsk_broadcast_address,
rfm9xfsk.enable_address_filter,
)
rfm9xfsk.radiohead = False
# rfm9xfsk.enable_address_filter=True
# rfm9xfsk.fsk_node_address=0x2
# rfm9xfsk.fsk_broadcast_address=0xff
print(
rfm9xfsk.fsk_node_address,
rfm9xfsk.fsk_broadcast_address,
rfm9xfsk.enable_address_filter,
)
# set the time interval (seconds) for sending packets
transmit_interval = 5
# Note that the radio is configured in LoRa mode so you can't control sync
# word, encryption, frequency deviation, or other settings!
# You can however adjust the transmit power (in dB). The default is 13 dB but
# high power radios like the RFM95 can go up to 23 dB:
rfm9xfsk.tx_power = 23
# initialize counter
counter = 0
# send a broadcast mesage
rfm9xfsk.send(bytes(f"message number {counter}", "UTF-8"))
# Wait to receive packets.
print("Waiting for packets...")
# initialize flag and timer
send_reading = False
time_now = time.monotonic()
while True:
# Look for a new packet - wait up to 2 seconds:
packet = rfm9xfsk.receive(timeout=2.0)
# 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 bytes): {packet}")
# send reading after any packet received
if time.monotonic() - time_now > transmit_interval:
# reset timeer
time_now = time.monotonic()
# clear flag to send data
send_reading = False
counter = counter + 1
rfm9xfsk.send(bytes(f"message number {counter}", "UTF-8"))

View file

@ -1,83 +0,0 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Example to send a packet periodically
# Author: Jerry Needell
#
import time
import board
import busio
import digitalio
from adafruit_rfm import rfm9xfsk
# 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
rfm9xfsk = rfm9xfsk.RFM9xFSK(spi, CS, RESET, RADIO_FREQ_MHZ)
print(
rfm9xfsk.fsk_node_address,
rfm9xfsk.fsk_broadcast_address,
rfm9xfsk.enable_address_filter,
)
rfm9xfsk.radiohead = False
print(rfm9xfsk.modulation_type)
rfm9xfsk.modulation_type = 1
print(rfm9xfsk.modulation_type)
# rfm9xfsk.enable_address_filter=True
# rfm9xfsk.fsk_node_address=0x2
# rfm9xfsk.fsk_broadcast_address=0xff
print(
rfm9xfsk.fsk_node_address,
rfm9xfsk.fsk_broadcast_address,
rfm9xfsk.enable_address_filter,
)
# set the time interval (seconds) for sending packets
transmit_interval = 5
# Note that the radio is configured in LoRa mode so you can't control sync
# word, encryption, frequency deviation, or other settings!
# You can however adjust the transmit power (in dB). The default is 13 dB but
# high power radios like the RFM95 can go up to 23 dB:
rfm9xfsk.tx_power = 23
# initialize counter
counter = 0
# send a broadcast mesage
rfm9xfsk.send(bytes(f"message number {counter}", "UTF-8"))
# Wait to receive packets.
print("Waiting for packets...")
# initialize flag and timer
send_reading = False
time_now = time.monotonic()
while True:
# Look for a new packet - wait up to 2 seconds:
packet = rfm9xfsk.receive(timeout=2.0)
# 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 bytes): {packet}")
# send reading after any packet received
if time.monotonic() - time_now > transmit_interval:
# reset timeer
time_now = time.monotonic()
# clear flag to send data
send_reading = False
counter = counter + 1
rfm9xfsk.send(bytes(f"message number {counter}", "UTF-8"))

View file

@ -9,8 +9,6 @@ import board
import busio
import digitalio
from adafruit_rfm import rfm69
# 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.
@ -23,26 +21,47 @@ RESET = digitalio.DigitalInOut(board.D25)
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
# Initialze RFM radio
rfm69 = rfm69.RFM69(spi, CS, RESET, RADIO_FREQ_MHZ)
# 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)
# Use rfm9xfsk for two RFM9x radios or RFM9x to RFM69 using FSK
from adafruit_rfm import rfm9xfsk
rfm = rfm9xfsk.RFM9xFSK(spi, CS, RESET, RADIO_FREQ_MHZ)
# Use rfm69 for two RFM69 radios using FSK
# from adafruit_rfm import rfm69
# rfm = rfm69.RFM69(spi, CS, RESET, RADIO_FREQ_MHZ)
# Disable the RadioHead Header
rfm.radiohead = False
# set the time interval (seconds) for sending packets
transmit_interval = 5
node = 1
destination = 2
rfm69.radiohead = False
rfm69.enable_address_filter = True
rfm69.fsk_node_address = node
rfm69.fsk_broadcast_address = 0xFF
rfm.enable_address_filter = True
rfm.fsk_node_address = node
rfm.fsk_broadcast_address = 0xFF
# For RFM69 only:
# Optionally set an encryption key (16 byte AES key). MUST match both
# on the transmitter and receiver (or be set to None to disable/the default).
rfm69.encryption_key = None
# rfm69.encryption_key = (
rfm.encryption_key = None
# rfm.encryption_key = (
# b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
# )
# initialize counter
counter = 0
# send a broadcast mesage
rfm69.send(bytes(f"message number {counter}", "UTF-8"), destination=destination)
rfm.send(bytes(f"message number {counter}", "UTF-8"), destination=destination)
# Wait to receive packets.
print("Waiting for packets...")
@ -50,7 +69,7 @@ print("Waiting for packets...")
time_now = time.monotonic()
while True:
# Look for a new packet - wait up to 5 seconds:
packet = rfm69.receive(timeout=2.0)
packet = rfm.receive(timeout=2.0)
# If no packet was received during the timeout then None is returned.
if packet is not None:
# Received a packet!
@ -61,4 +80,4 @@ while True:
# reset timeer
time_now = time.monotonic()
counter = counter + 1
rfm69.send(bytes(f"message number {counter}", "UTF-8"), destination=destination)
rfm.send(bytes(f"message number {counter}", "UTF-8"), destination=destination)

View file

@ -1,17 +1,14 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-FileCopyrightText: 2020 Jerry Needell for Adafruit Industries
# SPDX-License-Identifier: MIT
# Example to send a packet periodically
# Author: Jerry Needell
#
import time
import board
import busio
import digitalio
from adafruit_rfm import rfm69
# 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.
@ -23,44 +20,56 @@ RESET = digitalio.DigitalInOut(board.D25)
# Initialize SPI bus.
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
# Initialze RFM radio
rfm69 = rfm69.RFM69(spi, CS, RESET, RADIO_FREQ_MHZ)
# set the time interval (seconds) for sending packets
node = 1
destination = 2
rfm69.radiohead = False
# Optionally set an encryption key (16 byte AES key). MUST match both
# on the transmitter and receiver (or be set to None to disable/the default).
rfm69.encryption_key = None
# rfm69.encryption_key = (
# b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
# )
# 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)
# Use rfm9xfsk for two RFM9x radios or RFM9x to RFM69 using FSK
from adafruit_rfm import rfm9xfsk
rfm = rfm9xfsk.RFM9xFSK(spi, CS, RESET, RADIO_FREQ_MHZ)
# Use rfm69 for two RFM69 radios using FSK
# from adafruit_rfm import rfm69
# rfm = rfm69.RFM69(spi, CS, RESET, RADIO_FREQ_MHZ)
# Disable the RadioHead Header
rfm.radiohead = False
# set the time interval (seconds) for sending packets
transmit_interval = 5
# Note that the radio is configured in LoRa mode so you can't control sync
# word, encryption, frequency deviation, or other settings!
# You can however adjust the transmit power (in dB). The default is 13 dB but
# high power radios like the RFM69 can go up to 20 dB:
# rfm69.tx_power = 20
node = 2
destination = 1
rfm.enable_address_filter = True
rfm.fsk_node_address = node
rfm.fsk_broadcast_address = 0xFF
# For RFM69 only:
# Optionally set an encryption key (16 byte AES key). MUST match both
# on the transmitter and receiver (or be set to None to disable/the default).
rfm.encryption_key = None
# rfm.encryption_key = (
# b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
# )
# initialize counter
counter = 0
# send a broadcast mesage
rfm69.send(bytes(f"message number {counter}", "UTF-8"))
rfm.send(bytes(f"message number {counter}", "UTF-8"), destination=destination)
# Wait to receive packets.
print("Waiting for packets...")
# initialize flag and timer
send_reading = False
time_now = time.monotonic()
while True:
# Look for a new packet - wait up to 2 seconds:
packet = rfm69.receive(timeout=2.0)
# Look for a new packet - wait up to 5 seconds:
packet = rfm.receive(timeout=2.0)
# If no packet was received during the timeout then None is returned.
if packet is not None:
# Received a packet!
@ -70,7 +79,5 @@ while True:
if time.monotonic() - time_now > transmit_interval:
# reset timeer
time_now = time.monotonic()
# clear flag to send data
send_reading = False
counter = counter + 1
rfm69.send(bytes(f"message number {counter}", "UTF-8"))
rfm.send(bytes(f"message number {counter}", "UTF-8"), destination=destination)