Simple test

Ensure your device works with this simple test.

examples/rfm9x_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# Simple demo of sending and recieving data with the RFM95 LoRa radio.
 5# Author: Tony DiCola
 6import board
 7import busio
 8import digitalio
 9
10import adafruit_rfm9x
11
12# Define radio parameters.
13RADIO_FREQ_MHZ = 915.0  # Frequency of the radio in Mhz. Must match your
14# module! Can be a value like 915.0, 433.0, etc.
15
16# Define pins connected to the chip, use these if wiring up the breakout according to the guide:
17CS = digitalio.DigitalInOut(board.D5)
18RESET = digitalio.DigitalInOut(board.D6)
19# Or uncomment and instead use these if using a Feather M0 RFM9x board and the appropriate
20# CircuitPython build:
21# CS = digitalio.DigitalInOut(board.RFM9X_CS)
22# RESET = digitalio.DigitalInOut(board.RFM9X_RST)
23
24# Define the onboard LED
25LED = digitalio.DigitalInOut(board.D13)
26LED.direction = digitalio.Direction.OUTPUT
27
28# Initialize SPI bus.
29spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
30
31# Initialze RFM radio
32rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
33
34# Note that the radio is configured in LoRa mode so you can't control sync
35# word, encryption, frequency deviation, or other settings!
36
37# You can however adjust the transmit power (in dB).  The default is 13 dB but
38# high power radios like the RFM95 can go up to 23 dB:
39rfm9x.tx_power = 23
40
41# Send a packet.  Note you can only send a packet up to 252 bytes in length.
42# This is a limitation of the radio packet size, so if you need to send larger
43# amounts of data you will need to break it into smaller send calls.  Each send
44# call will wait for the previous one to finish before continuing.
45rfm9x.send(bytes("Hello world!\r\n", "utf-8"))
46print("Sent Hello World message!")
47
48# Wait to receive packets.  Note that this library can't receive data at a fast
49# rate, in fact it can only receive and process one 252 byte packet at a time.
50# This means you should only use this for low bandwidth scenarios, like sending
51# and receiving a single message at a time.
52print("Waiting for packets...")
53
54while True:
55    packet = rfm9x.receive()
56    # Optionally change the receive timeout from its default of 0.5 seconds:
57    # packet = rfm9x.receive(timeout=5.0)
58    # If no packet was received during the timeout then None is returned.
59    if packet is None:
60        # Packet has not been received
61        LED.value = False
62        print("Received nothing! Listening again...")
63    else:
64        # Received a packet!
65        LED.value = True
66        # Print out the raw bytes of the packet:
67        print(f"Received (raw bytes): {packet}")
68        # And decode to ASCII text and print it too.  Note that you always
69        # receive raw bytes and need to convert to a text format like ASCII
70        # if you intend to do string processing on your data.  Make sure the
71        # sending side is sending ASCII data before you try to decode!
72        packet_text = str(packet, "ascii")
73        print(f"Received (ASCII): {packet_text}")
74        # Also read the RSSI (signal strength) of the last received message and
75        # print it.
76        rssi = rfm9x.last_rssi
77        print(f"Received signal strength: {rssi} dB")