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