-
Notifications
You must be signed in to change notification settings - Fork 77
/
radio1.py
executable file
·80 lines (64 loc) · 1.89 KB
/
radio1.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
import RFM69
from RFM69registers import *
import datetime
import time
NODE = 1
OTHERNODE = 2
NET = 1
KEY = "1234567890123456"
TIMEOUT = 5
TOSLEEP = 0.1
radio = RFM69.RFM69(RF69_433MHZ, NODE, NET, True)
print("class initialized")
#print("reading all registers")
#results = radio.readAllRegs()
#for result in results:
# print(result)
print("Performing rcCalibration")
radio.rcCalibration()
print("setting high power")
radio.setHighPower(True)
#radio.setPowerLevel(0)
print("Checking temperature")
print(radio.readTemperature(0))
print("setting encryption")
radio.encrypt(KEY)
radio.setFrequency(433500000)
radio.writeReg(REG_BITRATEMSB, RF_BITRATEMSB_1200)
radio.writeReg(REG_BITRATELSB, RF_BITRATELSB_1200)
radio.writeReg(REG_FDEVMSB, RF_FDEVMSB_5000)
radio.writeReg(REG_FDEVLSB, RF_FDEVLSB_5000)
print("starting loop...")
sequence = 0
try:
while True:
msg = "I'm radio %d: %d" % (NODE, sequence)
sequence += 1
print(f"TX >> {OTHERNODE}: {msg}")
if radio.sendWithRetry(OTHERNODE, msg, 3, 500):
print("ACK received")
print("receiving...")
radio.receiveBegin()
timedOut = 0
while not radio.receiveDone():
timedOut += TOSLEEP
time.sleep(TOSLEEP)
if timedOut > TIMEOUT:
print("nothing received")
break
if timedOut <= TIMEOUT:
sender = radio.SENDERID
msg = "".join([chr(letter) for letter in radio.DATA])
ackReq = radio.ACKRequested()
print(f"RX << {sender}: {msg} (RSSI: {radio.RSSI})")
if ackReq:
print("sending ACK...")
time.sleep(0.05)
radio.sendACK()
time.sleep(TIMEOUT / 2)
except KeyboardInterrupt:
# Clean up properly to not leave GPIO/SPI in an unusable state
pass
print("shutting down")
radio.shutdown()