wireless working

This commit is contained in:
2019-05-26 22:58:04 +02:00
parent ee57e22b26
commit c3958b5eec
4 changed files with 26 additions and 49 deletions
+19 -41
View File
@@ -1,65 +1,43 @@
#include <SPI.h>
#include "RF24.h"
#include <RF24Network.h>
#include "TransferDataStructure.h"
// Nrf24 settings
byte addresses[][6] = {"1Node", "2Node"};
RF24 radio(9, 10); // CE, CSN
RF24Network network(radio);
const uint16_t this_node = 00; // Address of our node in Octal format
const uint16_t other_node = 01; // Address of the other node in Octal format
void NrfInit()
{
SPI.begin();
radio.begin();
network.begin(/*channel*/ 90, /*node address*/ this_node);
// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
// Possibly init data object here
radio.startListening();
}
dataStruct2 NrfSendData(dataStruct2 data, bool debug = false)
{
radio.stopListening();
// First, stop listening so we can talk.
network.update();
if (debug)
Serial.println(F("Now sending"));
Serial.print("Sending...");
if (!radio.write( &data, sizeof(data) )) {
if (debug)
Serial.println(F("failed"));
return data;
RF24NetworkHeader header(/*to node*/ other_node);
bool ok = network.write(header, &data, sizeof(data));
if (debug)
{
if (ok)
Serial.println("ok.");
else
Serial.println("failed.");
}
radio.startListening();
// Now, continue listening
unsigned long started_waiting_at = micros();
// Set up a timeout period, get the current microseconds
boolean timeout = false;
// Set up a variable to indicate if a response was received or not
while ( ! radio.available() ) {
if (micros() - started_waiting_at > 200000 ) {
// If waited longer than 200ms, indicate timeout and exit while loop
timeout = true;
break;
}
}
if ( timeout ) {
if (debug)
Serial.println(F("Failed, response timed out."));
return data;
} else {
// Grab the response, compare, and send to debugging spew
radio.read( &data, sizeof(data) );
return data;
}
return data;
}