// rf95_server.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing server
// with the RH_RF95 class. RH_RF95 class does not provide for addressing or
// reliability, so you should only use RH_RF95 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example rf95_client
// Tested with Anarduino MiniWirelessLoRa, Rocket Scream Mini Ultra Pro with
// the RFM95W, Adafruit Feather M0 with RFM95
#include <SPI.h>
#include <RH_RF95.h>
#define TRY_SPI1
#ifdef TRY_SPI1
#include <RHHardwareSPI1.h>
// MISO 1, MOSI 0, SCK 20
#define RFM95_CS 26 //10
#define RFM95_RST 9
#define RFM95_INT 2
RH_RF95 rf95(RFM95_CS, RFM95_INT, hardware_spi1);
// Singleton instance of the radio driver
#else
// RH_RF95(uint8_t slaveSelectPin = SS, uint8_t interruptPin = 2, RHGenericSPI& spi = hardware_spi);
#define RFM95_CS 10
#define RFM95_RST 9
#define RFM95_INT 2
RH_RF95 rf95(RFM95_CS, RFM95_INT);
#endif
// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 915.0
void setup()
{
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
Serial.begin(9600);
while (!Serial) ; // Wait for serial port to be available
if (!rf95.init())
Serial.println("init failed");
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
if (!rf95.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
while (1);
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
rf95.setTxPower(23, false);
}
void loop()
{
if (rf95.available())
{
// Should be a message for us now
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf95.recv(buf, &len))
{
// digitalWrite(led, HIGH);
// RH_RF95::printBuffer("request: ", buf, len);
Serial.print("got request: ");
Serial.println((char*)buf);
Serial.print("RSSI: ");
Serial.println(rf95.lastRssi(), DEC);
// Send a reply
uint8_t data[] = "And hello back to you";
delay(5);
bool sent = rf95.send(data, sizeof(data));
Serial.print("Send reply ");
Serial.println((int)sent, DEC);
rf95.waitPacketSent();
Serial.println("Sent a reply");
// digitalWrite(led, LOW);
}
else
{
Serial.println("recv failed");
}
}
}