Hi all,
I am brand new to the Arduino world , and i want to experiment with bi-directional communication. Transceiver A is supposed to send a message to transceiver B and if B receives the message, it should print the message from A and send a message back to A. If A receives the message, it should print the message from B and the cycle starts all over.
Here is my code for transceiver A:
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
RF24 radio(7, 8); // CE, CSN
struct outgoing
{
int id=1;
float temperature = 18.3;
char text[100] = "Text to be transmitted";
};
typedef struct outgoing Outgoing;
Outgoing transmitted_data;
struct incoming
{
int id=0;
float temperature = 0.0;
char ID[100] = "empty";
};
typedef struct incoming Incoming;
Incoming received_data;
const byte addresses[][6] = {"00001", "00002"};
void setup() {
Serial.begin(115200);
delay(1000);
radio.begin();
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(2, addresses[1]);
radio.setPALevel(RF24_PA_MAX);
}
void loop() {
radio.stopListening();
Serial.println("Transmitting...");
radio.write(&transmitted_data, sizeof(transmitted_data));
transmitted_data.id = transmitted_data.id + 1;
transmitted_data.temperature = transmitted_data.temperature+0.1;
delay(1000);
radio.startListening();
if (radio.available())
{
Serial.print("Receiveing...");
while (radio.available())
{
radio.read( &received_data, sizeof(received_data) );
}
Serial.print("\nPackage:");
Serial.print(received_data.id);
Serial.print("\n");
Serial.println(received_data.temperature);
Serial.println(received_data.ID);
}
}
And my code for transceiver B:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
RF24 radio(7, 8); // CE, CSN
struct incoming
{
int id=0;
float temperature = 0.0;
char text[100] = "empty";
};
typedef struct incoming Incoming;
Incoming received_data;
struct outgoing
{
int id=105;
float temperature = 18.3;
char ID[100] = "ID: 01234567";
};
typedef struct outgoing Outgoing;
Outgoing transmitted_data;
const byte addresses[][6] = {"00001", "00002"};
int flag = 0;
void setup() {
Serial.begin(9600);
delay(1000);
radio.begin();
radio.openReadingPipe(1, addresses[0]);
radio.openWritingPipe(addresses[1]);
radio.setPALevel(RF24_PA_MAX);
}
void loop() {
radio.startListening();
if (radio.available())
{
flag = 1;
while (radio.available())
{
radio.read( &received_data, sizeof(received_data) );
}
Serial.print("\nPackage:");
Serial.print(received_data.id);
Serial.print("\n");
Serial.println(received_data.temperature);
Serial.println(received_data.text);
}
if (flag == 1){
Serial.println("Transmitting...");
radio.stopListening();
radio.write(&transmitted_data, sizeof(transmitted_data));
flag = 0;
}
}
The problem I'm having is that while Transceiver A is able to send the message to transceiver B and B is able to print the message, A does not print the message from B. I am not sure if A just does not receive the message sent from B or whether B even actually sends the message in the first place.
Any help on this front would be highly appreciated.
Thank you