Skip to content

Code Examples

Sending a Hello World

Transmitter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Send Hello World on the USB Nugget (ESP32s2)
import board
import busio
import digitalio
import adafruit_rfm9x

# Initialize SPI interface.
spi = busio.SPI(board.IO6, MOSI=board.IO10, MISO=board.IO8)

# Initialize RFM9x LoRa radio
CS = digitalio.DigitalInOut(board.IO13)
RESET = digitalio.DigitalInOut(board.IO5)
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, 915.0)
rfm9x.tx_power = 23

# Send text without specifiyng encoding
message = "Hello World!"
rfm9x.send(message)
print("Sent Hello World string!")


#Send the same text, specify utf-8 bytes
rfm9x.send(bytes("Hello world!\r\n", "utf-8"))
print("Sent Hello World bytes!")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Send Hello World on the Bluetooth Nugget (ESP32s3)
import board
import busio
import digitalio
import adafruit_rfm9x

# Initialize SPI interface
spi = busio.SPI(board.IO6, MOSI=board.IO8, MISO=board.IO7)

# Initialize RFM9x LoRa radio
CS = digitalio.DigitalInOut(board.IO9)
RESET = digitalio.DigitalInOut(board.IO4)
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, 915.0)
rfm9x.tx_power = 23

# Send text without specifiyng encoding
message = "Hello World!"
rfm9x.send(message)
print("Sent Hello World string!")


#Send the same text, specify utf-8 bytes
rfm9x.send(bytes("Hello world!\r\n", "utf-8"))
print("Sent Hello World bytes!")

Receiver

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Recieve Hello World on the USB Nugget (ESP32s2)
import board
import busio
import digitalio
import adafruit_rfm9x

# Initialize SPI interface.
spi = busio.SPI(board.IO6, MOSI=board.IO10, MISO=board.IO8)

# Initialize RFM9x LoRa radio
CS = digitalio.DigitalInOut(board.IO13)
RESET = digitalio.DigitalInOut(board.IO5)
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, 915.0)
rfm9x.tx_power = 23

while True:
    packet = rfm9x.receive()
    if packet is None:
        print("Received nothing! Listening again...")
    else:
        print("Received (raw bytes):", packet)
        # you always receive raw bytes and need to convert to a text
        # format like ASCII if you intend to do string processing.
        packet_text = str(packet, "ascii")
        print("Received (ASCII):", packet_text)
        # Also read the signal strength of the received message and print it.
        signal = rfm9x.last_rssi
        print("Received signal strength:", signal)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Recieve Hello World on the Bluetooth Nugget (ESP32s3)
import board
import busio
import digitalio
import adafruit_rfm9x

# Initialize SPI interface
spi = busio.SPI(board.IO6, MOSI=board.IO8, MISO=board.IO7)

# Initialize RFM9x LoRa radio
CS = digitalio.DigitalInOut(board.IO9)
RESET = digitalio.DigitalInOut(board.IO4)
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, 915.0)
rfm9x.tx_power = 23

while True:
    packet = rfm9x.receive()
    if packet is None:
        print("Received nothing! Listening again...")
    else:
        print("Received (raw bytes):", packet)
        # you always receive raw bytes and need to convert to a text
        # format like ASCII if you intend to do string processing.
        packet_text = str(packet, "ascii")
        print("Received (ASCII):", packet_text)
        # Also read the signal strength of the received message and print it.
        signal = rfm9x.last_rssi
        print("Received signal strength:", signal)

Packet Re-Yeeter 2000

A Packet Re-Yeeter 2000 (Repeater) made by Kody Kinzie @skickar

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Repeat a received packet on the USB Nugget (ESP32s2)

import board
import busio
import digitalio
import adafruit_rfm9x
import time

# Initialize SPI interface.
spi = busio.SPI(board.IO6, MOSI=board.IO10, MISO=board.IO8)

# Initialize RFM9x LoRa radio
CS = digitalio.DigitalInOut(board.IO13)
RESET = digitalio.DigitalInOut(board.IO5)
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, 915.0)
rfm9x.tx_power = 23

# Send text without specifiyng encoding
message = "Ping!"
rfm9x.send(message)
print("Sent Ping!")

while True:
    packet = rfm9x.receive()
    # If no packet was received during the timeout then None is returned.
    if packet is None:
        print("Received nothing! Listening again...")
    else:
        print("Received (raw bytes):", packet)
        # you always receive raw bytes and need to convert to a text
        # format like ASCII if you intend to do string processing.
        packet_text = str(packet, "ascii")
        print("Received (ASCII):", packet_text)
        # Also read the signal strength of the received message and print it.
        signal = rfm9x.last_rssi
        print("Received signal strength:", signal)
        # Rebroadcast the packet
        time.sleep(1)
        print("Rebroadcasting packet...")
        rfm9x.send(packet_text)
        print("Sent!")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Repeat a received packet on the Bluetooth Nugget (ESP32s3)

import board
import busio
import digitalio
import adafruit_rfm9x
import time

# Initialize SPI interface
spi = busio.SPI(board.IO6, MOSI=board.IO8, MISO=board.IO7)

# Initialize RFM9x LoRa radio
CS = digitalio.DigitalInOut(board.IO9)
RESET = digitalio.DigitalInOut(board.IO4)
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, 915.0)
rfm9x.tx_power = 23

# Send text without specifiyng encoding
message = "Ping!"
rfm9x.send(message)
print("Sent Ping!")

while True:
    packet = rfm9x.receive()
    # If no packet was received during the timeout then None is returned.
    if packet is None:
        print("Received nothing! Listening again...")
    else:
        print("Received (raw bytes):", packet)
        # you always receive raw bytes and need to convert to a text
        # format like ASCII if you intend to do string processing.
        packet_text = str(packet, "ascii")
        print("Received (ASCII):", packet_text)
        # Also read the signal strength of the received message and print it.
        signal = rfm9x.last_rssi
        print("Received signal strength:", signal)
        # Rebroadcast the packet
        time.sleep(1)
        print("Rebroadcasting packet...")
        rfm9x.send(packet_text)
        print("Sent!")