I have two transmitters (NRF24L01+UNO) both sending data on different pipe addresses to a reciever (NRF24L01+MEGA) but my data arrays are picking up mixed data.
Not sure if my programming method is correct, reciever code is below.
Any help or guidance would be appreciated.
#include <SPI.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 53
#define DHT11_PIN 11
#include <dht.h>
dht DHT;
const byte level_module_address = {0xF0F0F0F0E2LL};
const byte moisture_module_address = {0xF0F0F0F0E3LL};
RF24 radio(CE_PIN, CSN_PIN);
int level_data[5] = {0,0,0,0,0}; // Data from Link 1
int moisture_data[2] = {0,0}; // Data from Link 2
int ackData[2] = {0,0};
bool newData = false;
void setup()
{
Serial.begin(250000);
Serial.println("RF Comms Starting...");
radio.begin();
radio.setDataRate( RF24_250KBPS );
}
void loop(void)
{
getData_level(); // Read RF Data from Link 1
getData_moisture(); // Read RF Data from link 2
showData();
}
void getData_level() {
radio.openReadingPipe(0, level_module_address);
radio.enableAckPayload();
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data
radio.startListening();
if (radio.available() ) {
radio.read( &level_data, sizeof(level_data) );
newData = true;
updateReplyData();
}
}
void getData_moisture() {
radio.openReadingPipe(1, moisture_module_address);
radio.enableAckPayload();
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data
radio.startListening();
if (radio.available() ) {
radio.read( &moisture_data, sizeof(moisture_data) );
newData = true;
updateReplyData();
}
}
void showData() {
if (newData == true) {
//
// Print actual data on serial monitor
//
Serial.println();
Serial.println();
Serial.print("Data Received from Module 1 (Level Sensor)");
Serial.println();
Serial.print("Temp = ");
Serial.println(level_data[0]);
Serial.print("Humidity = ");
Serial.println(level_data[1]);
Serial.print("Level = ");
Serial.println(level_data[2]);
Serial.print("Comms = ");
Serial.println(level_data[3]);
Serial.println();
Serial.print("Data Received from Module 2 (Moisture Sensor)");
Serial.println();
Serial.print("Comms = ");
Serial.println(moisture_data[0]);
Serial.print("Moisture = ");
Serial.println(moisture_data[1]);
Serial.print("Acknowledge Payload Sent = ");
Serial.print(ackData[0]);
Serial.println();
newData = false;
}
}
void updateReplyData() {
radio.writeAckPayload(0, &ackData, sizeof(ackData)); // load the payload for the next time
}
IMHO it makes life much easier to use the "receiving" Arduino as a "master" and have it send messages and get replies from the other two ("slaves") one at a time.
The second example in this Simple nRF24L01+ Tutorial can be extended to work with several slaves. And it does not need the complexity of multiple pipes.
Note that even when using multiple pipes it is not possible for two messages to be sent at the same time - that will just garble both messages.
Robin2:
even when using multiple pipes it is not possible for two messages to be sent at the same time - that will just garble both messages.
I have no control over this, but have tried increasing the delay so one tx only sends every 10 seconds and the other 5 seconds but data still comes through being asigned to wrong variables..?
If I run the transmitters individually the data goes to correct variable with the zero's assigned to the variable of the transmitter which is off, but both together they are inter-mixed. How can that happen when they are on dedicated different pipes?
dudester:
I thought i was, thats why i dont understand what I am doing wrong..?
Nope.
You are opening one pipe, attach an ack-payload and startListening.
Then you look for a packet if one is there, process it.
The you open the other pipe, attach an ack-payload and startListening.
Then you look for a packet if one is there, process it.
You repeat that sequence over and over...
Calling startListening over and over without any stopListening is a strange usage.
Open both pipes (with ack-payload preload) call startListening (once) in setup,
and only process the received packets in loop.
Use bool available (uint8_t *pipe_num)to get the pipe on which the packet was received.
Please bare with me, i am a begiiner so really have no idea how to code this.
I have used the code you suggested but that still displays zeroes as the pipe.
How should pipe_num be declared?
The same constant ack-payload for both pipes is rather senseless,
the sending station can detect the successful transmission without receiving a constant back (wasting bandwidth).
Having two different newData flags for two the different messages would be more logical.