The Radiohead library has the ability to add a way for the integrity of the data sent to a receiver to be assured.
Just like the SendwithRetry of the RFM69 library.
Using the sketch that I posted above, if the receiver doesn't get all the data, there is no way for the sender to know, because the size of the data is not accounted for. So if it misses its target there is no way to get another one sent.
Also, when the sender transmits, everyone with a similar radio tuned on that frequency will get it and are able to see the data.
And any data sent by some other radio will be received by the receiver, even though it might not be the intended receiver.
You could, of course, just code an id and a data type on the data struct itself and make the receiver check in the struct if the data is intended for itself.
But using a Reliable Datagram, all that is taken care for.
Looking at the Radiohead site, you can check how to use the RHReliabledatagram (
http://www.airspayce.com/mikem/arduino/RadioHead/classRHReliableDatagram.html).
For instance, the data is sent by using a sendtoWait() like this:
sendtoWait ( uint8_t * buf, uint8_t len, uint8_t address)
And the receiver will use a recfromAck() like this:
recvfromAck ( uint8_t * buf, uint8_t * len, uint8_t * from = NULL, uint8_t * to = NULL, uint8_t * id = NULL, uint8_t * flags = NULL)
As you can see, the "address" (the CLIENT_ADDRESS) and the "from" (the SERVER_ADDRESS) are checked, as well as the data length.
Try these two sketches:
// rf95_reliable_datagram_server.pde
#include <RHReliableDatagram.h> //get it here http://lowpowerlab.com/RadioHead_LowPowerLab.zip
#include <RH_RF95.h> //get it here http://lowpowerlab.com/RadioHead_LowPowerLab.zip
#include <SPI.h> //get it here: https://www.github.com/lowpowerlab/spiflash
#define SERVER_ADDRESS 1 // address number of the Server
#define CLIENT_ADDRESS 2 // address number of the Client
#define RETRIES 4 // Number of times the sendtoWait() will try to send a message. Default is 3
#define TIMEOUT 2000 // Timeout before sendWait() tries again to send a message
// Match frequency to the hardware version of the radio on your Moteino
#define FREQUENCY 433
// Singleton instance of the radio driver
RH_RF95 driver;
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, SERVER_ADDRESS);
void setup()
{
// connect to the serial monitor at 115200 baud
Serial.begin(115200);
Serial.println("DATAGRAM TEST - SERVER");
Serial.println("");
//Initialize the radio
if (!manager.init())
Serial.println("Init failed!");
else
{
Serial.print("Init OK - ");
Serial.println(FREQUENCY); Serial.print("mhz");
driver.setFrequency(FREQUENCY);
}
manager.setRetries(RETRIES);
manager.setTimeout(TIMEOUT);
}
uint8_t data[] = "FROM SERVER: Message received!";
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
void loop()
{
if (manager.available())
{
uint8_t len = sizeof(buf);
uint8_t from;
// Wait for a message addressed to us from the client and send an ACKNOWLEDGE if it is received with the right length
if (manager.recvfromAck(buf, &len, &from))
{
Serial.print("The following message was received from and acknowledged to CLIENT ID: ");
Serial.print(from);
Serial.print(" -> ");
Serial.println((char*)buf);
//Now send a reply back to the originator client
if (!manager.sendtoWait(data, sizeof(data), from))
Serial.println("Transmission of data failed! ACK not received from CLIENT!");
else
Serial.println("A reply was sent to and acknowledged by the CLIENT");
Serial.println("");
}
else
Serial.println("ACK not received from CLIENT");
}
}
// rf95_reliable_datagram_client.pde
#include <RHReliableDatagram.h> //get it here http://lowpowerlab.com/RadioHead_LowPowerLab.zip
#include <RH_RF95.h> //get it here http://lowpowerlab.com/RadioHead_LowPowerLab.zip
#include <SPI.h> //get it here: https://www.github.com/lowpowerlab/spiflash
#define SERVER_ADDRESS 1 // address number of the Server
#define CLIENT_ADDRESS 2 // address number of the Client
#define RETRIES 4 // Number of times the sendtoWait() will try to send a message. Default is 3
#define TIMEOUT 2000 // Timeout before sendWait() tries again to send a message
// Match frequency to the hardware version of the radio on your Moteino
#define FREQUENCY 433
// Singleton instance of the radio driver
RH_RF95 driver;
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, CLIENT_ADDRESS);
void setup()
{
// connect to the serial monitor at 115200 baud
Serial.begin(115200);
Serial.println("DATAGRAM TEST - CLIENT");
Serial.println("");
//Initialize the radio
if (!manager.init())
Serial.println("Init failed!");
else
{
Serial.print("Init OK - ");
Serial.println(FREQUENCY); Serial.print("mhz");
driver.setFrequency(FREQUENCY);
}
manager.setRetries(RETRIES);
manager.setTimeout(TIMEOUT);
}
uint8_t data[] = "Hello there!";
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
void loop()
{
Serial.println("Sending a message to the SERVER... ");
// Send a message to manager_server
if (manager.sendtoWait(data, sizeof(data), SERVER_ADDRESS))
{
uint8_t len = sizeof(buf);
uint8_t from;
// Now wait for a reply from the server
if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
{
Serial.print("Got reply from server ID ");
Serial.print(from);
Serial.print(": ");
Serial.println((char*)buf);
Serial.println("");
}
else
{
Serial.println("No reply, is SERVER running?");
}
}
else
Serial.println("Transmission of data failed!");
delay(500);
}