pythonraspberry-pispi

Nrf24 gets only one payload


I created a small script for the NRF24 radio module with the package from pyNRF.

I wired it up like in the example on the GitHub page.

When I start the simple_recever and simple_sender script everything works. I receve the messages and get the correct output.

But with my script its only the first message which is receved. Then the recever gets stuck. The nrf.data_ready function is always true and won't change the receved payload. And the first Byte won't be correct in the receved message. But the

My script transmitter side.

while True:
    if not self.sending_data.empty():
        for _ in range(self.sending_data.qsize()-1):
            send=self.sending_data.get_nowait()
    
        if not send:
            continue
        if send[0][9] and send[0][10]:
            pass
        else:     
            payload = struct.pack("<B"+"?"*13+"f"*6+"h"*2,
                                    0x01,
                                    *send[0].values(),
                                    *send[1].values(),
                                    send[2][0][0],
                                    send[2][0][1])
            nrf.reset_packages_lost()
            nrf.send(payload)
        print("<B"+"?"*13+"f"*6+"h"*2)
        print(not send)
        print(len(send[0].values()))
        print(len(send[1].values()))
    print(payload)        
    # Send the payload to the address specified above.
    
    try:
        if payload:
            nrf.wait_until_sent()
            print("sented")
    except TimeoutError:
        print("Timed out")
        time.sleep(0.2)
        continue
    
    if nrf.get_packages_lost() == 0:
        print(f"Success: lost={nrf.get_packages_lost()}, retries={nrf.get_retries()}")
    else:
        print(f"Error: lost={nrf.get_packages_lost()}, retries={nrf.get_retries()}")
    
    time.sleep(1)

My code on recever side:

while True:
    while nrf.data_ready():
        # Count message and record time of reception.            
        count += 1
        now = datetime.now()
         
        # Read pipe and payload for message. 
        pipe = nrf.data_pipe()              
        payload = nrf.get_payload()                                             
        print(payload)
        # If the length of the message is 9 bytes and the first byte is 0x01, then we try to interpret the bytes
        # sent as an example message holding a temperature and humidity sent from the "simple-sender.py" program.
        protocol = payload[0] if len(payload) > 0 else -1            
        hex = ':'.join(f'{i:02x}' for i in payload)
        # Show message received as hex.
        print(f"{now:%Y-%m-%d %H:%M:%S.%f}: pipe: {pipe}, len: {len(payload)}, bytes: {hex}, count: {count}, protocol: {protocol}")
        nrf.show_registers()
        
        if payload[0] == 0x01:
            
            for i in payload:
                print(i)
            print("Payload rx: " + str(struct.unpack("<B"+"?"*13+"f"*6+"h"*2, payload)))
            values = struct.unpack("<B"+"?"*13+"f"*6+"h"*2, payload)
            self.queue.put_nowait(values)
    time.sleep(0.1)

The settings are the same as in the example programms except the data rate is 2MBs.

The only part which changed is the payload from:

payload = struct.pack("<Bff", 0x01, temperature, humidity)

to

payload = struct.pack("<B"+"?"*13+"f"*6+"h"*2,
                                        0x01,
                                        *send[0].values(),
                                        *send[1].values(),
                                        send[2][0][0],
                                        send[2][0][1])

Why does this not work and how could it be fixed?


Solution

  • So after some reading from the datasheet. I found out that the max Buffer is 32Bytes...

    So in this case i have to write a Protocol that transmitts the correct data in multiple junks...