Hi. I'm going to make a network with some of this device. I use the RF24 library and I have some problem with the address and the openWritingPipe and openReadingPipe. Anyone can explain to me how address work? How can I send a message specifically to another module and not to all?
Also, for improving the range I read online that RF24_PA_MAX in setPALevel is the best option, but how can I power the module on to offer all the current that they need in this level? For example, if I use a mcp1702 it's enough to power the module(in RF24_PA_MAX) and an Arduino nano?
Many thanks!
This Simple nRF24L01+ Tutorial may help to clarify things.
The address is similar in concept to a phone number. Every nRF24 will receive a message if they all listen on the same channel. But those which are not programmed to listen for a particular address will just ignore the message. Another analogy is hearing someone's name shouted in a crowd. If it's not your name you ignore it.
...R
Thanks for the link to the tutorial! But I didn't understand something. First: In your example sketch the tx module receive an automatic response from the rx by default without doing nothing in the code? And how can I access and use that response?. Second, in the power section, you don't say if the Arduino UNO is enough to power up the module in RF24_PA_MAX.
Turning back to the address problem, I'll try to explain what I realised.
First, I must create an address array with all the address. Then, if the module is a TX one, I open a openWritingPipe with the address of the TX module, not the address of the RX module who I want to send the message. If the module is an RX one, I open a openReadingPipe for each TX module that I want to listen, filling the parameters with the number of the Pipe and the address of the TX module from which I want to receive the message.
Is it correct?
Eternyt:
First: In your example sketch the tx module receive an automatic response from the rx by default without doing nothing in the code?
Which of my examples are you talking about?
And how can I access and use that response?.
The example that uses the ackPayload feature shows how to get the data that is sent as part of the acknowledgement,
Second, in the power section, you don't say if the Arduino UNO is enough to power up the module in
RF24_PA_MAX.
AFAIK the default is max power and my nRF24s with the PCB antenna work fine when powered from the Uno's 3,3v pin. If you have a problem try putting a 10µF capacitor between 3.3v and GND. I suspect the 3,3v pin would not be able to power a high power nRF24 with the external antenna.
Then, if the module is a TX one, I open a
openWritingPipewith the address of the TX module, not the address of the RX module who I want to send the message.
When you call your friend on the phone do you type in your own phone number or your friend's number?
If the module is an RX one, I open a
openReadingPipefor each TX module that I want to listen, filling the parameters with the number of the Pipe and the address of the TX module from which I want to receive the message.
Think of the receiving nRF24 as your friend's phone. You allocate an address to the receiver and the transmitter must use that address.
...R
Which of my examples are you talking about?
I'm talking about the SimpleTx.ino. In the code there is a rslt variable but I don't understand what it is (It is an automatic response that the RX module automatically sends back to the TX?)
When you call your friend on the phone do you type in your own phone number or your friend's number?
If it's correct what you says, if I need to send a message from a one TX module to multiple RX, i must call openWritingPipe many times as the RX module which I want to send the message (one number for each friend)?
The variable rslt will be true or false depending on whether the transmission was successful or not. It will be considered successful if it was received without any error and if the acknowledgement from the Rx was successfully received by the Tx.
Your last paragraph in Reply #4 is correct. In that case you would call openWritingPipe() from loop() or from your send() function rather than from setup()
...R
So...
If I want to check if the message is correctly arrived I must do something like this:
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
if (rslt == true) {
Serial.println("Message received correctly");
}
And, if I want to send a message from a TX (1Node) module to multiple RX (2Node, 3Node, 4Node) module I must do this (I skip many part of the code, just focalize on what I trying to say):
// TX Module
byte addresses[][6] = {"1Node","2Node","3Node","4Node","5Node"};
radio.openWritingPipe(addresses[1]);
radio.openWritingPipe(addresses[2]);
radio.openWritingPipe(addresses[3]);
radio.openWritingPipe(addresses[4]);
//RX Module
byte addresses[][6] = {"1Node","2Node","3Node","4Node","5Node"};
radio.openReadingPipe(1,addresses[1]);
//RX Module
byte addresses[][6] = {"1Node","2Node","3Node","4Node","5Node"};
radio.openReadingPipe(1,addresses[2]);
//RX Module
byte addresses[][6] = {"1Node","2Node","3Node","4Node","5Node"};
radio.openReadingPipe(1,addresses[3]);
//RX Module
byte addresses[][6] = {"1Node","2Node","3Node","4Node","5Node"};
radio.openReadingPipe(1,addresses[4]);
//RX Module
byte addresses[][6] = {"1Node","2Node","3Node","4Node","5Node"};
radio.openReadingPipe(1,addresses[5]);
So, the "1Node" is userless, because is the address of the trasmitter, and it will never be use.
I suspect you really know enough about programming to know that this won't work
radio.openWritingPipe(addresses[1]);
radio.openWritingPipe(addresses[2]);
radio.openWritingPipe(addresses[3]);
radio.openWritingPipe(addresses[4]);
In that code only the final line will have any effect.
You need to open the writing pipe, send the message and then open the next pipe and send the next message
Please post a complete program as it is impossible to get the overall picture from snippets.
...R
Sorry, here is the entire code:
TX
#include <SPI.h>
#include "RF24.h"
RF24 radio(7,8);
int dataTransmitted = 1;
bool rslt;
// There is only the RX Module address
byte addresses[][6] = {"1Node", "2Node"};
void setup() {
Serial.begin(115200);
Serial.println("Boot OK");
Serial.println("MODE: Trasmitter");
radio.begin();
// Settings
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(108);
radio.setDataRate(RF24_250KBPS);
pinMode(5, INPUT);
pinMode(13, OUTPUT);
}
void loop() {
if(digitalRead(5) == 1) {
radio.openWritingPipe(addresses[0]);
rslt = radio.write( &dataTransmitted, sizeof(dataTransmitted) ); // Transmit the data, and checking the acknowledge
Serial.print("Data Transmitted = ");
Serial.println(dataTransmitted);
Serial.println("Acknowledge expected...");
// Check the response of the RX module
if(rslt == true){
Serial.println("Acknowledge received");
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(13, LOW);
} else {
Serial.println("Acknowledge NOT received");
}
radio.openWritingPipe(addresses[1]);
rslt = radio.write( &dataTransmitted, sizeof(dataTransmitted) ); // Transmit the data, and checking the acknowledge
Serial.print("Data Transmitted = ");
Serial.println(dataTransmitted);
Serial.println("Acknowledge expected...");
// Check the response of the RX module
if(rslt == true){
Serial.println("Acknowledge received");
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(13, LOW);
} else {
Serial.println("Acknowledge NOT received");
}
dataTransmitted++;
Serial.println("Update variable");
delay(1000);
}
}
RX1
#include <SPI.h>
#include "RF24.h"
RF24 radio(7,8);
unsigned long payload;
// RX address (address of this module)
byte addresses[][6] = {"1Node"};
void setup() {
Serial.begin(115200);
Serial.println("Boot OK");
Serial.println("MODE: Receiver");
radio.begin();
// Settings
radio.setPALevel(RF24_PA_LOW);// RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH RF24_PA_MAX
radio.setChannel(108);
radio.setDataRate(RF24_250KBPS); // Probably not working
// Reading pipe with the address of this module
radio.openReadingPipe(1,addresses[0]);
radio.startListening();
}
void loop() {
if(radio.available()) {
while (radio.available()) { // While there is data ready
radio.read( &payload, sizeof(unsigned long) ); // Get the payload
}
Serial.print(payload);
}
}
RX2
#include <SPI.h>
#include "RF24.h"
RF24 radio(7,8);
unsigned long payload;
// RX address (address of this module)
byte addresses[][6] = {"2Node"};
void setup() {
Serial.begin(115200);
Serial.println("Boot OK");
Serial.println("MODE: Receiver");
radio.begin();
// Settings
radio.setPALevel(RF24_PA_LOW);// RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH RF24_PA_MAX
radio.setChannel(108);
radio.setDataRate(RF24_250KBPS); // Probably not working
// Reading pipe with the address of this module
radio.openReadingPipe(1,addresses[0]);
radio.startListening();
}
void loop() {
if(radio.available()) {
while (radio.available()) { // While there is data ready
radio.read( &payload, sizeof(unsigned long) ); // Get the payload
}
Serial.print(payload);
}
}
What version of the RF24 library are you using? The older ManiacBug version can have problems with long delay()s between messages. Use the newer TMRh20 version of the RF24 library
Does the program work if you only try to send to one slave?
You really should use a FOR loop to iterate through the two TX parts. Duplicating code just makes more space for errors.
...R
I suppose I already use this library.
Here is the propriety file of the library
name=RF24
version=1.2.0
author=TMRh20
maintainer=TMRh20
sentence=A library for NRF24L01(+) communication.
paragraph=Optimized library for nRF24L01(+) that is simple to use for beginners, but yet offers a lot for advanced users. It also has a lot of good examples how to use the library.
category=Communication
url=http://tmrh20.github.io/RF24/
architectures=*
To only one slave works pretty well, with acknowledging and a pretty good range.
You really should use a FOR loop to iterate through the two TX parts. Duplicating code just makes more space for errors.
Thanks for the advice
Eternyt:
To only one slave works pretty well, with acknowledging and a pretty good range.
Is that true regardless of which slave you use?
If so then I can't see why it does not work with the two slaves.
...R
No, my question is more generic, the address system of this library works on this way?
Eternyt:
No, my question is more generic, the address system of this library works on this way?
Does the NO mean that your code only works with one slave and not with the other?
I thought I had explained how the addressing system works and I am reluctant to say yes to "this way" without knowing exactly what you have in mind. I suspect the simplest way to clarify that is with a working program - which is why I asked the question in Reply #11.
...R