Simple test

Ensure your device works with this simple test.

examples/rfm_simpletest.py
 1# SPDX-FileCopyrightText: 2024 Ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# Simple demo of sending and recieving data with the RFM9x or RFM69 radios.
 5# Author: Jerry Needell
 6
 7import board
 8import busio
 9import digitalio
10
11# Define radio parameters.
12RADIO_FREQ_MHZ = 915.0  # Frequency of the radio in Mhz. Must match your
13# module! Can be a value like 915.0, 433.0, etc.
14
15# Define pins connected to the chip, use these if wiring up the breakout according to the guide:
16CS = digitalio.DigitalInOut(board.CE1)
17RESET = digitalio.DigitalInOut(board.D25)
18
19# Initialize SPI bus.
20spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
21
22# Initialze RFM radio
23# uncommnet the desired import and rfm initialization depending on the radio boards being used
24
25# Use rfm9x for two RFM9x radios using LoRa
26
27# from adafruit_rfm import rfm9x
28
29# rfm = rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
30
31# Use rfm9xfsk for two RFM9x radios or RFM9x to RFM69 using FSK
32
33from adafruit_rfm import rfm9xfsk
34
35rfm = rfm9xfsk.RFM9xFSK(spi, CS, RESET, RADIO_FREQ_MHZ)
36
37# Use rfm69 for two RFM69 radios using FSK
38
39# from adafruit_rfm import rfm69
40
41# rfm = rfm69.RFM69(spi, CS, RESET, RADIO_FREQ_MHZ)
42
43# For RFM69 only: Optionally set an encryption key (16 byte AES key). MUST match both
44# on the transmitter and receiver (or be set to None to disable/the default).
45# rfm.encryption_key = None
46# rfm.encryption_key = (
47#    b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
48# )
49
50# for OOK on RFM69 or RFM9xFSK
51# rfm.modulation_type = 1
52
53# Send a packet.  Note you can only send a packet containing up to 60 bytes for an RFM69
54# and 252 bytes forn  an RFM9x.
55# This is a limitation of the radio packet size, so if you need to send larger
56# amounts of data you will need to break it into smaller send calls.  Each send
57# call will wait for the previous one to finish before continuing.
58rfm.send(bytes("Hello world!\r\n", "utf-8"))
59print("Sent Hello World message!")
60
61# Wait to receive packets.
62print("Waiting for packets...")
63
64while True:
65    packet = rfm.receive()
66    # Optionally change the receive timeout from its default of 0.5 seconds:
67    # packet = rfm9x.receive(timeout=5.0)
68    # If no packet was received during the timeout then None is returned.
69    if packet is None:
70        # Packet has not been received
71        print("Received nothing! Listening again...")
72    else:
73        # Received a packet!
74        # Print out the raw bytes of the packet:
75        print(f"Received (raw bytes): {packet}")
76        # And decode to ASCII text and print it too.  Note that you always
77        # receive raw bytes and need to convert to a text format like ASCII
78        # if you intend to do string processing on your data.  Make sure the
79        # sending side is sending ASCII data before you try to decode!
80        try:
81            packet_text = str(packet, "ascii")
82            print(f"Received (ASCII): {packet_text}")
83        except UnicodeError:
84            print("Hex data: ", [hex(x) for x in packet])
85        # Also read the RSSI (signal strength) of the last received message and
86        # print it.
87        rssi = rfm.last_rssi
88        print(f"Received signal strength: {rssi} dB")