uint8_t address[][6] = { "1Node", "2Node" };
bool radioNumber = 1;
bool role = false;
struct PayloadStruct {
char message[7];
uint8_t counter;
};
PayloadStruct payload;
void setup() {
while (!Serial) {
}
delay(5000);
Serial.println(F("radio hardware is not responding!!"));
while (1) {}
}
Serial.println(F("RF24/examples/AcknowledgementPayloads"));
Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
while (!Serial.available()) {
}
char input = Serial.parseInt();
radioNumber = input == 1;
Serial.print(F("radioNumber = "));
Serial.println((int)radioNumber);
Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
if (role) {
memcpy(payload.message, "Hello ", 6);
} else {
memcpy(payload.message, "World ", 6);
}
}
void loop() {
if (role) {
unsigned long start_timer = micros();
bool report = radio.
write(&payload,
sizeof(payload));
unsigned long end_timer = micros();
if (report) {
Serial.print(F("Transmission successful! "));
Serial.print(F("Time to transmit = "));
Serial.print(end_timer - start_timer);
Serial.print(F(" us. Sent: "));
Serial.print(payload.message);
Serial.print(payload.counter);
uint8_t pipe;
PayloadStruct received;
radio.
read(&received,
sizeof(received));
Serial.print(F(" Recieved "));
Serial.print(F(" bytes on pipe "));
Serial.print(pipe);
Serial.print(F(": "));
Serial.print(received.message);
Serial.println(received.counter);
payload.counter = received.counter + 1;
} else {
Serial.println(F(" Recieved: an empty ACK packet"));
}
} else {
Serial.println(F("Transmission failed or timed out"));
}
delay(1000);
} else {
uint8_t pipe;
PayloadStruct received;
radio.
read(&received,
sizeof(received));
Serial.print(F("Received "));
Serial.print(bytes);
Serial.print(F(" bytes on pipe "));
Serial.print(pipe);
Serial.print(F(": "));
Serial.print(received.message);
Serial.print(received.counter);
Serial.print(F(" Sent: "));
Serial.print(payload.message);
Serial.println(payload.counter);
payload.counter = received.counter + 1;
}
}
if (Serial.available()) {
char c = toupper(Serial.read());
if (c == 'T' && !role) {
role = true;
Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
memcpy(payload.message, "Hello ", 6);
} else if (c == 'R' && role) {
role = false;
Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
memcpy(payload.message, "World ", 6);
}
}
}
Driver class for nRF52840 2.4GHz Wireless Transceiver.
void startListening(bool resetAddresses=true)
void openReadingPipe(uint8_t child, const uint8_t *address)
void setPALevel(uint8_t level, bool lnaEnable=true)
bool write(void *buf, uint8_t len, bool multicast=false, bool doEncryption=true)
uint8_t getDynamicPayloadSize()
bool writeAckPayload(uint8_t pipe, void *buf, uint8_t len)
void enableDynamicPayloads(uint8_t payloadSize=DEFAULT_MAX_PAYLOAD_SIZE)
void openWritingPipe(const uint8_t *address)
void read(void *buf, uint8_t len)
void stopListening(bool setWritingPipe=true, bool resetAddresses=true)