def init(): start = time.time() GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(22, GPIO.OUT, initial=GPIO.LOW) print("Transmitter") pipe_Tx = [0xe7, 0xe7, 0xe7, 0xe7, 0xe7] pipe_Rx = [0xc2, 0xc2, 0xc2, 0xc2, 0xc2] payloadSize = 32 channel_TX = 40 channel_RX = 50 #Initializa the radio transceivers with the CE ping connected to the GPIO22 and GPIO24 radio_Tx = NRF24(GPIO, spidev.SpiDev()) radio_Rx = NRF24(GPIO, spidev.SpiDev()) radio_Tx.begin(0, 22) radio_Rx.begin(1, 23) #We set the Payload Size to the limit which is 32 bytes radio_Tx.setPayloadSize(payloadSize) radio_Rx.setPayloadSize(payloadSize) #We choose the channels to be used for one and the other transceiver radio_Tx.setChannel(channel_TX) radio_Rx.setChannel(channel_RX) #We set the Transmission Rate radio_Tx.setDataRate(NRF24.BR_2MBPS) radio_Rx.setDataRate(NRF24.BR_2MBPS) #Configuration of the power level to be used by the transceiver radio_Tx.setPALevel(NRF24.PA_LOW) radio_Rx.setPALevel(NRF24.PA_LOW) #CRC Length radio_Tx.setCRCLength(NRF24.CRC_8) radio_Rx.setCRCLength(NRF24.CRC_8) #We disable the Auto Acknowledgement radio_Tx.setAutoAck(False) radio_Rx.setAutoAck(False) radio_Tx.enableDynamicPayloads() radio_Rx.enableDynamicPayloads() #Open the writing and reading pipe radio_Tx.openWritingPipe(pipe_Tx) radio_Rx.openReadingPipe(0, pipe_Rx) #We print the configuration details of both transceivers radio_Tx.printDetails() print( "*------------------------------------------------------------------------------------------------------------*" ) radio_Rx.printDetails() print( "*------------------------------------------------------------------------------------------------------------*" ) return (radio_Tx, radio_Rx)
def __init__(self): global contador self.h = 0 self.contador = 0 self.rele = 15 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(self.rele, GPIO.OUT) GPIO.cleanup(self.rele) pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]] self.radio = NRF24(GPIO, spidev.SpiDev()) self.radio.begin(0, 22) self.radio.setRetries(15, 15) self.radio.setPayloadSize(32) self.radio.setChannel(0x60) self.radio.setDataRate(NRF24.BR_2MBPS) self.radio.setPALevel(NRF24.PA_MAX) self.radio.setAutoAck(True) self.radio.enableDynamicPayloads() self.radio.enableAckPayload() self.radio.openWritingPipe(pipes[1]) self.radio.openReadingPipe(1, pipes[0]) self.radio.printDetails() self.radio2 = NRF24(GPIO, spidev.SpiDev()) self.radio2.begin(0, 22) self.radio2.setRetries(15, 15) self.radio2.setPayloadSize(32) self.radio2.setChannel(0x60) self.radio2.setDataRate(NRF24.BR_2MBPS) self.radio2.setPALevel(NRF24.PA_MAX) self.radio2.setAutoAck(True) self.radio2.enableDynamicPayloads() self.radio2.enableAckPayload() self.radio2.openWritingPipe(pipes[0]) self.radio2.openReadingPipe(1, pipes[1]) self.radio2.startListening() self.radio2.stopListening() self.radio2.printDetails() self.radio2.startListening()
class radio_comms(object): """ An all-powerful radio communication class using NRF24""" pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]] radio = NRF24(GPIO, spidev.SpiDev()) def __init__(self): radio_comms.radio.begin(0, 17) time.sleep(1) radio_comms.radio.setRetries(15,15) radio_comms.radio.setPayloadSize(32) radio_comms.radio.setChannel(0x60) radio_comms.radio.setDataRate(NRF24.BR_2MBPS) radio_comms.radio.setPALevel(NRF24.PA_MIN) radio_comms.radio.setAutoAck(True) radio_comms.radio.enableDynamicPayloads() radio_comms.radio.enableAckPayload() radio_comms.radio.openWritingPipe(radio_comms.pipes[1]) radio_comms.radio.openReadingPipe(1, radio_comms.pipes[0]) radio_comms.radio.printDetails() def send_data(self,data): radio_comms.radio.write(data) print("Sent: ", data) def recv_data(self): radio_comms.radio.startListening() data_pipe = [0] while not radio_comms.radio.available(data_pipe): time.sleep(0.01) recv_buffer = [] radio_comms.radio.read(recv_buffer, radio_comms.radio.getDynamicPayloadSize()) radio_comms.radio.stopListening() return recv_buffer
def bleSetup(): global linecounter linecounter = findNumberOfLines(stringData) print("Linecounter: ", linecounter) #linecounter = 1 GPIO.setmode(GPIO.BCM) pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]] conn = NRF24(GPIO, spidev.SpiDev()) conn.begin(0, 17) conn.setPayloadSize(32) conn.setChannel(0x76) #endre paa kanal for aa legge til flere bleenheter conn.setDataRate(NRF24.BR_1MBPS) conn.setPALevel(NRF24.PA_MIN) conn.setAutoAck(True) conn.enableDynamicPayloads() conn.enableAckPayload() conn.openWritingPipe(pipes[0]) conn.openReadingPipe(1, pipes[1]) conn.printDetails() # conn.startListening() return conn
def radio_func(): GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) radio1 = NRF24(GPIO, spidev.SpiDev()) #Send and receive addresses pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0XE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xB5]] #begin radio and pass CSN to gpio (8/ce0) and CE to gpio 17 radio1.begin(0, 17) #Max bytes 32 radio1.setPayloadSize(32) radio1.setChannel(0x76) radio1.setDataRate(NRF24.BR_1MBPS) radio1.setPALevel(NRF24.PA_MIN) radio1.setAutoAck(False) radio1.enableDynamicPayloads() radio1.enableAckPayload() radio1.startListening() radio1.openReadingPipe(1, pipes[1]) #radio1.printDetails() while not radio1.available(): time.sleep(1 / 1000) receivedMessage = [] radio1.read(receivedMessage, radio1.getDynamicPayloadSize()) #### unpack_function is a module that translates and stores values into CB1 table Unpack.unpack_func(receivedMessage) time.sleep(1 / 1000)
def __init__(self): GPIO.setmode(GPIO.BCM) self.address = [0x44, 0x97, 0x4D, 0xE8, 0x5D] self.radio = NRF24(GPIO, spidev.SpiDev()) self.radio.begin(0, 17, 4000000) self.radio.setPayloadSize(32) self.radio.setChannel(0x43) self.radio.setDataRate(NRF24.BR_250KBPS) self.radio.setPALevel(NRF24.PA_MIN) self.radio.openWritingPipe(self.address) self.radio.stopListening()
def init_radio(): pipes=[[0xe8,0xe8,0xf0,0xf0,0xe1],[0xf0,0xf0,0xf0,0xf0,0xe1]] GPIO.setmode(GPIO.BCM) radio = NRF24(GPIO,spidev.SpiDev()) radio.begin(0,17) radio.setDataRate(NRF24.BR_2MBPS) radio.setPALevel(NRF24.PA_MAX) radio.setAutoAck(True) radio.enableDynamicPayloads() radio.openWritingPipe(pipes[0]) radio.openReadingPipe(1, pipes[1])
def build(self): # print('Beginning Radio') self.radio = NRF24(GPIO, spidev.SpiDev()) self.radio.begin(0, 17) time.sleep(0.4) self.radio.setPayloadSize(32) self.radio.setChannel(0x60) self.radio.setDataRate(NRF24.BR_250KBPS) self.radio.setPALevel(NRF24.PA_MAX) self.radio.setAutoAck(True) self.radio.enableDynamicPayloads() self.radio.enableAckPayload()
def set_parameters(self): self.radio = NRF24(GPIO, spidev.SpiDev()) self.radio.begin(0, 17) self.radio.setPayloadSize(32) self.radio.setChannel(0x76) self.radio.setDataRate(NRF24.BR_1MBPS) self.radio.setPALevel(NRF24.PA_MIN) self.radio.setAutoAck(True) self.radio.enableDynamicPayloads() self.radio.enableAckPayload()
def start_radio(): radio = NRF24(GPIO, spidev.SpiDev()) radio.begin(0, 17) time.sleep(1) radio.setRetries(15, 15) radio.setPayloadSize(32) radio.setChannel(100) radio.write_register(NRF24.FEATURE, 0) radio.setPALevel(NRF24.PA_MIN) radio.setAutoAck(False) radio.openWritingPipe(pipes[1]) radio.openReadingPipe(1, pipes[0]) radio.printDetails() return radio
def nrfSetup(): global radio 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_MAX) radio.setAutoAck(True) radio.enableDynamicPayloads() radio.enableAckPayload() radio.openReadingPipe(1,pipes[1]) radio.printDetails() radio.startListening()
def config_radio(channel, power, rate, autoAck=False, ce=25, csn=8): GPIO.setmode(GPIO.BOARD) radio = NRF24(GPIO, spidev.SpiDev()) print("[*] Starting Radio Interface...") radio.begin(BEGIN[0], BEGIN[1]) radio.setRetries(RETRY[0], RETRY[1]) radio.setPayloadSize(PACKET_LENGTH) radio.setChannel(channel) radio.setDataRate(rate) radio.setPALevel(power) radio.setAutoAck(autoAck) radio.enableDynamicPayloads() radio.enableAckPayload() radio.stopListening() radio.printDetails() return radio
def startup(self): spi = spidev.SpiDev() self.device = NRF24(GPIO, spi) self.device.begin(0, 17) time.sleep(0.1) self.device.setRetries(15, 15) self.device.setPayloadSize(32) self.device.setChannel(100) self.device.write_register(NRF24.FEATURE, 0) self.device.setPALevel(NRF24.PA_MAX) self.device.setAutoAck(True) self.device.enableDynamicPayloads() self.device.enableAckPayload() self.device.openReadingPipe(1, self.address) self.device.openWritingPipe(self.destination_address) self.telemetry = []
def __init__(self): GPIO.setmode(GPIO.BCM) print 'Init a new instance of SensorRadio' # Radio setup pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]] self.radio = NRF24(GPIO, spidev.SpiDev()) self.radio.begin(0, 25) self.radio.setRetries(15, 15) self.radio.setDataRate(NRF24.BR_1MBPS) self.radio.setPALevel(NRF24.PA_MIN) self.radio.setAutoAck(True) self.radio.enableDynamicPayloads() self.radio.openWritingPipe(pipes[0]) self.radio.openReadingPipe(1, pipes[1]) self.radio.startListening() self.radio.stopListening() self.radio.printDetails()
def init_nrf24(self): pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]] self.radio = NRF24(GPIO, self.spi) self.radio.begin(1, 13) self.radio.setPayloadSize(32) self.radio.setChannel(0x60) self.radio.setDataRate(NRF24.BR_2MBPS) self.radio.setPALevel(NRF24.PA_MIN) self.radio.setAutoAck(True) self.radio.enableDynamicPayloads() self.radio.enableAckPayload() self.radio.openWritingPipe(pipes[1]) self.radio.openReadingPipe(1, pipes[0]) self.radio.printDetails() return
def __init__(self, transmit_mode=True): # boilerplate to initialize radio if transmit_mode: CS_PIN = 0 DATA_PIN = 17 else: CS_PIN = 1 DATA_PIN = 27 GPIO.setmode(GPIO.BCM) #GPIO.setwarnings(False) # NRF24L01 has 6 pipes # all of our vehices read and write to only one pipe pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]] self.radio = NRF24(GPIO, spidev.SpiDev()) self.radio.begin(CS_PIN, DATA_PIN) time.sleep(1) #self.radio.setRetries(15,15) self.radio.setPayloadSize(32) # We can use any channel, but all cars must use the same one self.radio.setChannel(0x60) self.radio.setDataRate(NRF24.BR_2MBPS) self.radio.setPALevel(NRF24.PA_MIN) # disable automatic acknowledgment self.radio.setAutoAck(False) # allow variable message size # may consider using static payload size once message protocol is established self.radio.enableDynamicPayloads() # read and write on same pipe for everyone if transmit_mode: self.radio.openWritingPipe(pipes[1]) else: self.radio.openReadingPipe(1, pipes[1]) self.radio.startListening()
def initRadio(): radio = NRF24(GPIO, spidev.SpiDev()) print("[*] Starting Radio Interface...") radio.begin(CTE.BEGIN[0], CTE.BEGIN[1]) radio.setRetries(CTE.RETRY[0], CTE.RETRY[1]) radio.setPayloadSize(CTE.PACKET_SIZE) radio.setChannel(CTE.CHANNEL) radio.setDataRate(CTE.DATARATE) radio.setPALevel(CTE.PA_LEVEL) radio.setAutoAck(CTE.AUTO_TRACK) radio.enableDynamicPayloads() radio.enableAckPayload() radio.openWritingPipe(CTE.PIPES[0]) radio.openReadingPipe(1, CTE.PIPES[1]) radio.startListening() radio.stopListening() radio.startListening() radio.printDetails() return radio
def radio_func(str=""): rID = str print "Starting" print "Radio ID = " + rID GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) str = NRF24(GPIO, spidev.SpiDev()) start = time.time() #Send and receive addresses pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0XE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xB5]] #begin radio and pass CSN to gpio (8/ce0) and CE to gpio 17 str.begin(0, 17) #Max bytes 32 str.setPayloadSize(32) str.setChannel(0x76) str.setDataRate(NRF24.BR_1MBPS) str.setPALevel(NRF24.PA_MIN) str.setAutoAck(False) str.enableDynamicPayloads() str.enableAckPayload() str.startListening() str.openReadingPipe(1, pipes[1]) #str.printDetails() while not str.available(): time.sleep(1 / 1000) receivedMessage = [] str.read(receivedMessage, str.getDynamicPayloadSize()) #### unpack_function is a module that translates and stores values into CB1 table #### located: /usr/lib/pymodules/ICBP_modules/Unpack.py print "Radio ID passing to Unpack = " + rID print "" UnpackTest.unpack_func(receivedMessage, rID) time.sleep(100 / 1000)
def initialize_radios(csn, ce, channel): """ This function initializes the radios, each radio being the NRF24 transceivers. It gets 3 arguments, csn = Chip Select, ce = Chip Enable and the channel that will be used to transmit or receive the data.""" radio = NRF24(GPIO, spidev.SpiDev()) radio.begin(csn, ce) time.sleep(2) radio.setRetries(15, 15) radio.setPayloadSize(32) radio.setChannel(channel) radio.setDataRate(NRF24.BR_250KBPS) radio.setPALevel(NRF24.PA_MIN) radio.setAutoAck(False) radio.enableDynamicPayloads() radio.enableAckPayload() return radio
def __init__(self, csn=0, ce=17, debug=False): # RPi configs GPIO.setwarmings(False) GPIO.setmode(GPIO.BCM) # Radio setup self.radio = NRF24(GPIO, spidev.SpiDev()) self.radio.begin(csn, ce) # Set the data stream configs self.radio.serPayloadSize(32) self.radio.setChannel(0x76) self.radio.setDataRate(NRF24.BR_2MBPS) self.radio.setPALevel(NRF24.PA_MAX) # Use dynamic payloads and use Ack self.radio.setAutoAck(True) self.radio.enableDynamicPayloads() self.radio.enableAckPayload() if debug: self.radio.printDetails()
def get_radio(): GPIO.setmode(GPIO.BCM) pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]] radio = NRF24(GPIO, spidev.SpiDev()) radio.begin(0, 17) # TO-DO: Reset radio (just the line below) if program gets killed. radio.stopListening() radio.setPayloadSize(32) radio.setChannel(0x76) radio.setDataRate(NRF24.BR_250KBPS) #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() return radio
def main(): start = time.time() GPIO.setmode(GPIO.BCM) GPIO.setup(24, GPIO.OUT) GPIO.output(24, 1) GPIO.setup(22, GPIO.OUT) GPIO.output(22, 1) print("Transmitter") pipe_Tx = [0xe7, 0xe7, 0xe7, 0xe7, 0xe7] pipe_Rx = [0xc2, 0xc2, 0xc2, 0xc2, 0xc2] payloadSize = 32 channel_TX = 0x20 channel_RX = 0x25 #Initializa the radio transceivers with the CE ping connected to the GPIO22 and GPIO24 radio_Tx = NRF24(GPIO, spidev.SpiDev()) radio_Rx = NRF24(GPIO, spidev.SpiDev()) radio_Tx.begin(0, 22) radio_Rx.begin(1, 24) #We set the Payload Size to the limit which is 32 bytes radio_Tx.setPayloadSize(payloadSize) radio_Rx.setPayloadSize(payloadSize) #We choose the channels to be used for one and the other transceiver radio_Tx.setChannel(channel_TX) radio_Rx.setChannel(channel_RX) #We set the Transmission Rate radio_Tx.setDataRate(NRF24.BR_250KBPS) radio_Rx.setDataRate(NRF24.BR_250KBPS) #Configuration of the power level to be used by the transceiver radio_Tx.setPALevel(NRF24.PA_MIN) radio_Rx.setPALevel(NRF24.PA_MIN) #We disable the Auto Acknowledgement radio_Tx.setAutoAck(False) radio_Rx.setAutoAck(False) radio_Tx.enableDynamicPayloads() radio_Rx.enableDynamicPayloads() #Open the writing and reading pipe radio_Tx.openWritingPipe(pipe_Tx) radio_Rx.openReadingPipe(1, pipe_Rx) #We print the configuration details of both transceivers radio_Tx.printDetails() print( "*------------------------------------------------------------------------------------------------------------*" ) radio_Rx.printDetails() print( "*------------------------------------------------------------------------------------------------------------*" ) ############################################################################################################################### ############################################################################################################################### ############################################################################################################################### #Read file to transmit inFile = open("SampleTextFile1Mb.txt", "rb") data2Tx = inFile.read() inFile.close() #flag variables original_flag = 'A' flag = "" ctrl_flag_n = 0 flag_n = 0 #packet realted variables overhead = 1 dataSize = payloadSize - overhead dataControlSize = payloadSize - overhead #Data Packets packets = [] finalData = "" numberofPackets = 0 #ACK related variables ack = [] handshake = [] ctrl_ack = [] ack_received = 0 controlAck_received = 0 handshakeAck_received = 0 #Time variables time_ack = 0.5 start_c = time.time() #Compression of the data to transmit into encoded variable enc = lzw.ByteEncoder(20) encoding = enc.encodetobytes(data2Tx) encoded = b"".join(b for b in encoding) final_c = time.time() print(final_c - start_c) #Now we conform all the data packets in a list for i in range(0, len(encoded), dataSize): if ((i + dataSize) < len(encoded)): packets.append(encoded[i:i + dataSize]) else: packets.append(encoded[i:]) numberofPackets += 1 #Start sendind radio_Tx.write(str(numberofPackets)) timeout = time.time() + time_ack radio_Rx.startListening() str_Handshake = "" #While we don't receive the handshake ack we keep trying while not (handshakeAck_received): if radio_Rx.available(0): radio_Rx.read(handshake, radio_Rx.getDynamicPayloadSize()) for c in range(0, len(handshake)): str_Handshake = str_Handshake + chr(handshake[c]) #If the received ACK does not match the expected one we retransmit, else we set the received handshake ack to 1 if ( list(str_Handshake) != list("ACK") ): #####Can we avoid the for above? using directly ack received from .read() radio_Tx.write(str(numberofPackets)) timeout = time.time() + time_ack print("Handshake Message Lost") str_Handshake = "" else: print("Handshake done") handshakeAck_received = 1 #If an established time passes and we have not received anything we retransmit the handshake packet if ((time.time() + 0.01) > timeout): print("No Handshake ACK received resending message") radio_Tx.write(str(numberofPackets)) timeout = time.time() + time_ack #We iterate over every packet to be sent for message in packets: flag = chr(ord(original_flag) + flag_n) message2Send = str(flag) + message radio_Tx.write(list(message2Send)) #time.sleep(1) timeout = time.time() + time_ack radio_Rx.startListening() str_ack = "" #While we don't receive a correct ack for the transmitted packet we keep trying for the same packet while not (ack_received): if radio_Rx.available(0): radio_Rx.read(ack, radio_Rx.getDynamicPayloadSize()) for c in range(0, len(ack)): str_ack = str_ack + chr(ack[c]) #If the received ACK does not match the expected one we retransmit, else we set the received data ack to 1 if (list(str_ack) != (list("ACK") + list(flag))): radio_Tx.write(list(message2Send)) timeout = time.time() + time_ack print( "Data ACK received but not the expected one --> resending message" ) str_ack = "" else: ack_received = 1 #If an established time passes and we have not received anything we retransmit the data packet if ((time.time() + 0.2) > timeout): print("No Data ACK received resending message") radio_Tx.write(list(message2Send)) timeout = time.time() + time_ack ack_received = 0 flag_n = (flag_n + 1) % 10 final = time.time() totalTime = final - start print(totalTime)
m = float(float(resolution - 1) / float(x2 - x1)) xT = float(m) * float(xin - x1) return xT sleep(15) GPIO.setmode(GPIO.BCM) # set GPIO mode as BCM mode (not physical) pipes = [ [0xE8, 0xE8, 0xF0, 0xF0, 0xE1], # where the data is going to be stored for writing/reading [0xF0, 0xF0, 0xF0, 0xF0, 0xE1] ] # this is the address on the NRF board radio = NRF24(GPIO, spidev.SpiDev()) # initialize radio object radio.begin(0, 17) # begin the radio setup radio.setPayloadSize(32) # set maximum payload size to 32 bytes (max) radio.enableDynamicPayloads( ) # enable dynamic payloads (shouldn't need it practically, but it makes for a good demo radio.setChannel(0x76) # set the channel to transmit/receive on radio.setDataRate(NRF24.BR_1MBPS) # set the data transmission rate radio.setPALevel(NRF24.PA_MAX) # set the amplifier power to maximum radio.setAutoAck(True) # set Auto Acknowledge (mostly for debugging) radio.enableAckPayload() # enable the acknowledge payload radio.openWritingPipe(pipes[0]) # open the corresponding pipe for writing while (1): # creates an infinite loop to continually retrieve and output data
########################Modules Path Information################################ ########################Modules Path Information################################ from lib_nrf24 import NRF24 import RPi.GPIO as GPIO import spidev import array i = 0 while (i < 10): str = "radio1" GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) str = NRF24(GPIO, spidev.SpiDev()) start = time.time() #Send and receive addresses pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0XE1]] #begin radio and pass CSN to gpio (8/ce0) and CE to gpio 17 str.begin(0, 17) #Max bytes 32 str.setPayloadSize(32) str.setChannel(0x76) str.setDataRate(NRF24.BR_1MBPS) str.setPALevel(NRF24.PA_MIN) str.setAutoAck(False) str.enableDynamicPayloads()
def main(): GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(22, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(2, GPIO.OUT) #LED 1 TX_RX Running GPIO.setup(3, GPIO.OUT) #LED 2 End-of-File GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #ON or OFF GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Transmit or Receive GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Network Mode GPIO.output(2, 0) GPIO.output(3, 0) TX0_RX1 = True global blink while True: input_onoff = GPIO.input(14) blink = 1 led_thread2 = Thread(target=led_blink, args=( 2, 1, )) if (input_onoff == False): time.sleep(1) print("Waiting to start") led_thread2.start() else: blink = 0 break TX_RX = GPIO.input(15) NM = GPIO.input(18) if (not NM): #Single Mode Code ######################################## ######################################## ######################################## if (TX_RX): print("Transmitter") pipe_Tx = [0xe7, 0xe7, 0xe7, 0xe7, 0xe7] pipe_Rx = [0xc2, 0xc2, 0xc2, 0xc2, 0xc2] payloadSize = 32 channel_TX = 30 channel_RX = 50 #Initializa the radio transceivers with the CE ping connected to the GPIO22 and GPIO24 radio_Tx = NRF24(GPIO, spidev.SpiDev()) radio_Rx = NRF24(GPIO, spidev.SpiDev()) radio_Tx.begin(0, 22) radio_Rx.begin(1, 23) #We set the Payload Size to the limit which is 32 bytes radio_Tx.setPayloadSize(payloadSize) radio_Rx.setPayloadSize(payloadSize) #We choose the channels to be used for one and the other transceiver radio_Tx.setChannel(channel_TX) radio_Rx.setChannel(channel_RX) #We set the Transmission Rate radio_Tx.setDataRate(NRF24.BR_2MBPS) radio_Rx.setDataRate(NRF24.BR_2MBPS) #Configuration of the power level to be used by the transceiver radio_Tx.setPALevel(NRF24.PA_MAX) radio_Rx.setPALevel(NRF24.PA_MAX) #CRC Length radio_Tx.setCRCLength(NRF24.CRC_8) radio_Rx.setCRCLength(NRF24.CRC_8) #We disable the Auto Acknowledgement radio_Tx.setAutoAck(False) radio_Rx.setAutoAck(False) radio_Tx.enableDynamicPayloads() radio_Rx.enableDynamicPayloads() #Open the writing and reading pipe radio_Tx.openWritingPipe(pipe_Tx) radio_Rx.openReadingPipe(0, pipe_Rx) ############################################################################################################################### ############################################################################################################################### ############################################################################################################################### #Read file to transmit inFile = open("MTP_Prev.txt", "rb") data2Tx = inFile.read() inFile.close() #flag variables original_flag = 'A' flag = "" ctrl_flag_n = 0 flag_n = 0 #packet realted variables overhead = 1 dataSize = payloadSize - overhead dataControlSize = payloadSize - overhead #Data Packets packets = [] finalData = "" numberofPackets = 0 #ACK related variables ack = [] handshake = [] ctrl_ack = [] ack_received = 0 controlAck_received = 0 handshakeAck_received = 0 #Time variables time_ack = 0.02 #LED Blinking thread led_thread = Thread(target=led_blink, args=(2, 0.3)) #Compression of the data to transmit into data2Tx_compressed data2Tx_compressed = compress(data2Tx) #Compression ######################################################################################################### listLengh = len(data2Tx_compressed) listMax = max(data2Tx_compressed) bitsMax = int(np.ceil(np.log(listMax + 1) / np.log(2))) charLength = 8 data2Tx_compressed.append(0) remainded = bitsMax pad = bitsMax - charLength toSend = "" i = 0 while i < listLengh: compJoin = (data2Tx_compressed[i] << bitsMax) + data2Tx_compressed[i + 1] toSend += chr((compJoin >> (pad + remainded)) % (2**charLength)) remainded = remainded - charLength if remainded <= 0: i = i + 1 remainded = remainded % bitsMax if remainded == 0: remainded = bitsMax ######################################################################################################################## #Now we conform all the data packets in a list for i in range(0, len(toSend), dataSize): if ((i + dataSize) < len(toSend)): packets.append(toSend[i:i + dataSize]) else: packets.append(toSend[i:]) numberofPackets += 1 #Start sendind Handshake Packet handshakePacket = str(numberofPackets) + "," + str( listLengh) + "," + str(listMax) radio_Tx.write(handshakePacket) timeout = time.time() + time_ack radio_Rx.startListening() str_Handshake = "" blink = 1 led_thread.start() print("Starting Script") ############################################################################################################################### ############################################################################################################################### ############################################################################################################################### #While we don't receive the handshake ack we keep trying while not (handshakeAck_received): if radio_Rx.available(0): radio_Rx.read(handshake, radio_Rx.getDynamicPayloadSize()) #print("Something Received") for c in range(0, len(handshake)): str_Handshake = str_Handshake + chr(handshake[c]) #If the received ACK does not match the expected one we retransmit, else we set the received handshake ack to 1 if (list(str_Handshake) != list("ACK")): radio_Tx.write(handshakePacket) timeout = time.time() + time_ack #print("Handshake Message Lost") str_Handshake = "" else: #print("Handshake done") handshakeAck_received = 1 #If an established time passes and we have not received anything we retransmit the handshake packet if ((time.time()) > timeout): #print("No Handshake ACK received resending message") radio_Tx.write(handshakePacket) timeout = time.time() + time_ack messageSent = "" #We iterate over every packet to be sent suma = 0 for message in packets: messageSent += message flag = chr(ord(original_flag) + flag_n) message2Send = list(flag) + list(message) radio_Tx.write(message2Send) timeout = time.time() + time_ack str_ack = "" #While we don't receive a correct ack for the transmitted packet we keep trying for the same packet while not (ack_received): if radio_Rx.available(0): radio_Rx.read(ack, radio_Rx.getDynamicPayloadSize()) for c in range(0, len(ack)): str_ack = str_ack + chr(ack[c]) #If the received ACK does not match the expected one we retransmit, else we set the received data ack to 1 if (list(str_ack) != (list("ACK") + list(flag))): radio_Tx.write(message2Send) timeout = time.time() + time_ack suma += 1 str_ack = "" if (suma > 50): time_ack += 0.05 suma = 0 if (time_ack > 0.2): time_ack = 0.2 else: ack_received = 1 #If an established time passes and we have not received anything we retransmit the data packet if ((time.time()) > timeout): suma += 1 radio_Tx.write(message2Send) timeout = time.time() + time_ack ack_received = 0 flag_n = (flag_n + 1) % 10 blink = 0 GPIO.output(3, 1) radio_Rx.stopListening() radio_Tx.end() radio_Rx.end() GPIO.output(22, 0) GPIO.output(23, 0) time.sleep(2) GPIO.cleanup() else: print("Receiver") pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]] payloadSize = 32 channel_RX = 30 channel_TX = 50 #Initializa the radio transceivers with the CE ping connected to the GPIO22 and GPIO24 radio_Tx = NRF24(GPIO, spidev.SpiDev()) radio_Rx = NRF24(GPIO, spidev.SpiDev()) radio_Tx.begin(0, 22) radio_Rx.begin(1, 23) #We set the Payload Size to the limit which is 32 bytes radio_Tx.setPayloadSize(payloadSize) radio_Rx.setPayloadSize(payloadSize) #We choose the channels to be used for one and the other transceiver radio_Tx.setChannel(channel_TX) radio_Rx.setChannel(channel_RX) #We set the Transmission Rate radio_Tx.setDataRate(NRF24.BR_2MBPS) radio_Rx.setDataRate(NRF24.BR_2MBPS) #Configuration of the power level to be used by the transceiver radio_Tx.setPALevel(NRF24.PA_MAX) radio_Rx.setPALevel(NRF24.PA_MAX) #CRC Length radio_Tx.setCRCLength(NRF24.CRC_8) radio_Rx.setCRCLength(NRF24.CRC_8) #We disable the Auto Acknowledgement radio_Tx.setAutoAck(False) radio_Rx.setAutoAck(False) radio_Tx.enableDynamicPayloads() radio_Rx.enableDynamicPayloads() #Open the writing and reading pipe radio_Tx.openWritingPipe(pipes[1]) radio_Rx.openReadingPipe(0, pipes[0]) ############################################################################################################################### ############################################################################################################################### ############################################################################################################################### #Flag variables original_flag_data = 'A' flag = "" flag_n = 0 ctrl_flag_n = 0 #Packet related variables frame = [] handshake_frame = [] compressed = [] str_compressed = "" #ACK related variables time_ack = 0.02 receivedPacket = 0 receivedHandshakePacket = 0 #LED Blinking thread led_thread = Thread(target=led_blink, args=(2, 0.3)) radio_Rx.startListening() #We listen for the control packet while not (receivedHandshakePacket): str_Handshakeframe = "" if radio_Rx.available(0): radio_Rx.read(handshake_frame, radio_Rx.getDynamicPayloadSize()) for c in range(0, len(handshake_frame)): str_Handshakeframe = str_Handshakeframe + chr( handshake_frame[c]) #print("Handshake frame: " + str_Controlframe) if (len(str_Handshakeframe.split(",")) == 3): radio_Tx.write(list("ACK")) numberOfPackets, listLength, listMax = str_Handshakeframe.split( ",") listLength = int(listLength) listMax = int(listMax) else: if (chr(handshake_frame[0]) == original_flag_data): handshake_frame = handshake_frame[ 1:len(handshake_frame)] compressed.extend(handshake_frame) blink = 1 led_thread.start() radio_Tx.write( list("ACK") + list(original_flag_data)) flag_n = (flag_n + 1) % 10 receivedHandshakePacket = 1 bitsMax = int(np.ceil(np.log(listMax + 1) / np.log(2))) for i in range(0, int(numberOfPackets) - 1): timeout = time.time() + time_ack flag = chr(ord(original_flag_data) + flag_n) while not (receivedPacket): if radio_Rx.available(0): radio_Rx.read(frame, radio_Rx.getDynamicPayloadSize()) #print(frame) if (chr(frame[0]) == flag): compressed.extend(frame[1:len(frame)]) if (((len(compressed) * 8) % (bitsMax * 300)) == 0): thread = Thread( target=decompressionOnTheGo, args=(compressed, listMax)) thread.start() radio_Tx.write(list("ACK") + list(flag)) receivedPacket = 1 else: if flag_n == 0: radio_Tx.write(list("ACK") + list('J')) else: radio_Tx.write( list("ACK") + list( chr( ord(original_flag_data) + flag_n - 1))) timeout = time.time() + time_ack flag_n = (flag_n + 1) % 10 receivedPacket = 0 thread = Thread(target=decompressionOnTheGo, args=(compressed, listMax)) thread.start() blink = 0 GPIO.output(3, 1) radio_Rx.stopListening() radio_Tx.end() radio_Rx.end() GPIO.output(22, 0) GPIO.output(23, 0) time.sleep(2) GPIO.cleanup() else: print("Network Mode") NetMode.main_nm()
while a < 5: radio.write(mensaje1) print("On") a = a + 1 elif (armada == True): escribirTxt('Alarma Desactivada!') radio.stopListening() print("Off") armada = not armada time.sleep(2) pipes = [[0xe8, 0xe8, 0xf0, 0xf0, 0xe1], [0xf0, 0xf0, 0xf0, 0xf0, 0xe1]] radio = NRF24(GPIO, SPI.SpiDev()) radio.begin(0, 25) radio.setRetries(15, 15) radio.setPayloadSize(32) radio.setChannel(0x4c) radio.setDataRate(NRF24.BR_1MBPS) radio.setPALevel(NRF24.PA_MAX) radio.openReadingPipe(1, pipes[1]) radio.openWritingPipe(pipes[0]) radio.printDetails() #Opcional radio.powerUp() mensaje1 = list("on") while len(mensaje1) < 32:
#!/usr/bin/python # -*- coding: utf-8 -*- # # Example program to send packets to the radio link # import virtGPIO as GPIO from lib_nrf24 import NRF24 import time pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]] radio = NRF24(GPIO, GPIO.SpiDev()) radio.begin(10, 8) #Set spi-ce pin10, and rf24-CE pin 8 time.sleep(1) radio.setRetries(15, 15) radio.setPayloadSize(32) radio.setChannel(0x60) radio.setDataRate(NRF24.BR_2MBPS) radio.setPALevel(NRF24.PA_MIN) radio.setAutoAck(True) radio.enableDynamicPayloads() radio.enableAckPayload() radio.openWritingPipe(pipes[1]) radio.openReadingPipe(1, pipes[0]) radio.printDetails() c = 1 while True:
def main(): GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(22, GPIO.OUT, initial=GPIO.LOW) print("Transmitter") pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]] payloadSize = 32 channel_TX = 0x40 channel_RX = 0x45 #Initializa the radio transceivers with the CE ping connected to the GPIO22 and GPIO23 radio_Tx = NRF24(GPIO, spidev.SpiDev()) radio_Rx = NRF24(GPIO, spidev.SpiDev()) radio_Tx.begin(0, 22) radio_Rx.begin(1, 24) #We set the Payload Size to the limit which is 32 bytes radio_Tx.setPayloadSize(payloadSize) radio_Rx.setPayloadSize(payloadSize) #We choose the channels to be used for one and the other transceiver radio_Tx.setChannel(channel_TX) radio_Rx.setChannel(channel_RX) #We set the Transmission Rate radio_Tx.setDataRate(NRF24.BR_250KBPS) radio_Rx.setDataRate(NRF24.BR_250KBPS) #Configuration of the power level to be used by the transceiver radio_Tx.setPALevel(NRF24.PA_MIN) radio_Rx.setPALevel(NRF24.PA_MIN) #We disable the Auto Acknowledgement radio_Tx.setAutoAck(False) radio_Rx.setAutoAck(False) radio_Tx.enableDynamicPayloads() radio_Rx.enableDynamicPayloads() #Open the writing and reading pipe radio_Tx.openWritingPipe(pipes[1]) radio_Rx.openReadingPipe(0, pipes[0]) #We print the configuration details of both transceivers print( "Transmitter Details #################################################################################" ) radio_Tx.printDetails() print( "*---------------------------------------------------------------------------------------------------*" ) print( "Receiver Details ####################################################################################" ) radio_Rx.printDetails() print( "*---------------------------------------------------------------------------------------------------*" ) ############################################################################################################################### ############################################################################################################################### ############################################################################################################################### #Read file to transmit #inFile = open("SampleTextFile1Mb.txt", "rb") inFile = open("ElQuijote.txt", "rb") data2Tx = inFile.read() inFile.close() #flag variables original_flag_data = 'A' flag = "" flag_n = 0 #packet realted variables overhead = 1 dataSize = payloadSize - overhead dataControlSize = payloadSize - overhead #Data Packets packets = [] numberofPackets = 0 #ACK related variables ack = [] handshake = [] ack_received = 0 handshakeAck_received = 0 #Time variables time_ack = 1 start_c = time.time() #Compression of the data to transmit into data2Tx_compressed data2Tx_compressed = compress(data2Tx) n = len(bin(max(data2Tx_compressed))) - 2 #We create the string with the packets needed to decompress the file transmitted controlList_extended = [] controlList = [] for val in data2Tx_compressed: division = int(val / 256) controlList.append(division) if (n > 16): for val in controlList: division = int(val / 256) controlList_extended.append(division) data2Send = [] for iterator in range(0, len(controlList)): data2Send.append(data2Tx_compressed[iterator]) data2Send.append(controlList[iterator]) if (n > 16): data2Send.append(controlList_extended[iterator]) final_c = time.time() print("Compression time: " + str(final_c - start_c)) #Now we conform all the data packets in a list for i in range(0, len(data2Send), dataSize): if ((i + dataSize) < len(data2Send)): packets.append(data2Send[i:i + dataSize]) else: packets.append(data2Send[i:]) numberofPackets += 1 #Start time start = time.time() radio_Rx.startListening() radio_Tx.write(str(numberofPackets) + "," + str(n)) timeout = time.time() + time_ack str_Handshake = "" #While we don't receive the handshake ack we keep trying while not (handshakeAck_received): if radio_Rx.available(0): radio_Rx.read(handshake, radio_Rx.getDynamicPayloadSize()) print("Something received") for c in range(0, len(handshake)): str_Handshake = str_Handshake + chr(handshake[c]) #If the received ACK does not match the expected one we retransmit, else we set the received handshake ack to 1 if (list(str_Handshake) != list("ACK")): radio_Tx.write(str(numberofPackets) + "," + str(n)) timeout = time.time() + time_ack print("Handshake Message Lost") str_Handshake = "" else: print("Handshake done") handshakeAck_received = 1 #If an established time passes and we have not received anything we retransmit the handshake packet if ((time.time() + 0.2) > timeout): print("No Handshake ACK received resending message") radio_Tx.write(str(numberofPackets) + "," + str(n)) timeout = time.time() + time_ack #We iterate over every packet to be sent dec_ready = 0 for message in packets: flag = chr(ord(original_flag_data) + flag_n) message2Send = list(flag) + message radio_Tx.write(message2Send) time.sleep(1) if (dec_ready == 200): time.sleep(0.3) dec_ready = 0 timeout = time.time() + time_ack radio_Rx.startListening() str_ack = "" #While we don't receive a correct ack for the transmitted packet we keep trying for the same packet while not (ack_received): if radio_Rx.available(0): radio_Rx.read(ack, radio_Rx.getDynamicPayloadSize()) for c in range(0, len(ack)): str_ack = str_ack + chr(ack[c]) print(str_ack) #If the received ACK does not match the expected one we retransmit, else we set the received data ack to 1 if (list(str_ack) != (list("ACK") + list(flag))): radio_Tx.write(list(flag) + list(message)) timeout = time.time() + time_ack #print("Data ACK received but not the expected one --> resending message") str_ack = "" else: ack_received = 1 #If an established time passes and we have not received anything we retransmit the data packet if ((time.time() + 0.01) > timeout): print("No Data ACK received resending message") radio_Tx.write(message2Send) timeout = time.time() + time_ack dec_ready = 0 ack_received = 0 flag_n = (flag_n + 1) % 10 final = time.time() totalTime = final - start print(totalTime) GPIO.output(22, 0) GPIO.output(23, 0)
import logging ''' set up GPIO numbering system in the format of BCM. 2 format available. GPIO.Board & GPIO.BCM ''' # Use GPIO numbers not pin numbers GPIO.setmode(GPIO.BCM) #setup addresses for transceiver #there is 2 addresses. 1 for send address,another one for recieve address pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]] #set up radio and activate radio = NRF24(GPIO, spidev.SpiDev()) #(CSN, CE) .CSN =CE_0(GPIO8) radio.begin(0, 25) #maximum size is actually 32byte radio.setPayloadSize(32) radio.setChannel(0x76) #consider slower and more secure data rate. slower actually give better range radio.setDataRate(NRF24.BR_1MBPS) #set power level. since arduino and raspberry pi is near each other. min power will do. radio.setPALevel(NRF24.PA_MAX) radio.setAutoAck(True) radio.enableDynamicPayloads() radio.enableAckPayload() #input 2nd address for reading pipe. This is for receiving data
pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]] GPIO.setmode(GPIO.BCM) # Comment re multiple SPIDEV devices: # Official spidev documentation is sketchy. Implementation in virtGPIO allows multiple SpiDev() objects. # This may not work on RPi? Probably RPi uses alternating open() / xfer2() /close() within one SpiDev() object??? # On virtGPIO each of multiple SpiDev() stores its own mode and cePin. Multiple RF24 used here becomes easy. # This issue affects only using MULTIPLE Spi devices. ################################################################## # SET UP RADIO1 - PTX radio1 = NRF24(GPIO, spidev.SpiDev()) radio1.begin(0, 17) # SPI-CE=RF24-CSN=pin9, no RF24-CE pin time.sleep(1) radio1.setRetries(15,15) radio1.setPayloadSize(32) radio1.setChannel(0x62) radio1.setDataRate(NRF24.BR_2MBPS) radio1.setPALevel(NRF24.PA_MIN) radio1.setAutoAck(True) radio1.enableDynamicPayloads() radio1.enableAckPayload() radio1.openWritingPipe(pipes[1]) radio1.openReadingPipe(1, pipes[0]) if not radio1.isPVariant():