def on_rx_done(self): BOARD.led_on() print("\nRxDone") self.clear_irq_flags(RxDone=1) payload = self.read_payload(nocheck=True) print(bytes(payload).decode()) self.set_mode(MODE.SLEEP) self.reset_ptr_rx() BOARD.led_off() self.set_mode(MODE.RXCONT)
def start(self): global args sys.stdout.write("\rstart") self.tx_counter = 0 BOARD.led_on() pl = b"Winter is coming" self.write_payload([ord(elem) for elem in pl]) # self.write_payload([0x0f]) self.set_mode(MODE.TX) while True: sleep(1)
def on_rx_done(self): BOARD.led_on() print("Entra a rx") self.clear_irq_flags(RxDone=1) #if DEBUG_MODE: sys.stdout.write("SND_ADDR", SND_ADDR) ackn = self.read_payload(nocheck=True) ack=''.join(chr(i) for i in ackn) #print(bytes(payload).decode()) print(ack) self.set_mode(MODE.SLEEP) self.reset_ptr_rx() BOARD.led_off() self.set_mode(MODE.RXCONT)
def start(self): self.m_counter = 1 global args sys.stdout.write("\rstart") self.tx_counter = 0 BOARD.led_on() pl = b"Hello from Raspberry message: " + str(self.m_counter) self.write_payload([ord(elem) for elem in pl]) # self.write_payload([0x0f]) self.set_mode(MODE.TX) #if(self.tx_counter==5): # self.set_mode(SLEEP) while True: sleep(1) return (self.tx_counter)
def on_tx_done(self): global args self.set_mode(MODE.STDBY) self.clear_irq_flags(TxDone=1) sys.stdout.flush() self.tx_counter += 1 sys.stdout.write("\rtx #%d" % self.tx_counter) if args.single: print sys.exit(0) BOARD.led_off() sleep(args.wait) pl = b"Winter is coming" self.write_payload([ord(elem) for elem in pl]) BOARD.led_on() self.set_mode(MODE.TX)
def on_tx_done(self): global args print("Entra Aqui") self.set_mode(MODE.STDBY) self.clear_irq_flags(TxDone=1) sys.stdout.flush() self.tx_counter += 1 sys.stdout.write("\rtx #%d" % self.tx_counter) if args.single: print sys.exit(0) BOARD.led_off() sleep(args.wait) pl = b"Hello from Raspberry message: " + str(self.m_counter) self.write_payload([ord(elem) for elem in pl]) self.m_counter = self.m_counter + 1 BOARD.led_on() self.set_mode(MODE.TX)
# # You can be released from the requirements of the license by obtaining a commercial license. Such a license is # mandatory as soon as you develop commercial activities involving pyhoperf without disclosing the source code of your # own applications, or shipping pyhoperf with a closed source product. # # You should have received a copy of the GNU General Public License along with pySX127. If not, see # <http://www.gnu.org/licenses/>. from time import sleep from hoperf.LoRa import * from hoperf.LoRaArgumentParser import LoRaArgumentParser from hoperf.board_config import BOARD DEBUG_MODE = True BOARD.setup() parser = LoRaArgumentParser("Continous LoRa receiver.") class LoRaRcvCont(LoRa): #def __init__(self): #super(LoRaRcvCont, self).__init__(verbose) def on_rx_done(self): BOARD.led_on() print("Entra a rx") self.clear_irq_flags(RxDone=1) #if DEBUG_MODE: sys.stdout.write("SND_ADDR", SND_ADDR) ackn = self.read_payload(nocheck=True) ack=''.join(chr(i) for i in ackn)
def trans(self, payload, SND_ADDR, RCV_ADDR): if DEBUG_MODE: print("DEBUG: Transmission Function") self.frec = 0 self.env = 1 self.set_mode(MODE.SLEEP) self.set_dio_mapping([1, 0, 0, 0, 0, 0]) global args #signal.signal(signal.SIGALRM, self.handler) self.flag = 0 self.payload = payload self.rec_add = RCV_ADDR self.seqnum = 0 self.acknum = 0 self.sent = 0 self.retrans = 0 self.flagrec = 0 self.flagn = 0 self.nsent = 0 self.timeout_time = 1 # 1 second self.estimated_rtt = -1 self.dev_rtt = 1 self.tx_counter = 0 self.frx = 0 sys.stdout.write("\rstart") BOARD.led_on() if DEBUG_MODE: print("RCV_ADDR", RCV_ADDR) self.snd_add = SND_ADDR[ 8:] #We don't need all the address, only the last bytes self.rec_add = RCV_ADDR[8:] if DEBUG_MODE: print("New RCV_ADDR", self.rec_add) if DEBUG_MODE: print("SND_ADDR", self.snd_add) # Reads first block from string "payload" self.text = self.payload[ 0:self. PAYLOAD_SIZE] # Copying PAYLOAD_SIZE bytes header from the input string self.payload = self.payload[ self.PAYLOAD_SIZE:] # Shifting the input string # Checking if this is the last packet if (len(self.text) == self.PAYLOAD_SIZE) and (len(self.payload) > 0): self.last_pkt = False else: self.last_pkt = True self.flagrec = 0 #Making the packet packet = self.make_packet(self.snd_add, self.rec_add, self.seqnum, self.acknum, DATA_PACKET, self.last_pkt, self.text) self.write_payload([ord(elem) for elem in packet ]) #Sending the packet through LoRa if DEBUG_MODE: self.debug_printpacket("sending 1st", packet) self.send_time = time.time() #Check the sending time time.sleep(.5) self.set_mode(MODE.TX) #Setting the board in transmission mode self.sent += 1 self.inside = False #time.sleep(1) while (True): if self.frec == 1: signal.alarm(10) time.sleep(.5) if (self.flag == 1): time.sleep(1) self.set_mode(MODE.STDBY) self.set_mode(MODE.SLEEP) break return (self.sent, self.retrans, self.sent)
def on_tx_done(self): global args if (self.env == 1 ): #Flag to check if this is transmission or reception if DEBUG_MODE: print("DEBUG: transmission") #print(self.frx) self.a = 1 self.set_mode(MODE.STDBY) self.clear_irq_flags(TxDone=1) sys.stdout.flush() #packet="Paquete 2" if (self.flag == 0): #Checking if it's the last packet self.set_mode(MODE.TX) else: self.set_mode(MODE.STDBY) self.clear_irq_flags(TxDone=1) self.set_mode(MODE.SLEEP) time.sleep(.5) if (self.frx == 0): #Reception Flag if DEBUG_MODE: print("DEBUG: Reception Flag", self.frx) self.set_mode(MODE.SLEEP) self.set_dio_mapping([0] * 6) self.reset_ptr_rx() BOARD.led_off() self.set_mode(MODE.RXCONT) #Changing Mode to RX elif (self.frx == 2): #No response, resending the packet if DEBUG_MODE: print("DEBUG: Flag Reception", self.frx) packet = self.make_packet(self.snd_add, self.rec_add, self.seqnum, self.acknum, DATA_PACKET, self.last_pkt, self.text) self.write_payload([ord(elem) for elem in packet]) self.flagn += 1 if DEBUG_MODE: self.debug_printpacket("re-sending packet: ", packet) if DEBUG_MODE: print("From rasp Flag Number: ", self.flagn) self.sent += 1 self.retrans += 1 if (self.flagn == 3): #AM: We resend the packet 3 times self.inside = True self.set_mode(MODE.SLEEP) self.frx = 0 elif (self.frx == 1): #First packet sent, so we send a new packet if DEBUG_MODE: print("DEBUG: Flag Reception", self.frx) self.sample_rtt = self.recv_time - self.send_time if self.estimated_rtt == -1: self.estimated_rtt = self.sample_rtt else: self.estimated_rtt = self.estimated_rtt * 0.875 + self.sample_rtt * 0.125 self.dev_rtt = 0.75 * self.dev_rtt + 0.25 * abs( self.sample_rtt - self.estimated_rtt) if DEBUG_MODE: print("Payload left", self.payload) self.text = self.payload[ 0:self. PAYLOAD_SIZE] # Copying PAYLOAD_SIZE bytes header from the input string if DEBUG_MODE: print("Payload to send", self.text) self.payload = self.payload[ self.PAYLOAD_SIZE:] # Shifting the input string if DEBUG_MODE: print("Payload left", self.payload) if (len(self.text) == self.PAYLOAD_SIZE) and (len(self.payload) > 0): self.last_pkt = False else: self.last_pkt = True self.flagrec = 0 # Increment sequence and ack numbers self.seqnum += 1 self.acknum += 1 RCV_ADDR = self.rcv2 packet = self.make_packet(self.snd_add, RCV_ADDR, self.seqnum, self.acknum, DATA_PACKET, self.last_pkt, self.text) self.write_payload([ord(elem) for elem in packet]) self.sent += 1 if DEBUG_MODE: self.debug_printpacket("sending new packet", packet, True) signal.alarm(10) #Setting timeout to 10 seconds self.frx = 0 elif (self.env == 0): if DEBUG_MODE: print("DEBUG: Receiving") self.a = 1
def on_rx_done(self): #signal.alarm(10) if DEBUG_MODE: print("DEBUG: on_rx_done function") BOARD.led_on() if (self.env == 0): if DEBUG_MODE: print("DEBUG: Reception") if DEBUG_MODE: print("DEBUG: From Swlp My Address: ", self.MY_ADDR) packet_raw = self.read_payload(nocheck=True) #Receiving the packet packet = ''.join(chr(i) for i in packet_raw) #Changing the packet format if DEBUG_MODE: print("DEBUG: packet", packet) if (packet == ""): if DEBUG_MODE: print("ERROR: packet received not valid") else: if (self.fpacket == True): self.source_addr, dest_addr, seqnum, acknum, ack, self.last_pkt, check, content = self.unpack( packet) #Unpacking the packet address_check = dest_addr if (dest_addr == self.MY_ADDR): self.flag_recv = True else: if DEBUG_MODE: self.debug_printpacket( "DISCARDED received packet; not for me!!", packet) self.set_mode(MODE.SLEEP) if (self.flag_recv == True): if DEBUG_MODE: self.debug_printpacket("received 1st packet", packet, True) checksum_OK = (check == self.get_checksum(content)) if (checksum_OK) and (self.next_acknum == acknum): packet_valid = True self.rcvd_data += content #putting the content of the packet together self.next_acknum += 1 if DEBUG_MODE: print("DEBUG: Data So far: ", self.rcvd_data) #if DEBUG_MODE: print("DEBUG: acknum So far: ", self.next_acknum) else: packet_valid = False # Sending first ACK ack_segment = self.make_packet(self.MY_ADDR, self.source_addr, seqnum, acknum, packet_valid, self.last_pkt, "") self.write_payload([ord(elem) for elem in ack_segment]) time.sleep(.5) self.set_mode(MODE.SLEEP) self.set_dio_mapping([1, 0, 0, 0, 0, 0]) self.set_mode(MODE.TX) #Changing to TX mode if DEBUG_MODE: self.debug_printpacket("sent 1st ACK", ack_segment) self.fpacket = False time.sleep(2) self.set_mode(MODE.SLEEP) self.set_dio_mapping([0] * 6) self.reset_ptr_rx() BOARD.led_off() self.set_mode(MODE.RXCONT) #Changing to RX mode again if (self.fpacket == False and self.last_pkt == False): #if DEBUG_MODE: print("DEBUG: acknum Of the second Packet: ", self.next_acknum) self.source_addr, dest_addr, seqnum, acknum, ack, self.last_pkt, check, content = self.unpack( packet) if (dest_addr == self.MY_ADDR): #Packet for me if DEBUG_MODE: self.debug_printpacket("received packet", packet, True) checksum_OK = (check == self.get_checksum(content)) if DEBUG_MODE: print("checksum_OK", checksum_OK) # ACK the packet if it's correct; otherwise send NAK. if (checksum_OK) and (self.next_acknum == acknum): packet_valid = True self.rcvd_data += content self.next_acknum += 1 if DEBUG_MODE: print("DEBUG: Data So far: ", self.rcvd_data) else: packet_valid = False ack_segment = self.make_packet( self.MY_ADDR, self.source_addr, seqnum, acknum, packet_valid, self.last_pkt, "") #Making ACK self.write_payload([ord(elem) for elem in ack_segment]) time.sleep(1) self.set_mode(MODE.SLEEP) self.set_dio_mapping([1, 0, 0, 0, 0, 0]) self.set_mode(MODE.TX) #Changing to TX mode if DEBUG_MODE: self.debug_printpacket("sending ACK", ack_segment) #if DEBUG_MODE: print("DEBUG: last_pkt so far: ", self.last_pkt) if (self.last_pkt == False): time.sleep(.5) self.set_mode(MODE.SLEEP) self.set_dio_mapping([0] * 6) self.reset_ptr_rx() BOARD.led_off() self.set_mode(MODE.RXCONT) else: time.sleep(.5) self.set_mode(MODE.SLEEP) self.flag = 1 #Is the last packet we can exit the function else: if DEBUG_MODE: self.debug_printpacket( "DISCARDED received packet; not for me!!", packet) else: time.sleep(.5) self.set_mode(MODE.SLEEP) self.flag = 1 elif (self.env == 1): #signal.alarm(10) if DEBUG_MODE: print("DEBUG: Waiting for ACK") if DEBUG_MODE: print("SND_ADDR", self.snd_add) #print("\nRxDone") #print(self.get_irq_flags()) ackn = self.read_payload(nocheck=True) ack = ''.join(chr(i) for i in ackn) if DEBUG_MODE: print("ack", ack) if (ack == ""): print("ERROR: packet received not valid") else: try: #Unpacking the packet ack_source_addr, ack_dest_addr, ack_seqnum, ack_acknum, ack_is_ack, ack_final, ack_check, ack_content = self.unpack( ack) self.recv_time = time.time() #Checking the reception time if (ack_seqnum == 0 and self.flagrec == 0): self.rcv2 = ack_source_addr self.flagrec = 1 if DEBUG_MODE: print("DEBUG: rcv2", self.rcv2) if DEBUG_MODE: self.debug_printpacket("received ack", ack) if DEBUG_MODE: print("DEBUG: ack_source_addr", ack_source_addr) if ack_final: self.flag = 1 if DEBUG_MODE: print("DEBUG:", self.flag) if DEBUG_MODE: print("DEBUG: last packet") time.sleep(.5) self.set_mode(MODE.SLEEP) print(self.frx) # If valid, here we go! elif (ack_is_ack) and (ack_acknum == self.acknum) and ( self.rcv2 == ack_source_addr): self.frx = 1 time.sleep(1) self.set_mode(MODE.SLEEP) self.set_dio_mapping([1, 0, 0, 0, 0, 0]) self.reset_ptr_rx() self.set_mode( MODE.TX) #Changing to TX mode to send a new packet self.flagsend = 1 else: if DEBUG_MODE: print("ERROR: packet not for me") self.set_mode(MODE.SLEEP) self.reset_ptr_rx() BOARD.led_off() self.set_mode(MODE.RXCONT) except TimedOutExc as e: #signal.alarm(0) time.sleep(.5) self.frx = 2 print(self.frx) self.frec = 1 self.set_mode(MODE.SLEEP) self.set_dio_mapping([1, 0, 0, 0, 0, 0]) self.reset_ptr_rx() self.set_mode(MODE.TX)
""" import sys import time from hoperf.LoRa import * from hoperf.LoRaArgumentParser import LoRaArgumentParser from hoperf.board_config import BOARD import hashlib import binascii import signal import struct import socket DEBUG_MODE = True socket.setdefaulttimeout(10) BOARD.setup() # # BEGIN: Utility functions # parser = LoRaArgumentParser("Stop and Wait LoRa Protocol for Raspberry") parser.add_argument('--single', '-S', dest='single', default=False, action="store_true", help="Single transmission") parser.add_argument('--wait', '-w', dest='wait',