warwolf7
Posts: 6
Joined: Wed Dec 23, 2015 8:41 pm

Re: NRF24L01+ in Python

Wed Aug 31, 2016 5:51 am

blavery wrote:
diag.png
send.png
luisantunes,

The RECV end (PRX mode) awaits packages, displays the "HELO" (in hex) it received,
... and every alternate time prepares a payload to be bundled with the next AUTO-ACK process.
So the reply is seen by the PTX a transaction delayed - it arrives with the ACK sequence of the next transfer. The PRX cannot initiate a transfer, it can only set up a return package to catch a free ride on the next ACK sequence.
Both forward packet payload and return payload are variable size to 32 bytes.


Brian
The HELO received is in a ascii decimal format (http://www.nthelp.com/ascii.htm)

Code: Select all

print (''.join(chr(i) for i in recv_buffer))
this write "HELO" and a weird caracter
I figured it was just easier to send string instead of a list. So I changed the code a bit. Just input the command you want to send, and if it reaches more than 32bits, the program will creates chunks of 32bits.

Code: Select all

#you still need the top portion of the code in send-rpi.py

#credits http://stackoverflow.com/questions/18854620/whats-the-best-way-to-split-a-string-into-fixed-length-chunks-and-work-with-the?answertab=active#tab-top
def chunkstring(string,length):  
    return (string[0+i:length+i] for i in range(0,len(string),length))

while True:
    buf = input("enter the command to send")

    buf = list(chunkstring(buf,32))
    print("nombre de packet de 32bits : " + str(len(buf)))
    radio.write("Chunk:" + str(len(buf)))

       
    c = (c + 1) & 255
    # send a packet to receiver
    for i in buf:
        radio.write(i)
        print ("Sent:"),
        print (i)
    # did it return with a payload?
        if radio.isAckPayloadAvailable():
            pl_buffer=[]
            radio.read(pl_buffer, radio.getDynamicPayloadSize())
            print ("Received back:"),
            print (pl_buffer)
        else:
            print ("Received: Ack only, no payload")
the code in my recv.py is not clean enough to be shown here, but from here it's quite easy to make a simple loop when chunk is bigger than 1 and merge each chunk together until you've reach the total amount of chunk.

I don't know if this help anyone.

Thanks for OP for all the info

-Matt

krkarkav
Posts: 1
Joined: Mon Apr 17, 2017 4:24 pm

Re: NRF24L01+ in Python

Mon Apr 17, 2017 4:30 pm

Hey, I've trying to establish pi to pi communication using nrf24, the code is pretty much the same. as you 've given above.Except when i tried too edit radio.begin(0,17) to radio.begin(0,0,17) , i got a traceback.
I tried interchanging the csn and ce pins to gpio 17 and CEO ..but one RPI shows crc length disabled no matter what.The other shows crc :16 bits
what could be the problem
Please help.

raspitryout
Posts: 1
Joined: Fri Oct 27, 2017 8:30 pm

Re: NRF24L01+ in Python

Fri Oct 27, 2017 8:54 pm

Hi everyone,

I'm trying to get a ATTINY84A to communicate with my Raspberry Pi. On the Attiny I'm using the RF24.h library by TMRh20 and got this working. So, when I'm trying the Gettingstarted sketch (in C/C++) on both sides, I can receive something. But a soon as I switch to python I don't get any signal anymore. If I try the Gettingstarted sketch on a Arduino Nano, I can receive everything.

Since I'm not a programmer, I don't want to mess with C and stick with python.

What I can see right now, is a difference in the config between C and Python:

Code: Select all

Python Config:
STATUS   = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1     = 0xe7e7e7e7e7 0xc2c2c2c2c2
RX_ADDR_P2-5     = 0xc3 0xc4 0xc5 0xc6
TX_ADDR          = 0xe7e7e7e7e7
RX_PW_P0-6       = 0x05 0x05 0x00 0x00 0x00 0x00
EN_AA            = 0x3f
EN_RXADDR        = 0x02
RF_CH            = 0x4c
RF_SETUP         = 0x01
CONFIG           = 0x0f
DYNPD/FEATURE    = 0x3f 0x06
Data Rate        = 1MBPS
Model            = nRF24l01+
CRC Length       = 16 bits
PA Power         = PA_MIN

Code: Select all

C Config
STATUS           = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1     = 0xe7e7e7e7e7 0xc2c2c2c2c2
RX_ADDR_P2-5     = 0xc3 0xc4 0xc5 0xc6
TX_ADDR          = 0xe7e7e7e7e7
RX_PW_P0-6       = 0x20 0x20 0x00 0x00 0x00 0x00
EN_AA            = 0x3f
EN_RXADDR        = 0x02
RF_CH            = 0x4c
RF_SETUP         = 0x01
CONFIG           = 0x0e
DYNPD/FEATURE    = 0x00 0x00
Data Rate        = 1MBPS
Model            = nRF24L01+
CRC Length       = 16 bits
PA Power         = PA_MIN
Since I'm not having any idea anymore, I hope you can help me. This whole thingy is driving me crazy...

rajeev2301
Posts: 22
Joined: Mon Jan 15, 2018 8:14 am

Re: NRF24L01+ in Python

Mon Jan 15, 2018 5:51 pm

I have more or less similar situation. Not sure what is wrong with my code I am not able to receive messages from Arduino Nano. Also, scratching my head on why RX_ADDR_P0-1 is NULL(0x0000000000 0x0000000000) when my array ( address) is initialized with [[0xF0,0xF0,0xE8] , [0xF,0xF0,0xE1]]. Any help is greatly appreciated..

aSTATUS = 0x00 RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=0 TX_FULL=0
RX_ADDR_P0-1 # =
0x0000000000 0x0000000000
RX_ADDR_P2-5 # =
0x00
0x00
0x00
0x00

TX_ADDR =
0x0000000000
RX_PW_P0-6 # =
0x00
0x00
0x00
0x00
0x00
0x00

EN_AA =
0x00

EN_RXADDR # =
0x00

RF_CH =
0x00

RF_SETUP # =
0x00

CONFIG =
0x00

DYNPD/FEATURE # =
0x00
0x00

Data Rate = 1MBPS
Model = nRF24L01
CRC Length = Disabled
PA Power = PA_MIN
eceived: []
Translating our received Message into Unicode character...
Received: []
Translating our received Message into Unicode character...
Received: []
Translating our received Message into Unicode character...
Received: []
Translating our received Message into Unicode character...
Received: []
Translating our received Message into Unicode character...
Received: []

import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev

GPIO.setmode(GPIO.BCM)

address = [[0xF0,0xF0,0xE8] , [0xF,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.openReadingPipe(1 ,address[1])
radio.printDetails()
radio.startListening()

while True:
while not radio.available(0):
time.sleep(1/100)
receivedMessage = []
radio.read(receivedMessage, radio.getDynamicPayloadSize())
print("Received: {}".format(receivedMessage))

print("Translating our received Message into Unicode character...")
string = " "

for n in receivedMessage:
if ( n >=32 and n <= 126):
string += chr(n)
print (" Our received message decodes to : {} ".format(string))

KyluAce
Posts: 40
Joined: Mon Sep 04, 2017 10:01 am

Re: NRF24L01+ in Python

Mon Apr 16, 2018 7:52 am

Hello,

I'm working with NRF24L01 + python and I'm confused about reading data. I have simple function to read data for 20 seconds. There is no problem if I get some data but if there is no radio signals my program stuck on "radio.read(revc_buffer)" till it gets data. There is an option to fight this? set up some kind of timeout ?

Code: Select all

def recv():
    global dictt
    radio.openWritingPipe(pipes[0])
    radio.openReadingPipe(1, pipes[1])
    radio.startListening()
    radio.stopListening()
    radio.printDetails()
    radio.startListening()
    t_end = time.time() + 20
    while time.time() < t_end:
        pipe = [0]
        while not radio.available(pipe):
            time.sleep(1000/1000000.0)
        recv_buffer = []
        radio.read(recv_buffer)
        if recv_buffer[0] == 144:
            list_temp.append(recv_buffer)
    print(list_temp)
    return

swaraj8542
Posts: 1
Joined: Sat Sep 07, 2019 7:50 am

Re: NRF24L01+ in Python

Sat Sep 07, 2019 7:57 am

I have the done exactly same as stated in the first comment.
I am using the printDetails() funtion and when I am running the code every value in it is showing "0xff".
Please help me for this.

Here is the output.

Return to “HATs and other add-ons”