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
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 RFM9x board and the appropriate
21# CircuitPython build:
22# CS = digitalio.DigitalInOut(board.RFM9X_CS)
23# RESET = digitalio.DigitalInOut(board.RFM9X_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
33rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
34
35# Note that the radio is configured in LoRa mode so you can't control sync
36# word, encryption, frequency deviation, or other settings!
37
38# You can however adjust the transmit power (in dB).  The default is 13 dB but
39# high power radios like the RFM95 can go up to 23 dB:
40rfm9x.tx_power = 23
41
42# Send a packet.  Note you can only send a packet up to 252 bytes in length.
43# This is a limitation of the radio packet size, so if you need to send larger
44# amounts of data you will need to break it into smaller send calls.  Each send
45# call will wait for the previous one to finish before continuing.
46rfm9x.send(bytes("Hello world!\r\n", "utf-8"))
47print("Sent Hello World message!")
48
49# Wait to receive packets.  Note that this library can't receive data at a fast
50# rate, in fact it can only receive and process one 252 byte packet at a time.
51# This means you should only use this for low bandwidth scenarios, like sending
52# and receiving a single message at a time.
53print("Waiting for packets...")
54
55while True:
56    packet = rfm9x.receive()
57    # Optionally change the receive timeout from its default of 0.5 seconds:
58    # packet = rfm9x.receive(timeout=5.0)
59    # If no packet was received during the timeout then None is returned.
60    if packet is None:
61        # Packet has not been received
62        LED.value = False
63        print("Received nothing! Listening again...")
64    else:
65        # Received a packet!
66        LED.value = True
67        # Print out the raw bytes of the packet:
68        print("Received (raw bytes): {0}".format(packet))
69        # And decode to ASCII text and print it too.  Note that you always
70        # receive raw bytes and need to convert to a text format like ASCII
71        # if you intend to do string processing on your data.  Make sure the
72        # sending side is sending ASCII data before you try to decode!
73        packet_text = str(packet, "ascii")
74        print("Received (ASCII): {0}".format(packet_text))
75        # Also read the RSSI (signal strength) of the last received message and
76        # print it.
77        rssi = rfm9x.last_rssi
78        print("Received signal strength: {0} dB".format(rssi))