Skip to content

Instantly share code, notes, and snippets.

@jenschr
Last active September 17, 2017 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenschr/4649afdd236e6ed10ffdf8d697f6f6f4 to your computer and use it in GitHub Desktop.
Save jenschr/4649afdd236e6ed10ffdf8d697f6f6f4 to your computer and use it in GitHub Desktop.
/*
Basic server for Adafruit Feather LoRa radios.
This is based on the RadioHead library for RF95, but has all
the required pins and setup for Feather LoRa's.
NOTE: Each client needs a unique radio address for this to work
*/
#include <RHReliableDatagram.h>
#include <RH_RF95.h>
#include <SPI.h>
// Client address isn't really used in the server. It will reply
// back to any client sending to it.
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 3 // Feather LoRa M0 = 3, Feather LoRa 32U4 = 7
#define RF95_FREQ 868.0
// Singleton instance of the radio driver
RH_RF95 driver(RFM95_CS, RFM95_INT);
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, SERVER_ADDRESS);
char data[20];
// Dont put this on the stack:
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
void setup()
{
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
Serial.begin(9600);
delay(100);
// manual reset
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
while (!manager.init()) {
Serial.println("Datagram init failed");
while (1);
}
Serial.println("Datagram init OK!");
if (!driver.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
while (1);
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
// The default transmitter power is 13dBm, using PA_BOOST.
driver.setTxPower(23, false);
}
void loop()
{
if (manager.available())
{
// Wait for a message addressed to us from the client
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 client confirming address and data
sprintf(data,"ACK 0x%x %s", from, buf);
if (!manager.sendtoWait((uint8_t*)data, sizeof(data), from))
Serial.println("reply failed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment