forked from adafruit/Adafruit_CircuitPython_RFM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfm_rh_asyncio_node1.py
129 lines (99 loc) · 3.9 KB
/
rfm_rh_asyncio_node1.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import asyncio
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)
# 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)
# 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"
# )
# for OOK on RFM69 or RFM9xFSK
# rfm.modulation_type = 1
# set node addresses
rfm.node = 1
rfm.destination = 100
# send startup message from my_node
rfm.send_with_ack(bytes(f"startup message from node {rfm.node}", "UTF-8"))
rfm.listen()
# 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
# setup interrupt callback function
async def wait_for_packets(packet_status, lock):
while True:
if rfm.payload_ready():
if lock.locked():
print("locked waiting for receive")
async with lock:
packet = await rfm.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: {rfm.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 rfm.asyncio_send_with_ack(
bytes(
f"message from node {rfm.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))
await asyncio.gather(task1, task2) # Don't forget "await"!
asyncio.run(main())