433MHz Transmitter and Receiver on same Arduino (RadioHead)

I've been having trouble trying to get this to work. So basically, I'm trying to get some sort of bidirectional communication between two arduinos, but it is a one way communication that happens on different times i.e. Board 1 sends message to Board 2, Board two recieves it and sends a message back to Board 1 (that way it's not simultaneous). I'm using RadioHead library for this, these are the codes (after including libraries):
Board 1

// Msg 2 size expected
const byte tamMsg2 = 6;

// Create objects
RH_ASK askTx; // Transmitter
RH_ASK askRx; // Receiver

void setup() {
  Serial.begin(9600);
  // Initialize objects
  askTx.init();
  askRx.init();
}

void loop() {
  // Send message
  const char *msg1 = "ABCDEF";
  askTx.send((uint8_t *)msg1, strlen(msg1));
  askTx.waitPacketSent();
  Serial.print("Msg sent: ");
  Serial.println(msg1);


  // Recieve msg from board 2
  uint8_t msg2[tamMsg2];
  uint8_t msg2Len = sizeof(msg2);
  if (askRx.recv(msg2, &msg2Len)){
    Serial.print("Msg received: ");
    Serial.println((char *)msg2);
    delay(1000);
  }
  delay(1000);
}

Board 2:

// Size of msg expected
const byte tamMsg1 = 6;

// Create objects
RH_ASK askRx; // Receiver
RH_ASK askTx; // Transmitter

void setup() {
  Serial.begin(9600);
  // Initialize objects
  askRx.init();
  askTx.init();
}

void loop() {
  // Receive msg from Board 1
  uint8_t msg1[tamMsg1];
  uint8_t msg1Len = sizeof(msg1);
  if (askRx.recv(msg1, &msg1Len)){
    Serial.print("Msg received: ");
    Serial.println((char *)msg1);

    If received send msg to Board 1
    const char *msg2 = "ASDFGH";
    askTx.send((uint8_t *)msg2, strlen(msg2));
    askTx.waitPacketSent();
    
    delay(1000);
  }

}

Circuit is well built as I can have a one way communication B1->B2 and B2->B1, but when trying both on same code it doesn't work.

Anyone knows how to fix this? Please helppp.
Thanks in advance.

PD: noticed also that one way communication doesn't work when trying to init() both the Tx and Rx so this maybe the source of the problem, but I don't know how to fix this either.

If you are using an AVR based Arduino, you can't have two instances of the library operational at the same time, because they conflict over timer use.

That may be possible with other MCUs, but I doubt such a setup can be practical, when there are so many inexpensive transceivers that also vastly outperform the modules you are using.

Is there a way for me to initialize and "un-initialize" an instance during the loop? That way I can have operational the one that I'm using at that current moment?

Or a better question would be: how can I manage to do this one way communication back and forth on two different times with the modules I'm using without having this issue?
I've read posts that say it's possible but never explain how speciffically

Hi, @silva8064
Welcome to the forum.

What you are trying to make is a "half duplex" system

Google;

arduino half duplex 433MHz

This will provide you with some methods.

Tom.. :smiley: :+1: :coffee: :australia:

In principle the library should have a destructor, which will deactivate that instance. I haven't looked.

Thanks everyone for the answers. Now it works!

In case others have the same question, it may be helpful for them if you posted the final, working code.

You're right, my bad.
So basically, as said above, the problem was having two instances of RH_ASK operational at the same time (askRx for receiver and askTx for transmitter), and that could be solved by just creating a single instance (driver) that does both tranmsit and receive. These were the codes that worked best:
Board 1:

// Include libraries 
#include <RH_ASK.h>
#include <SPI.h>

// Msg 2 size expected
const byte tamMsg2 = 6;

// Create ASK objects (Tx on pin 12 & Rx on pin 11)
RH_ASK driver;

void setup() {
  Serial.begin(9600);
  // Initialize object
  driver.init();
}

void loop() {
  // Send message
  const char *msg1 = "ABCDEF";
  driver.send((uint8_t *)msg1, strlen(msg1));
  driver.waitPacketSent();
  Serial.print("Msg sent: ");
  Serial.println(msg1);
  delay(100);

  // Receive msg from board 2
  uint8_t msg2[tamMsg2];
  uint8_t msg2Len = sizeof(msg2);

  bool listened = false;
  int t = 0;
  while ((! listened) && (t < 15)) {
    listened = driver.recv(msg2, &msg2Len);
    t ++;
    delay(100);
  }
  if (listened) {
    Serial.print("Msg received: ");
    Serial.println((char *)msg2);
    
    delay(1000);
  } else {
    Serial.print("NOTHING");
  }
}

Board 2:

// Include libraries
#include <RH_ASK.h>
#include <SPI.h>

// Msg 1 size expected
const byte tamMsg1 = 6;


// Crear ASK object  (Tx 12 & Rx 11)
RH_ASK driver;

void setup() {
  Serial.begin(9600);
  // Initialize object
  driver.init();
}

void loop() {
  // Receive msj from Board 1
  uint8_t msg1[tamMsg1];
  uint8_t msg1Len = sizeof(msg1);
  if (driver.recv(msg1, &msg1Len)){
    Serial.print("Msg received: ");
    Serial.println((char *)msg1);

    // Send msg to Board 1
    const char *msg2 = "ASDFGH";
    int t = 0;
    while (t<4){
      driver.send((uint8_t *)msg2, strlen(msg2));
      driver.waitPacketSent();
      t++;
      delay(100);
    }
    Serial.print("Msg sent: ");
    Serial.println(msg2);

    delay(200);
  }
}

Also had the problem that B1 won't allways get the msg when listening instantly, that's why I used 2 while cycles: the one for B1 makes it so it listens for 1.5s (0.1 duration loop) or until it receives B2's msg, and B2's makes it so it sends 4 msgs (with 0.1s delay each as well) so that B1 catches one of them.
These delays worked for me so that B2 would be allways ready to receive when B1 finishes it's loop, otherwise 1 of every 2 msgs sent by B1 would be received by B2, and that means a 1.5s delay between communcations (B1 main loop duration).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.