Bi directional communication not working

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

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

...R

Robin2:
Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

In line with the above recommendation, I would like to add that the OP may first check bi-directional communication between the Arduinos using wired network such as 'Software UART Port. After that, he can conveniently replace the wired connection by a easy to implement (HC12) wireless network. This strategy will allow the OP to write codes from his own.

struct outgoing
{
  int id=105;
  float temperature = 18.3;
  char  ID[100] = "ID: 01234567";
};

The maximal packet size possible is 32 bytes.

GolamMostafa:
he can conveniently replace the wired connection by a easy to implement (HC12) wireless network.

You seem to have missed the fact that the OP is using nRF24L01+ wireless transceivers for which Serial is irrelevant.

...R