60 lines
1.6 KiB
Plaintext
60 lines
1.6 KiB
Plaintext
// ask_reliable_datagram_client.pde
|
|
// -*- mode: C++ -*-
|
|
// Example sketch showing how to create a simple addressed, reliable messaging client
|
|
// with the RHReliableDatagram class, using the RH_ASK driver to control a ASK radio.
|
|
// It is designed to work with the other example ask_reliable_datagram_server
|
|
// Tested on Arduino Mega, Duemilanova, Uno, Due, Teensy
|
|
|
|
#include <RHReliableDatagram.h>
|
|
#include <RH_ASK.h>
|
|
#include <SPI.h>
|
|
|
|
#define CLIENT_ADDRESS 1
|
|
#define SERVER_ADDRESS 2
|
|
|
|
// Singleton instance of the radio driver
|
|
RH_ASK driver;
|
|
// RH_ASK driver(2000, 2, 4, 5); // ESP8266: do not use pin 11
|
|
|
|
// Class to manage message delivery and receipt, using the driver declared above
|
|
RHReliableDatagram manager(driver, CLIENT_ADDRESS);
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(9600);
|
|
if (!manager.init())
|
|
Serial.println("init failed");
|
|
}
|
|
|
|
uint8_t data[] = "Hello World!";
|
|
// Dont put this on the stack:
|
|
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
|
|
|
|
void loop()
|
|
{
|
|
Serial.println("Sending to ask_reliable_datagram_server");
|
|
|
|
// Send a message to manager_server
|
|
if (manager.sendtoWait(data, sizeof(data), SERVER_ADDRESS))
|
|
{
|
|
// Now wait for a reply from the server
|
|
uint8_t len = sizeof(buf);
|
|
uint8_t from;
|
|
if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
|
|
{
|
|
Serial.print("got reply from : 0x");
|
|
Serial.print(from, HEX);
|
|
Serial.print(": ");
|
|
Serial.println((char*)buf);
|
|
}
|
|
else
|
|
{
|
|
Serial.println("No reply, is ask_reliable_datagram_server running?");
|
|
}
|
|
}
|
|
else
|
|
Serial.println("sendtoWait failed");
|
|
delay(500);
|
|
}
|
|
|