first commit
This commit is contained in:
69
examples/serial/serial_gateway/serial_gateway.ino
Normal file
69
examples/serial/serial_gateway/serial_gateway.ino
Normal file
@@ -0,0 +1,69 @@
|
||||
// serial_gateway.pde
|
||||
// This sketch can be used as a gateway between 2 RadioHead radio networks (connected by a serial line),
|
||||
// or between
|
||||
// 1 RadioHead radio network and a Unix host.
|
||||
// It relays all messages received on the radio driver to the serial port
|
||||
// (using the RH_Serial driver and protocol). And it relays all messages received on the RH_Serial
|
||||
// driver port to the radio driver.
|
||||
// Both drivers operate in promiscuous mode and preserve all headers, so this sketch acts as
|
||||
// a transparent gateway between RH_Serial and and other RadioHead driver.
|
||||
//
|
||||
// By replacing RH_Serial with another RadioHead driver, this can act as a universal gateway
|
||||
// between any 2 RadioHead networks.
|
||||
//
|
||||
// Tested with RF22 driver and RH_Serial driver. The serial port was connected to a Unix host, where the
|
||||
// serial_reliable_datagram_server.pde was built and running like this:
|
||||
// tools/simBuild examples/serial/serial_reliable_datagram_server/serial_reliable_datagram_server.pde
|
||||
// RH_HARDWARESERIAL_DEVICE_NAME=/dev/ttyUSB1 ./serial_reliable_datagram_server
|
||||
// -*- mode: C++ -*-
|
||||
//
|
||||
|
||||
#include <SPI.h>
|
||||
#include <RH_RF22.h>
|
||||
#include <RH_Serial.h>
|
||||
|
||||
// Singleton instance of the radio driver
|
||||
// You can use other radio drivers if you want
|
||||
RH_RF22 radio;
|
||||
// Singleton instance of the serial driver which relays all messages
|
||||
// via Serial to another RadioHead RH_Serial driver, perhaps on a Unix host.
|
||||
// You could use a different Serial if the arduino has more than 1, eg Serial1 on a Mega
|
||||
RH_Serial serial(Serial);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
if (!radio.init())
|
||||
Serial.println("radio init failed");
|
||||
radio.setPromiscuous(true);
|
||||
// Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
|
||||
if (!serial.init())
|
||||
Serial.println("serial init failed");
|
||||
serial.setPromiscuous(true);
|
||||
}
|
||||
|
||||
uint8_t buf[RH_SERIAL_MAX_MESSAGE_LEN];
|
||||
void loop()
|
||||
{
|
||||
if (radio.available())
|
||||
{
|
||||
uint8_t len = sizeof(buf);
|
||||
radio.recv(buf, &len);
|
||||
serial.setHeaderTo(radio.headerTo());
|
||||
serial.setHeaderFrom(radio.headerFrom());
|
||||
serial.setHeaderId(radio.headerId());
|
||||
serial.setHeaderFlags(radio.headerFlags(), 0xFF); // Must clear all flags
|
||||
serial.send(buf, len);
|
||||
}
|
||||
if (serial.available())
|
||||
{
|
||||
uint8_t len = sizeof(buf);
|
||||
serial.recv(buf, &len);
|
||||
radio.setHeaderTo(serial.headerTo());
|
||||
radio.setHeaderFrom(serial.headerFrom());
|
||||
radio.setHeaderId(serial.headerId());
|
||||
radio.setHeaderFlags(serial.headerFlags(), 0xFF); // Must clear all flags
|
||||
radio.send(buf, len);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// serial_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_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_server
|
||||
// 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
|
||||
// Also works on Linux and OSX. Build and test with:
|
||||
// tools/simBuild examples/serial/serial_reliable_datagram_client/serial_reliable_datagram_client.pde
|
||||
// RH_HARDWARESERIAL_DEVICE_NAME=/dev/ttyUSB1 ./serial_reliable_datagram_client
|
||||
|
||||
#include <RHReliableDatagram.h>
|
||||
#include <RH_Serial.h>
|
||||
|
||||
#define CLIENT_ADDRESS 1
|
||||
#define SERVER_ADDRESS 2
|
||||
|
||||
#if (RH_PLATFORM == RH_PLATFORM_UNIX)
|
||||
#include <RHutil/HardwareSerial.h>
|
||||
// On Unix we connect to a physical serial port
|
||||
// You can override this with RH_HARDWARESERIAL_DEVICE_NAME environment variable
|
||||
HardwareSerial hardwareserial("/dev/ttyUSB0");
|
||||
RH_Serial driver(hardwareserial);
|
||||
|
||||
#else
|
||||
// On arduino etc, use a predefined local serial port
|
||||
// eg Serial1 on a Mega
|
||||
#include <SPI.h>
|
||||
// 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 driver(Serial1);
|
||||
#endif
|
||||
|
||||
|
||||
// Class to manage message delivery and receipt, using the driver declared above
|
||||
RHReliableDatagram manager(driver, CLIENT_ADDRESS);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
// Configure the port RH_Serial will use:
|
||||
driver.serial().begin(9600);
|
||||
if (!manager.init())
|
||||
Serial.println("init failed");
|
||||
//manager.setTimeout(2000); // Might need this at slow data rates or if a radio is involved
|
||||
}
|
||||
|
||||
uint8_t data[] = "Hello World!";
|
||||
// Dont put this on the stack:
|
||||
uint8_t buf[RH_SERIAL_MAX_MESSAGE_LEN];
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.println("Sending to serial_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, 6000, &from))
|
||||
{
|
||||
Serial.print("got reply from : 0x");
|
||||
Serial.print(from, HEX);
|
||||
Serial.print(": ");
|
||||
Serial.println((char*)buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("No reply, is serial_reliable_datagram_server running?");
|
||||
}
|
||||
}
|
||||
else
|
||||
Serial.println("sendtoWait failed");
|
||||
delay(500);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
// 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
|
||||
// Also works on Linux an OSX. Build and test with:
|
||||
// tools/simBuild examples/serial/serial_reliable_datagram_server/serial_reliable_datagram_server.pde
|
||||
// RH_HARDWARESERIAL_DEVICE_NAME=/dev/ttyUSB0 ./serial_reliable_datagram_server
|
||||
|
||||
#include <RHReliableDatagram.h>
|
||||
#include <RH_Serial.h>
|
||||
|
||||
#define CLIENT_ADDRESS 1
|
||||
#define SERVER_ADDRESS 2
|
||||
|
||||
#if (RH_PLATFORM == RH_PLATFORM_UNIX)
|
||||
#include <RHutil/HardwareSerial.h>
|
||||
// On Unix we connect to a physical serial port
|
||||
// You can override this with RH_HARDWARESERIAL_DEVICE_NAME environment variable
|
||||
HardwareSerial hardwareserial("/dev/ttyUSB0");
|
||||
RH_Serial driver(hardwareserial);
|
||||
|
||||
#else
|
||||
// On arduino etc, use a predefined local serial port
|
||||
// eg Serial1 on a Mega
|
||||
#include <SPI.h>
|
||||
// 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 driver(Serial1);
|
||||
#endif
|
||||
|
||||
// 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:
|
||||
driver.serial().begin(9600);
|
||||
if (!manager.init())
|
||||
Serial.println("init failed");
|
||||
// manager.setTimeout(2000); // Might need this at slow data rates or if a radio is involved
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user