and follow the prompts.
    1"""
    2A simple example of sending data from 1 nRF24L01 transceiver to another
    3with manually transmitted (non-automatic) Acknowledgement (ACK) payloads.
    4This example still uses ACK packets, but they have no payloads. Instead the
    5acknowledging response is sent with `write()`. This tactic allows for more
    6updated acknowledgement payload data, where actual ACK payloads' data are
    7outdated by 1 transmission because they have to loaded before receiving a
    8transmission.
    9 
   10This example was written to be used on 2 devices acting as 'nodes'.
   11 
   12See documentation at https://nRF24.github.io/RF24
   13"""
   14 
   15import time
   16from RF24 import RF24, RF24_PA_LOW, RF24_DRIVER
   17 
   18 
   19print(__file__)  
   20 
   21
   27CSN_PIN = 0  
   28if RF24_DRIVER == "MRAA":
   29    CE_PIN = 15  
   30elif RF24_DRIVER == "wiringPi":
   31    CE_PIN = 3  
   32else:
   33    CE_PIN = 22
   34radio = 
RF24(CE_PIN, CSN_PIN)
 
   35 
   36
   37if not radio.begin():
   38    raise RuntimeError("radio hardware is not responding")
   39 
   40
   41
   42address = [b"1Node", b"2Node"]
   43
   44
   45 
   46
   47
   48
   49radio_number = bool(
   50    int(input("Which radio is this? Enter '0' or '1'. Defaults to '0' ") or 0)
   51)
   52 
   53
   54
   55radio.setPALevel(RF24_PA_LOW)  
   56 
   57
   58radio.stopListening(address[radio_number])
   59 
   60
   61radio.openReadingPipe(1, address[not radio_number])  
   62 
   63
   64
   65
   66radio.payloadSize = 8
   67 
   68
   69
   70
   71
   72
   73 
   74
   75
   76counter = [0]
   77 
   78 
   79def master():
   80    """Transmits a message and an incrementing integer every second, then
   81    wait for a response for up to 200 ms.
   82    """
   83    radio.stopListening()  
   84    failures = 0
   85    while failures < 6:
   86        
   87        
   88        buffer = b"Hello \x00" + bytes(counter)
   89        start_timer = time.monotonic_ns()  
   90        result = radio.write(buffer)
   91        if not result:
   92            failures += 1
   93            print("Transmission failed or timed out")
   94        else:
   95            radio.startListening()  
   96            timeout = time.monotonic() * 1000 + 200  
   97            
   98            while not radio.available() and time.monotonic() * 1000 < timeout:
   99                pass  
  100            radio.stopListening()  
  101            end_timer = time.monotonic_ns()  
  102            decoded = buffer[:6].decode("utf-8")
  103            print(
  104                f"Transmission successful. Sent: {decoded}{counter[0]}.",
  105                end=" ",
  106            )
  107            has_payload, pipe_number = radio.available_pipe()
  108            if has_payload:
  109                
  110                received = radio.read(radio.payloadSize)
  111                
  112                counter[0] = received[7:8][0]  
  113                decoded = bytes(received[:6]).decode("utf-8")
  114                print(
  115                    f"Received {radio.payloadSize} bytes",
  116                    f"on pipe {pipe_number}: {decoded}{counter[0]}.",
  117                    f"Round-trip delay: {(end_timer - start_timer) / 1000} us.",
  118                )
  119            else:
  120                print("No response received.")
  121        time.sleep(1)  
  122    print(failures, "failures detected. Leaving TX role.")
  123 
  124 
  125def slave(timeout: int = 6):
  126    """Listen for any payloads and print the transaction
  127 
  128    :param int timeout: The number of seconds to wait (with no transmission)
  129        until exiting function.
  130    """
  131    radio.startListening()  
  132 
  133    start_timer = time.monotonic()  
  134    while (time.monotonic() - start_timer) < timeout:
  135        
  136        has_payload, pipe_number = radio.available_pipe()
  137        if has_payload:
  138            received = radio.read(radio.payloadSize)  
  139            
  140            
  141            counter[0] = received[7:8][0] + 1 if received[7:8][0] < 255 else 0
  142            
  143            
  144            buffer = b"World \x00" + bytes(counter)
  145            radio.stopListening()  
  146            radio.writeFast(buffer)  
  147            
  148            result = radio.txStandBy(150)  
  149            
  150            radio.startListening()  
  151            
  152            decoded = bytes(received[:6]).decode("utf-8")
  153            print(
  154                f"Received {radio.payloadSize} bytes"
  155                f"on pipe {pipe_number}: {decoded}{received[7:8][0]}.",
  156                end=" ",
  157            )
  158            if result:  
  159                
  160                decoded = buffer[:6].decode("utf-8")
  161                print(f"Sent: {decoded}{counter[0]}")
  162            else:
  163                print("Response failed or timed out")
  164            start_timer = time.monotonic()  
  165 
  166    print("Nothing received in", timeout, "seconds. Leaving RX role")
  167    
  168    radio.stopListening()  
  169 
  170 
  171def set_role() -> bool:
  172    """Set the role using stdin stream. Timeout arg for slave() can be
  173    specified using a space delimiter (e.g. 'R 10' calls `slave(10)`)
  174 
  175    :return:
  176        - True when role is complete & app should continue running.
  177        - False when app should exit
  178    """
  179    user_input = (
  180        input(
  181            "*** Enter 'R' for receiver role.\n"
  182            "*** Enter 'T' for transmitter role.\n"
  183            "*** Enter 'Q' to quit example.\n"
  184        )
  185        or "?"
  186    )
  187    user_input = user_input.split()
  188    if user_input[0].upper().startswith("R"):
  189        if len(user_input) > 1:
  190            slave(int(user_input[1]))
  191        else:
  192            slave()
  193        return True
  194    if user_input[0].upper().startswith("T"):
  195        master()
  196        return True
  197    if user_input[0].upper().startswith("Q"):
  198        radio.powerDown()
  199        return False
  200    print(user_input[0], "is an unrecognized input. Please try again.")
  201    return set_role()
  202 
  203 
  204if __name__ == "__main__":
  205    try:
  206        while set_role():
  207            pass  
  208    except KeyboardInterrupt:
  209        print(" Keyboard Interrupt detected. Powering down radio.")
  210        radio.powerDown()
  211else:
  212    print("    Run slave() on receiver\n    Run master() on transmitter")
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.