nrf24l01 not working in mixed environment

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Majodi
Posts: 3
Joined: Tue May 15, 2018 7:57 am

nrf24l01 not working in mixed environment

Post by Majodi » Tue May 15, 2018 8:38 am

I have a simple sensorbox that communicates using nrf24l01-PA-LNA modules. Using Arduino sketches for sending/receiving works but I would like to switch to MicroPython. So I got an ESP board that can run MicroPython to test the connectivity with the sensorbox using the nrf24l01 driver. I can not get it to work.

For the sensorbox I use a simple Arduino Compatible Pro Mini and for the receiving side I've now got the MicroPython capable ESP12F board. The sender (Pro Mini) runs the Arduino code. When I use Arduino code on the receiver side (ESP12) it works. However, using MicroPython at the receiver side doesn't.

I really would like to use MicroPython. I hoped to get started with it on the receiver side only so I can leave my sensor code in place. But is that the problem? Can I not mix Arduino on sender and MicroPython on receiver? Why? Or am I missing something here?

By the way, the SPI setup is working. And I use the exact same pins for both Arduino/MicroPython.

See code below.

Thnx,

Majodi

This is the simplified test code:

===> Arduino code on sender:

#include "nRF24L01.h"
#include "RF24.h"

// Pro-Mini

#define CE 7
#define CSN 8

RF24 radio(CE, CSN);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

void setup() {
Serial.begin(115200);
radio.begin();
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
radio.setDataRate( RF24_250KBPS );
radio.setChannel(100);
radio.powerUp() ;
}

void loop() {
Serial.println("AA");
radio.write("AA", 2);
delay(3000);
}

===> Working Arduino code on receiver:

#include "nRF24L01.h"
#include "RF24.h"

// Nano or ESP12F DEVKIT V3

#define CE 2 //nano 7 - esp12f 4 of 2 (dit zijn GPIO nummers!)
#define CSN 15 //nano 8 - esp12f 15 (dit zijn GPIO nummers!)

RF24 radio(CE, CSN);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

void setup() {
Serial.begin(115200);
radio.begin();
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
radio.setDataRate( RF24_250KBPS );
radio.setChannel(100);
radio.powerUp() ;
radio.startListening();
Serial.println("RX ready...");
}

void loop() {
while (radio.available()) {
Serial.println("available!!");
char inStr[15] = {0};
radio.read(&inStr, 2);
Serial.println(inStr);
}
}

===> Now the NOT Working MicroPython code on receiver:

import utime
from machine import Pin, SPI
from nrf24l01 import NRF24L01

pipes = (b'\xf0\xf0\xf0\xf0\xe1', b'\xf0\xf0\xf0\xf0\xd2')

def readSensorLoop():
spi = SPI(1)
csn = Pin(15, mode=Pin.OUT, value=1) # 4
ce = Pin(2, mode=Pin.OUT, value=0) # 5
nrf = NRF24L01(spi, csn, ce, channel=100)

nrf.open_tx_pipe(pipes[1])
nrf.open_rx_pipe(1, pipes[0])
nrf.start_listening()

print('readSensorLoop, waiting for packets... (ctrl-C to stop)')

while True:

utime.sleep(1)
if nrf.any():
print('received something!!')

readSensorLoop()

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: nrf24l01 not working in mixed environment

Post by pythoncoder » Tue May 15, 2018 5:58 pm

The Python code appears not to set the data rate.
Peter Hinch
Index to my micropython libraries.

Majodi
Posts: 3
Joined: Tue May 15, 2018 7:57 am

Re: nrf24l01 not working in mixed environment

Post by Majodi » Tue May 15, 2018 6:06 pm

That's done in the constructor of the nrf24l01 module (the one I've got from the MicroPython repository):

# set rf power and speed
self.set_power_speed(POWER_3, SPEED_250K) # Best for point to point links

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: nrf24l01 not working in mixed environment

Post by pythoncoder » Wed May 16, 2018 6:03 am

You're right. I'm a bit stumped by this one. If it were me I think I'd set up another MicroPython/NRF24L01+ kit and ensure that they can talk to each other. Then you know your code is OK and the fault must be down to mis-configuration between the Pyboard and Arduino radios. These things are never easy to fix as there is no easy way to monitor what's happening on the link.

For what it's worth the Pyboard NRF24L01+ driver works well. I have an application which has been running continuously for a couple of years. Three Pyboards wake up every hour from deep sleep and communicate with a fourth via these radios.

Good luck ;)
Peter Hinch
Index to my micropython libraries.

Majodi
Posts: 3
Joined: Tue May 15, 2018 7:57 am

Re: nrf24l01 not working in mixed environment

Post by Majodi » Wed May 16, 2018 9:40 am

Yeah, I thought about other possibilities too as I maybe stretching it a bit with this mixed setup.

Thnx

rahai9
Posts: 1
Joined: Mon Jul 27, 2020 12:44 am

Re: nrf24l01 not working in mixed environment

Post by rahai9 » Mon Jul 27, 2020 1:00 am

It has been two years since last post but this might just help someone.

The Arduino library uses the default payload size of 32 bytes but the defaults for MicroPython is only 16 bytes. I found this works if I just add a payload_size parameter and set it to 32 bytes in MicroPython code. ( or you could do the other way around and change it in Arduino code :mrgreen: ).

Majodi wrote:
Tue May 15, 2018 8:38 am
nrf = NRF24L01(spi, csn, ce, channel=100)
So basically just change the above line of code to:

nrf = NRF24L01(spi, csn, ce, channel=100, payload_size=32)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Not the only bug

Post by pythoncoder » Mon Jul 27, 2020 4:58 am

Well spotted.

There is another issue re working with Arduino in that MicroPython pipes addresses are little-endian while Arduino uses big-endian format. This is now explained in comments in the official test program nrf24l01test.py. The MicroPython code in this thread is incorrect and should read:

Code: Select all

pipes = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd2\xf0\xf0\xf0\xf0")
Peter Hinch
Index to my micropython libraries.

Post Reply