working version 1

This commit is contained in:
cally22
2023-07-08 22:44:00 +02:00
parent d1816ba485
commit 6cc938d45b
3 changed files with 105 additions and 59 deletions
+48 -9
View File
@@ -1,3 +1,4 @@
#include "HardwareSerial.h"
#ifndef RadioController_h
#define RadioController_h
@@ -10,32 +11,70 @@
class RadioController {
private:
RF24 radio; // CE, CSN
const uint64_t writePipe = 0xE8E8F0F0E1LL;
const uint64_t readPipe = 0xE8E8F0F0E2LL;
public:
void setup(int cepin, int cspin);
void send(String msg);
String receive();
const char* sendAck(char* msg, int& tx);
};
void RadioController::setup(int cepin, int cspin) {
uint8_t address[][6] = { "1Node", "2Node" };
radio = RF24(cepin, cspin);
if (!radio.begin()) {
Serial.println("Error initializing radio");
} else {
Serial.println("Succesfully initializing radio");
radio.openWritingPipe(writePipe);
radio.openReadingPipe(1, readPipe);
radio.setPALevel(RF24_PA_MAX);
radio.disableDynamicPayloads();
radio.setAutoAck(false);
radio.setRetries(5, 15);
// radio.openWritingPipe(writePipe);
// radio.openReadingPipe(1, readPipe);
// radio.setPALevel(RF24_PA_MAX);
// radio.disableDynamicPayloads();
// radio.setAutoAck(false);
// radio.setDataRate( RF24_250KBPS );
// radio.enableAckPayload();
// radio.setRetries(15, 15); // delay, count
// Set the PA Level low to try preventing power supply related problems
// because these examples are likely run with nodes in close proximity to
// each other.
radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default.
// to use ACK payloads, we need to enable dynamic payload lengths (for all nodes)
radio.enableDynamicPayloads(); // ACK payloads are dynamically sized
// Acknowledgement packets have no payloads by default. We need to enable
// this feature for all nodes (TX & RX) to use ACK payloads.
radio.enableAckPayload();
// set the TX address of the RX node into the TX pipe
radio.openWritingPipe(address[0]); // always uses pipe 0
// set the RX address of the TX node into a RX pipe
radio.openReadingPipe(1, address[1]); // using pipe 1
radio.stopListening(); // put radio in TX mode
}
}
const char* RadioController::sendAck(char* textToSend, int& tx) {
bool transmissionSuccess = radio.write(textToSend, strlen(textToSend));
tx = 0;
if (transmissionSuccess) {
if (radio.available()) {
static char receivedText[8] = ""; // Declare a static char array to hold the received text
radio.read(receivedText, sizeof(receivedText)); // Read the received text into the char array
tx = 1;
return receivedText; // Return the char array
} else {
Serial.println("Acknowledge but no data");
}
} else {
Serial.println("Transmission failed");
}
return ""; // Return an empty char array if no data was received
}
void RadioController::send(String msg) {
radio.stopListening();
const char *text = msg.c_str();
bool transmissionSuccess = radio.write(text, strlen(text));