forked from adafruit/Adafruit_CircuitPython_RFM9x
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrfm9x_simpletest.py
More file actions
66 lines (57 loc) · 2.7 KB
/
rfm9x_simpletest.py
File metadata and controls
66 lines (57 loc) · 2.7 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from micropython_rfm9x import *
from machine import SPI, Pin
#ESP32 Example
CS = Pin(33, Pin.OUT)
RESET = Pin(27, Pin.OUT)
spi = SPI(2, baudrate=1000000, polarity=0, phase=0, bits=8, firstbit=0, sck=Pin(5), mosi=Pin(18), miso=Pin(19))
#CS = Pin(5, Pin.OUT)
#RESET = Pin(22, Pin.OUT)
#spi = SPI(2, baudrate=1000000, polarity=0, phase=0, bits=8, firstbit=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
#ESP8266 Example
#RADIO_FREQ_MHZ = 915.0
#CS = Pin(2, Pin.OUT)
#RESET = Pin(0, Pin.OUT)
#spi = SPI(1, baudrate=5000000, polarity=0, phase=0)
RADIO_FREQ_MHZ = 915.0
# Initialze RFM radio
rfm9x = RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
# Note that the radio is configured in LoRa mode so you can't control sync
# word, encryption, frequency deviation, or other settings!
# You can however adjust the transmit power (in dB). The default is 13 dB but
# high power radios like the RFM95 can go up to 23 dB:
rfm9x.tx_power = 23
# Send a packet. Note you can only send a packet up to 252 bytes in length.
# This is a limitation of the radio packet size, so if you need to send larger
# amounts of data you will need to break it into smaller send calls. Each send
# call will wait for the previous one to finish before continuing.
rfm9x.send(bytes("Hello world!\r\n", "utf-8"))
print("Sent Hello World message!")
# Wait to receive packets. Note that this library can't receive data at a fast
# rate, in fact it can only receive and process one 252 byte packet at a time.
# This means you should only use this for low bandwidth scenarios, like sending
# and receiving a single message at a time.
print("Waiting for packets...")
while True:
packet = rfm9x.receive()
# Optionally change the receive timeout from its default of 0.5 seconds:
# packet = rfm9x.receive(timeout=5.0)
# If no packet was received during the timeout then None is returned.
if packet is None:
# Packet has not been received
LED.value = False
print("Received nothing! Listening again...")
else:
# Received a packet!
LED.value = True
# Print out the raw bytes of the packet:
print("Received (raw bytes): {0}".format(packet))
# And decode to ASCII text and print it too. Note that you always
# receive raw bytes and need to convert to a text format like ASCII
# if you intend to do string processing on your data. Make sure the
# sending side is sending ASCII data before you try to decode!
packet_text = str(packet, "ascii")
print("Received (ASCII): {0}".format(packet_text))
# Also read the RSSI (signal strength) of the last received message and
# print it.
rssi = rfm9x.last_rssi
print("Received signal strength: {0} dB".format(rssi))