Hi all,
I've been trying to follow along in the nice tutorial thread by Robin2,
but there are a few things I keep tripping up on.
For brevity, heres the code im using,
with comments in them of me trying to make sense of it;
TRANSMITTER SIDE :
#include <SPI.h> // Lets arduino talk to nRF24L01 radio module.
#include <nRF24L01.h> // Not sure, subsidiary to RF24.h ?
#include <RF24.h> // Translates functions to actual communication.
const byte server[5] = {'R', 'x', 'A', 'A', 'A'}; // address of server.
RF24 radio(9, 10); // Defines radio com lines.
char dataToSend[10] = "Message 0"; // Static part of message.
char txNum = '0'; // Changing part of message.
// Timer variables.
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
void setup() {
Serial.begin(9600); // Open serial feed.
Serial.println("SimpleTx Starting"); // Announce start.
radio.begin(); // Starts radio.
radio.setDataRate( RF24_250KBPS ); // Transmission rate.
radio.setRetries(3, 5); // Delay, Count.
radio.openWritingPipe(server); // Adress of server.
}
void loop() {
// Loop just checks if millis increased by 1000 or more,
// triggering a send() function if it has.
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
}
void send() {
// rslt set to TRUE if transmission acknowledged,
// blocks until out of tries, or success.
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Announces what has been sent.
Serial.print("Data Sent ");
Serial.print(dataToSend);
if (rslt) {
// Announces that it has succeeded.
Serial.println(" Acknowledge received");
updateMessage(); // Cycles dynamic part of message by one.
}
else {
// Announces that it has failed.
Serial.println(" Tx failed");
}
}
void updateMessage() {
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}
RECEIVER SIDE:
#include <SPI.h> // Lets arduino talk to nRF24L01 radio module.
#include <nRF24L01.h> // Not sure, subsidiary to RF24.h ?
#include <RF24.h> // Translates functions to actual communication.
const byte server[5] = {'R', 'x', 'A', 'A', 'A'}; // This servers address.
RF24 radio(9, 10); // Defines radio com lines.
char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;
void setup() {
Serial.begin(9600); // Open serial feed.
Serial.println("SimpleRx Starting"); // Announce start.
radio.begin(); // Starts radio.
radio.setDataRate( RF24_250KBPS ); // Transmission rate.
radio.openReadingPipe(1, server); // This confuses me.
radio.startListening(); // Start listening on pipe 1.
}
void loop() {
// If radio has new data; fetch and raise flag.
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
// If flag has been raised; present data, lower flag.
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
newData = false;
}
}
What confuses me is the receiver specifying a pipe (openReadingPipe).
The server has a 5 byte address, but then there are also 6 pipes?
The transmitting side does not specify a pipe, so how does this work?
Can I have multiple transmitters all calling the same address?
And if so, how can the receiver tell them apart?
Im sure its in a manual somewhere, but my googlefoo is failing me.
Can somebody throw me some hints? ![]()
Thanks,
- Soko