RadioHead
serial_encrypted_reliable_datagram_server.pde
// serial_reliable_datagram_server.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, reliable messaging server
// with the RHReliableDatagram class, using the RH_Serial driver to
// communicate using packets over a serial port (or a radio connected to a
// serial port, such as the 3DR Telemetry radio V1 and others).
// It is designed to work with the other example serial_reliable_datagram_client
// Tested on Arduino Mega and ChipKit Uno32 (normal Arduinos only have one
// serial port and so it not possible to test on them and still have debug
// output)
// Tested with Arduino Mega, Teensy 3.1, Moteino, Arduino Due
#include <RHReliableDatagram.h>
#include <RH_Serial.h>
#include <RHEncryptedDriver.h>
#include <Speck.h>
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
// On arduino etc, use a predefined local serial port
// eg Serial1 on a Mega
// Singleton instance of the Serial driver, configured
// to use the port Serial1. Caution: on Uno32, Serial1 is on pins 39 (Rx) and
// 40 (Tx)
RH_Serial raw_driver(Serial1);
// You can choose any of several encryption ciphers
Speck myCipher; // Instantiate a Speck block ciphering
// The RHEncryptedDriver acts as a wrapper for the actual radio driver
RHEncryptedDriver driver(raw_driver, myCipher);
// The key MUST be the same as the one in the client
unsigned char encryptkey[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, SERVER_ADDRESS);
void setup()
{
Serial.begin(9600);
// Configure the port RH_Serial will use:
raw_driver.serial().begin(9600);
if (!manager.init())
Serial.println("init failed");
// Now set up the encryption key in our cipher
myCipher.setKey(encryptkey, sizeof(encryptkey));
}
uint8_t data[] = "And hello back to you";
// Dont put this on the stack:
uint8_t buf[RH_SERIAL_MAX_MESSAGE_LEN];
void loop()
{
// Wait for a message addressed to us from the client
manager.waitAvailable();
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAck(buf, &len, &from))
{
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
// Send a reply back to the originator client
if (!manager.sendtoWait(data, sizeof(data), from))
Serial.println("sendtoWait failed");
}
}