-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranciever.py
56 lines (43 loc) · 1.31 KB
/
Tranciever.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
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize(32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
radio.openWritingPipe(pipes[0])
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
radio.startListening()
message = list("GETSTRING")
while len(message) < 32:
message.append(0)
while True:
start = time.time()
radio.write(message)
print("Sent the message: {}".format(message))
radio.startListening()
while not radio.available(0):
time.sleep(1/100)
if time.time() - start > 2:
print("Timed out.")
break
receivedMessage = []
radio.read(receivedMessage, radio.getDynamicPayloadSize())
print("Received: {}".format(receivedMessage))
print("Translating our received Message into unicode characters...")
string = ""
for n in receivedMessage:
if (n >= 32 and n <= 126):
string += chr(n)
print("Our received message decodes to: {}".format(string))
radio.stopListening()
time.sleep(1)