Simple test
Ensure your device works with this simple test.
examples/rfm69_simpletest.py
1# SPDX-FileCopyrightText: 2018 Tony DiCola for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Simple example to send a message and then wait indefinitely for messages
5# to be received. This uses the default RadioHead compatible GFSK_Rb250_Fd250
6# modulation and packet format for the radio.
7import board
8import busio
9import digitalio
10
11import adafruit_rfm69
12
13# Define radio parameters.
14RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your
15# module! Can be a value like 915.0, 433.0, etc.
16
17# Define pins connected to the chip, use these if wiring up the breakout according to the guide:
18CS = digitalio.DigitalInOut(board.D5)
19RESET = digitalio.DigitalInOut(board.D6)
20# Or uncomment and instead use these if using a Feather M0 RFM69 board
21# and the appropriate CircuitPython build:
22# CS = digitalio.DigitalInOut(board.RFM69_CS)
23# RESET = digitalio.DigitalInOut(board.RFM69_RST)
24
25# Define the onboard LED
26LED = digitalio.DigitalInOut(board.D13)
27LED.direction = digitalio.Direction.OUTPUT
28
29# Initialize SPI bus.
30spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
31
32# Initialze RFM radio
33rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, RADIO_FREQ_MHZ)
34
35# Optionally set an encryption key (16 byte AES key). MUST match both
36# on the transmitter and receiver (or be set to None to disable/the default).
37rfm69.encryption_key = b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
38
39# Print out some chip state:
40print(f"Temperature: {rfm69.temperature}C")
41print(f"Frequency: {rfm69.frequency_mhz}mhz")
42print(f"Bit rate: {rfm69.bitrate / 1000}kbit/s")
43print(f"Frequency deviation: {rfm69.frequency_deviation}hz")
44
45# Send a packet. Note you can only send a packet up to 60 bytes in length.
46# This is a limitation of the radio packet size, so if you need to send larger
47# amounts of data you will need to break it into smaller send calls. Each send
48# call will wait for the previous one to finish before continuing.
49rfm69.send(bytes("Hello world!\r\n", "utf-8"))
50print("Sent hello world message!")
51
52# Wait to receive packets. Note that this library can't receive data at a fast
53# rate, in fact it can only receive and process one 60 byte packet at a time.
54# This means you should only use this for low bandwidth scenarios, like sending
55# and receiving a single message at a time.
56print("Waiting for packets...")
57while True:
58 packet = rfm69.receive()
59 # Optionally change the receive timeout from its default of 0.5 seconds:
60 # packet = rfm69.receive(timeout=5.0)
61 # If no packet was received during the timeout then None is returned.
62 if packet is None:
63 # Packet has not been received
64 LED.value = False
65 print("Received nothing! Listening again...")
66 else:
67 # Received a packet!
68 LED.value = True
69 # Print out the raw bytes of the packet:
70 print(f"Received (raw bytes): {packet}")
71 # And decode to ASCII text and print it too. Note that you always
72 # receive raw bytes and need to convert to a text format like ASCII
73 # if you intend to do string processing on your data. Make sure the
74 # sending side is sending ASCII data before you try to decode!
75 packet_text = str(packet, "ascii")
76 print(f"Received (ASCII): {packet_text}")