Hello everybody,
I beg your help for my for my project !
I would like to make arduinos communicate wirelessly with nrf24l01+
The idea is to have one master which receives the sensors datas from two points.
Each emitting point would send values from 100 sensors
I searched for hours but nothing work !
Please can someone explain what's wrong ?
Thank you in advance,
PS : sorry if my english is... so so !
Master code:
//*************Branchement**************************************************************
// RF Name Uno Mega
// |------------------------------|---|---| Nano
// | |___ ________ | 8 | 7 | 8 IRA N/A N/A
// | ___| (________) |---|---| 7 MISO 12 50
// | |___ | 6 | 5 | 6 MOSI 11 51
// | ___| | |---|---| 5 CSK 13 52
// | |___ | | 4 | 3 | 4 CSN 7* 7*
// | ___| | |---|---| 3 CE 8* 8*
// | |_________| | 2 ||1|| 2 VCC 3,3 3,3
// |------------------------------|---|---| 1 GND GND GND
//
// * modifiable par variable
//**************************************************************************************
#include "nRF24L01.h"
#include "RF24.h"
#include <SPI.h>
const uint64_t pipes[] = { 0xF0F0F0F0E1LL,
0xF0F0F0F0E2LL,
0xF0F0F0F0E3LL
}; // MOD : add slaves if required & copy to slaves code !
#define ThisNodeID 0 // Fix : ID of this MASTER
#define wait 6000 // Fix : max wait duration in us
int requestToNode; // AUTO : switch between slave
int nbSlaves; // AUTO : number of node (master excluded)
uint32_t data; // AUTO : node message
bool Astat = 0; // AUTO : action status (OK/Not OK)
unsigned long At = 0; // AUTO : last action (R/W) time in us
long Dt = 0; // AUTO : elapsed time in us
RF24 radio(9, 10);
void setup() {
Serial.begin(115200);
nbSlaves = (sizeof(pipes) / 8) - 1; // save the table size
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.setRetries(0, 0);
Serial.println(F("Master Initialized"));
requestToNode = nbSlaves + 1; // Initialize
}
void loop() {
// Node switch
if (requestToNode > nbSlaves) {
requestToNode = 1;
}
// Declare pipes
radio.stopListening();
radio.openWritingPipe(pipes[requestToNode]);
radio.openReadingPipe(requestToNode, pipes[requestToNode]);
radio.printDetails();
// Send request == lock other nodes
At = micros();
Dt = 0;
Astat = 0;
while ((Dt <= wait) && (Astat == 0)) {
Astat = radio.write(&requestToNode, sizeof(requestToNode));
Dt = micros() - At;
if (Astat == 1) {
Dt = micros() - At;
Serial.print(F("Sent request to Slave Node "));
Serial.print(requestToNode);
Serial.print(F(" in "));
Serial.print(Dt);
Serial.println(F(" us"));
}
}
if (Astat == 0) {
Serial.print(F("Send to Slave Node "));
Serial.print(requestToNode);
Serial.print(F(" failed. Waited "));
Serial.print(Dt);
Serial.println(F(" us. Realeased for fresh data"));
}
// Receive data from Slave
radio.startListening();
At = micros();
Dt = 0;
Astat = 0;
while ((Dt <= wait) && (Astat == 0)) {
Dt = micros() - At;
if (radio.available()) {
radio.read(&data, sizeof(data));
Dt = micros() - At;
Astat = 1;
Serial.print(F("Received from Slave Node "));
Serial.print(requestToNode);
Serial.print(F(" : "));
Serial.print(data / 10000);
Serial.print(F(" "));
Serial.print(data % 10000);
Serial.print(F(", in "));
Serial.print(Dt);
Serial.println(F(" us"));
}
}
if (Astat == 0) {
Serial.print(F("Receive from Slave Node "));
Serial.print(requestToNode);
Serial.print(F(" failed. Waited "));
Serial.print(Dt);
Serial.println(F(" us. Realeased for fresh data"));
}
// Request next Slave node
requestToNode = requestToNode + 1;
}
Slave code :
//*************Branchement**************************************************************
// RF Name Uno Mega
// |------------------------------|---|---| Nano
// | |___ ________ | 8 | 7 | 8 IRA N/A N/A
// | ___| (________) |---|---| 7 MISO 12 50
// | |___ | 6 | 5 | 6 MOSI 11 51
// | ___| | |---|---| 5 CSK 13 52
// | |___ | | 4 | 3 | 4 CSN 7* 7*
// | ___| | |---|---| 3 CE 8* 8*
// | |_________| | 2 ||1|| 2 VCC 3,3 3,3
// |------------------------------|---|---| 1 GND GND GND
//
// * modifiable par variable
//**************************************************************************************
#include "nRF24L01.h"
#include "RF24.h"
#include <SPI.h>
const uint64_t pipes[] = { 0xF0F0F0F0E1LL,
0xF0F0F0F0E2LL,
0xF0F0F0F0E3LL
}; // MOD : add slaves if required & copy to slaves code !
#define ThisNodeID 1 // MOD : ID of this NODE
#define wait 8000 // Fix : max wait duration in us
int nbSlaves; // AUTO : number of node (master excluded)
uint32_t data; // AUTO : node message
bool Astat = 0; // AUTO : action status (OK/Not OK)
unsigned long At = 0; // AUTO : last action (R/W) time in us
unsigned long Dt = 0; // AUTO : elapsed time in us
uint32_t value = 2000000; // AUTO : storage
RF24 radio(9, 10);
void setup() {
Serial.begin(115200);
nbSlaves = (sizeof(pipes) / 8) - 1; // save the table size
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(pipes[ThisNodeID]);
radio.openReadingPipe(ThisNodeID, pipes[ThisNodeID]);
radio.setRetries(0, 0);
Serial.println(F("Slave "));
Serial.print(ThisNodeID);
Serial.println(F(" Initialized"));
}
void loop() {
// Message
if (value > 2000000) {
value = 1001234;
}
else {
value = value + 10000;
}
// Waiting GO from Master
radio.startListening();
At = micros();
Dt = 0;
Astat = 0;
while ((Dt <= wait) && (Astat == 0)) {
Dt = micros() - At;
if (radio.available()) {
radio.read(&data, sizeof(data));
Dt = micros() - At;
if (data == ThisNodeID) {
Dt = micros() - At;
Astat = 1;
Serial.print(F("Go received after "));
Serial.print(Dt);
Serial.println(F(" us"));
}
}
}
if (Astat == 0) {
Serial.print(F("Go from Master Node missed. Waited "));
Serial.print(Dt);
Serial.println(F(" us. Realeased for fresh data"));
}
// Remplacement valeur data
data = value;
// Send value
if (Astat == 1) {
radio.stopListening();
At = micros();
Dt = 0;
Astat = 0;
while ((Dt <= wait) && (Astat == 0)) {
Astat = radio.write(&data, sizeof(data));
Dt = micros() - At;
if (Astat == 1) {
Serial.print(F("Sent "));
Serial.print(data);
Serial.print(F(" to Master Node in "));
Serial.print(Dt);
Serial.println(F(" us"));
}
}
if (Astat == 0) {
Serial.print(F("Send to Master Node failed. Waited "));
Serial.print(Dt);
Serial.println(F(" us. Realeased for fresh data"));
}
}
}