Particle Photon / Electron + RFN95W Long Range Radio

I updated the Reliable Datagram Code for the Photon & Adafruit Feather LoRa module to include the following:

  • RSSI signal strength for each message received.
  • LED Flash Indicators to detect if the message was successful or not.
  • Added Message Counter so each message sent has a number attached that increases every message.

Photon Code -

//PHOTON
// this the client running on photon
#include <RHReliableDatagram.h>
#include <RH_RF95.h>
//#include <SPI.h>
SYSTEM_MODE(SEMI_AUTOMATIC);

#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

const int ledPin = D7;     // D7 has an LED

// Singleton instance of the radio driver
RH_RF95 driver;  //(A2,D2)-->you don't nee set this
//RH_RF95 rf95(5, 2); // Rocket Scream Mini Ultra Pro with the RFM95W

// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, CLIENT_ADDRESS);

// Need this on Arduino Zero with SerialUSB port (eg RocketScream Mini Ultra Pro)
//#define Serial SerialUSB

void setup()
{
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);


  Serial.begin(9600);

  if (!manager.init())
  Serial.println("init failed");
  driver.setTxPower(23, false);
  driver.setFrequency(915.0);  // hay que setear frecuencia para los rfm96 915
  delay(5000);
  Serial.println("..this is the Photon as Client Radio");
}

//uint8_t data[] = "Hello World #      ";
// Dont put this on the stack:
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];

int16_t packetnum = 0;  // packet counter, we increment per xmission

void loop()
{

  Serial.println("Sending to Server Base Station");
  // Send a message to manager_server basestation.

  char radiopacket[20] = "Hello World #      ";
  itoa(packetnum++, radiopacket+13, 10);
  Serial.print("Transmitting > "); Serial.println(radiopacket);
  radiopacket[19] = 0;
  Serial.println("");

  if (manager.sendtoWait((uint8_t *)radiopacket, sizeof(radiopacket), SERVER_ADDRESS))
  {
    // Now wait for a reply from the server base station.
    uint8_t len = sizeof(buf);
    uint8_t from;

    Serial.println("Waiting for reply..."); delay(10);

    if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
    {
      //Flash D7 LED on Photon if Received Reply
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);

      Serial.print("Got reply from Server Base Station 0x");
      Serial.print(from, HEX);
      Serial.print(": ");
      Serial.println((char*)buf);
      Serial.print("RSSI: ");
      Serial.println(driver.lastRssi(), DEC);

      Serial.println();
      Serial.println();

    }
    else
    {

      Serial.println("No reply from Server Base Station?");

      //Flash D7 LED on Photon if No Reply From Server Base Station.
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(500);
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
    }
  }
  else{
    Serial.println("sendtoWait failed");
    digitalWrite(ledPin, HIGH); //Flash D7 LED on Photon if SendtoWait Failed
    delay(250);
    digitalWrite(ledPin, LOW);
    delay(250);
    digitalWrite(ledPin, HIGH);
    delay(250);
    digitalWrite(ledPin, LOW);
    delay(250);
    digitalWrite(ledPin, HIGH);
    delay(250);
    digitalWrite(ledPin, LOW);
    delay(250);
    digitalWrite(ledPin, HIGH);
    delay(250);
    digitalWrite(ledPin, LOW);
    }

  delay(5000);
}

Code for the Adafruit 32u4 Feather + LoRa RF95 915mhz model -

// rf95_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_RF95 driver to control a RF95 radio.
// It is designed to work with the other example rf95_reliable_datagram_client
// Tested with Anarduino MiniWirelessLoRa, Rocket Scream Mini Ultra Pro with the RFM95W 

#include <RHReliableDatagram.h>
#include <RH_RF95.h>
#include <SPI.h>

/* for feather32u4 */
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 7

#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
//RH_RF95 driver(5, 2); // Rocket Scream Mini Ultra Pro with the RFM95W

#define RF95_FREQ 915.0

// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(rf95, SERVER_ADDRESS);

// Blinky on receipt
#define LED 13

void setup() 
{
  pinMode(LED, OUTPUT);
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);

  Serial.begin(9600);
  delay(100);
  //while (!Serial) ; // Wait for serial port to be available

  Serial.println("Feather LoRa TX Test!");
 // manual reset
  digitalWrite(RFM95_RST, LOW);
  delay(10);
  digitalWrite(RFM95_RST, HIGH);
  delay(10);
  

  if (!manager.init())
    Serial.println("init failed"); 
   rf95.setTxPower(10, false);
   rf95.setFrequency(915.0);
  Serial.println("..this is the Adafuit Feather LoRa as server +RFM95 @915mhz");
  
  // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
    
  // The default transmitter power is 13dBm, using PA_BOOST.
  // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then 
  // you can set transmitter powers from 5 to 23 dBm:
   //driver.setTxPower(23, false);
  
  // You can optionally require this module to wait until Channel Activity
  // Detection shows no activity on the channel before transmitting by setting
  // the CAD timeout to non-zero:
  // driver.setCADTimeout(10000);
}

uint8_t data[] = "And hello back to you";
// Dont put this on the stack:
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];


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);
      Serial.print("RSSI: ");
      Serial.println(rf95.lastRssi(), DEC);
      Serial.println();
      digitalWrite(LED, HIGH); //Flash LED 2 times to indicate received message. 
      delay(250);
      digitalWrite(LED, LOW);
      delay(250);
      digitalWrite(LED, HIGH);
      delay(250);
      digitalWrite(LED, LOW);
      delay(250);
      
      // Send a reply back to the originator client
      if (!manager.sendtoWait(data, sizeof(data), from))
        Serial.println("sendtoWait failed");
        
        
    }
  }
}


2 Likes