CODE:
HRFMessage
5.1.1 HRFMessageClient
Sends an unaddressed message every second. Any HRFMEssageServer within range
will receive the message and send a reply. Test this with the HRFMessageServer below.
#include <HRFMessage.h>
// Declare the HRFMessage to use Serial1 for IO
// with the HM-TR module
HRFMessage client(&Serial1);
long lastSendTime = 0;
void setup()
{
Serial.begin(9600); // Monitoring via USB
Serial1.begin(9600); // defaults to 9600
}
void loop()
{
// Send a message to the server at most once a second
long thisTime = millis();
if (thisTime > lastSendTime + 1000)
{
client.send((uint8_t*)"test\n", 6);
Serial.print("sending\n");
lastSendTime = thisTime;
}
// But always look for a reply
uint8_t buf[HRF_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (client.recv((uint8_t*)&buf, &len))
{
// Got a reply from the server
Serial.print("got: ");
Serial.print((const char*)buf);
}
}
5.1.2 HRFMessageServer
Server to work with HRFMessageClient above. When it receives a message from
HRFMessageClient it sends a reply.
#include <HRFMessage.h>
// Declare the HRFMessage to use Serial1 for
// IO with the HM-TR module
HRFMessage server(&Serial1);
void setup()
{
Serial.begin(9600); // Monitoring via USB
Serial1.begin(9600); // defaults to 9600
Sample code
6 of 12 HopeRF
}
void loop()
{
uint8_t buf[HRF_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (server.recv((uint8_t*)&buf, &len))
{
// Got a message from the client.
// Send a reply back
server.send((uint8_t*)"reply\n", 7);
Serial.print("got: ");
Serial.print((const char*)buf);
}
}