I'm using an rf 24 library to make a two ways communication between two Arduino UNOs.
One Arduino UNO is connected to NRF24L01 with an antenna, and the other is connected to a regular NRF24L01.
I plan to use the transmitter as a ground station, and the other as a drone.
So, the ground station side should show sensor data from the drone, and the drone should receive control data from the ground station.
I have connected CE and CNS to pins 7 and 8.
Using ICSP for MOSI, SCK, and MISO.
The ground station fails to radio.write().
This is for drone.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
// NRF
RF24 radio(7, 8); // CE, CSN
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
bool newData = false;
struct Data_Package_control {
int c1;
int c2;
int c3;
int c4;
int c5;
};
struct Data_Package_sensor {
int s1;
int s2;
int s3;
int s4;
int s5;
int s6;
int s7;
};
// Create variables with the above structures
Data_Package_control data_c;
Data_Package_sensor data_s;
// Control Surfaces
// create servo object to control a servo
// Servo aileronLServo;
void setup() {
// test
Serial.begin(9600);
// Servo
// aileronLServo.attach(9,600,2600);
// NRF24
radio.begin();
radio.setChannel(125);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_MIN);
radio.openReadingPipe(1, thisSlaveAddress);
radio.enableAckPayload();
radio.startListening();
radio.writeAckPayload(1, &data_s, sizeof(Data_Package_sensor));
}
void loop() {
getData();
showData();
}
void resetData() {
// Reset the values when there is no radio connection - Set initial default values
// aileronLServo.write(0);
// aileronRServo.write(0);
// servo
// servo
// servo
}
void updateMessage() {
// new sensor data -> sensor package
data_s.s1 = 22;
data_s.s2 = 22;
data_s.s3 = 22;
data_s.s4 = 22;
data_s.s5 = 22;
data_s.s6 = 22;
data_s.s7 = 22;
radio.writeAckPayload(1, &data_s, sizeof(Data_Package_sensor));
}
void getData() {
if ( radio.available() ) {
radio.read( &data_c, sizeof(Data_Package_control) );
updateMessage();
newData = true;
}
else {
Serial.println(" Failed to receive");
}
}
void showData() {
if (newData == true) {
// aileronLServo.write(data_c.c2);
Serial.print("Data received ");
Serial.println(data_c.c2);
newData = false;
}else{
resetData();
}
}
This is for ground station.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte slaveAddress[5] = {'R','x','A','A','A'};
bool newData = false;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 100;
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
// byte, int, float, string
struct Data_Package_control {
int c1;
int c2;
int c3;
int c4;
int c5;
};
struct Data_Package_sensor {
int s1;
int s2;
int s3;
int s4;
int s5;
int s6;
int s7;
};
// Create variables with the above structures
Data_Package_control data_c;
Data_Package_sensor data_s;
// Joystick
int VRx = A0;
int VRy = A1;
int SW = 2;
int SW_state = 0;
void setup() {
Serial.begin(9600);
// Joystick
pinMode(VRx, INPUT);
pinMode(VRy, INPUT);
pinMode(SW, INPUT_PULLUP);
// NRF24
radio.begin();
radio.setChannel(125);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_MIN);
radio.setRetries(5,5);
radio.enableAckPayload();
radio.openWritingPipe(slaveAddress);
}
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
}
showData();
}
void showData() {
if (newData == true) {
Serial.print(data_s.s1);
Serial.print(" ");
Serial.print(data_s.s2);
Serial.print(" ");
Serial.print(data_s.s3);
Serial.print(" ");
Serial.print(data_s.s4);
Serial.print(" ");
Serial.print(data_s.s5);
Serial.print(" ");
Serial.print(data_s.s6);
Serial.print(" ");
Serial.println(data_s.s7);
}
}
void send() {
bool rslt;
rslt = radio.write( &data_c, sizeof(Data_Package_control) );
if (rslt) {
if ( radio.isAckPayloadAvailable() ) {
radio.read(&data_s, sizeof(Data_Package_sensor));
newData = true;
}
else {
Serial.println(" Acknowledge but no data ");
}
updateMessage();
}
else {
Serial.println(" Tx failed");
}
prevMillis = millis();
}
void updateMessage() {
data_c.c1 = digitalRead(SW);
data_c.c2 = map(analogRead(VRx), 0, 1023, 0, 180);
data_c.c3 = map(analogRead(VRy), 0, 1023, 0, 180);
data_c.c4 = 20;
data_c.c5 = 40;
}