Author Topic: RFGateway M4 issue with RadioHead Library  (Read 3564 times)

hockeyprogrammer

  • NewMember
  • *
  • Posts: 8
RFGateway M4 issue with RadioHead Library
« on: May 18, 2021, 08:35:45 AM »
I purchased a RFGateway M4 with a RFM95 lora chip a few weeks back and finally got around to testing it out yesterday. When I went in to add the RadioHead library that Felix had updated with the default constructors the code just seems to fail on init. I tracked it down to the spiWrite method in the library not setting the lora chip to sleep. I did some testing and the constructor seems to be setting the right values, but no matter what I do I can not seem to get the gateway to connect. Has anyone encountered this before or have sample working code?

Also, I have followed all the steps to install moteino core and the correct boards. I have a lora moteino that is already working as a client with the RadioHead library.

Code: [Select]
// 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>

// Singleton instance of the radio driver
RH_RF95 rf95;
//RH_RF95 rf95(5, 2); // Rocket Scream Mini Ultra Pro with the RFM95W
//RH_RF95 rf95(8, 3); // Adafruit Feather M0 with RFM95

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

int led = 9;

void setup()
{
  // Rocket Scream Mini Ultra Pro with the RFM95W only:
  // Ensure serial flash is not interfering with radio communication on SPI bus
//  pinMode(4, OUTPUT);
//  digitalWrite(4, HIGH);

  pinMode(led, OUTPUT);     
  Serial.begin(9600);
  while (!Serial) ; // Wait for serial port to be available
  if (!rf95.init())
    Serial.println("init failed");
  else
    Serial.println("init");
  // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on

  // You can change the modulation parameters with eg
  // rf95.setModemConfig(RH_RF95::Bw500Cr45Sf128);

// 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 2 to 20 dBm:
//  driver.setTxPower(20, false);
  // If you are using Modtronix inAir4 or inAir9, or any other module which uses the
  // transmitter RFO pins and not the PA_BOOST pins
  // then you can configure the power transmitter power for 0 to 15 dBm and with useRFO true.
  // Failure to do that will result in extremely low transmit powers.
//  driver.setTxPower(14, true);
}

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";
      rf95.send(data, sizeof(data));
      rf95.waitPacketSent();
      Serial.println("Sent a reply");
       digitalWrite(led, LOW);
    }
    else
    {
      Serial.println("recv failed");
    }
  }
}

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6867
  • Country: us
    • LowPowerLab
Re: RFGateway M4 issue with RadioHead Library
« Reply #1 on: May 18, 2021, 12:59:01 PM »
Are you using the patched version linked at the Moteino LoRa page?

That latest library is currently v 1.116: lowpowerlab.com/RadioHead-1.116_LPLpatched.zip

It contains the changes required for RFGateway M4 to work, and it has examples that were tested on this and all Moteinos that operate with the offered LoRa transceivers.

hockeyprogrammer

  • NewMember
  • *
  • Posts: 8
Re: RFGateway M4 issue with RadioHead Library
« Reply #2 on: May 18, 2021, 02:02:07 PM »
Yes, I downloaded right from the link provided on that page and I know these are all tested before they leave so I assume I'm missing something. I used the example from this path RadioHead\examples\rf95\rf95_server.

hockeyprogrammer

  • NewMember
  • *
  • Posts: 8
Re: RFGateway M4 issue with RadioHead Library
« Reply #3 on: May 18, 2021, 02:07:39 PM »
More specifically these are the lines failing in the RadioHead Library

// Set sleep mode, so we can also set LORA mode:
    spiWrite(RH_RF95_REG_01_OP_MODE, RH_RF95_MODE_SLEEP | RH_RF95_LONG_RANGE_MODE);
    delay(1000); // Wait for sleep mode to take over from say, CAD
    // Check we are in sleep mode, with LORA set
    if (spiRead(RH_RF95_REG_01_OP_MODE) != (RH_RF95_MODE_SLEEP | RH_RF95_LONG_RANGE_MODE))

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6867
  • Country: us
    • LowPowerLab
Re: RFGateway M4 issue with RadioHead Library
« Reply #4 on: May 18, 2021, 04:02:35 PM »
Not sure what I'm looking at ???

delay(1000)?

Where is that in the library? There's a delay(10) in RH_RF95.h.

You should have no reason to touch the library, not even sure why you'd say that something is failing in the library.
And yes the boards are tested with the client sketch against the server sketch loaded on a receiver to ensure they communicate.

I suggest you reload the library from the link above and start fresh with the client/server sketches.

hockeyprogrammer

  • NewMember
  • *
  • Posts: 8
Re: RFGateway M4 issue with RadioHead Library
« Reply #5 on: May 18, 2021, 04:16:20 PM »
Thank you very much for the reply. I have tried to reload the libraries, but in the init of the example the serial prints out init failed.

The code I pasted in was where I managed to find in the RadioHead library where it was returning false in the init function which is on line 74.

Yes that delay was me playing around with the code to make sure I had set everything up correctly.

Am I missing anything from the example? Its a direct copy and paste from the folder.

Code: [Select]
#include <SPI.h>
#include <RH_RF95.h>

// Singleton instance of the radio driver
RH_RF95 rf95;
//RH_RF95 rf95(5, 2); // Rocket Scream Mini Ultra Pro with the RFM95W
//RH_RF95 rf95(8, 3); // Adafruit Feather M0 with RFM95

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

int led = 9;

void setup()
{
  // Rocket Scream Mini Ultra Pro with the RFM95W only:
  // Ensure serial flash is not interfering with radio communication on SPI bus
//  pinMode(4, OUTPUT);
//  digitalWrite(4, HIGH);

  //pinMode(led, OUTPUT);     
  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

  // You can change the modulation parameters with eg
  // rf95.setModemConfig(RH_RF95::Bw500Cr45Sf128);

// 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 2 to 20 dBm:
//  driver.setTxPower(20, false);
  // If you are using Modtronix inAir4 or inAir9, or any other module which uses the
  // transmitter RFO pins and not the PA_BOOST pins
  // then you can configure the power transmitter power for 0 to 15 dBm and with useRFO true.
  // Failure to do that will result in extremely low transmit powers.
//  driver.setTxPower(14, true);
}
« Last Edit: May 18, 2021, 07:04:34 PM by hockeyprogrammer »

hockeyprogrammer

  • NewMember
  • *
  • Posts: 8
Re: RFGateway M4 issue with RadioHead Library
« Reply #6 on: May 19, 2021, 07:36:37 PM »
I started completely over. Removed the libraries and reinstalled them/downloaded them, verified that I was using the updated library from Felix and am still getting init failed. Has anyone been able to get the RFGateway M4 working with Lora and the RadioHead library? If so can you post the server code?

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6867
  • Country: us
    • LowPowerLab
Re: RFGateway M4 issue with RadioHead Library
« Reply #7 on: May 20, 2021, 11:33:41 AM »
What happens if you declare the radio object with explicit CS and IRQ pins as below?

Code: [Select]
RH_RF95 rf95(20, 18);

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6867
  • Country: us
    • LowPowerLab
Re: RFGateway M4 issue with RadioHead Library
« Reply #8 on: May 20, 2021, 01:36:08 PM »
OK I tracked down what's really going on. Radiohead.h has this piece of code:

Code: [Select]
#if (RH_PLATFORM != RH_PLATFORM_ESP32)
  #ifndef SS
    #define SS 10
  #endif
#endif

Unless you specifically call the constructor with numeric SS/IRQ pins, the SS will get redefined to value 10. That is fine if the hardware SS is indeed 10 but not otherwise.
This is because on SAMD variants the SS is a numeric variable and not a #define. For my internal testing I was using a modified example where I was conditionally and explicitly passing the SS/IRQ to the constructor based on the board (overriding whatever was in the library), and hence the explanation why my test sketch worked on the RFGatewayM4, but not the stock example.

The simple solution to this, without further modifying the RadioHead library to remove that #ifndef SS check, is to either:

1) call the constructor with specific values (this is always the best insurance if somehow the variant you're using doesn't use #define SS):

Code: [Select]
RH_RF95 rf95(20, 18);

2) Update to the latest 1.6.3 Moteino SAMD package which I just patched to add the "#define SS SS" so the "#ifndef SS" in RadioHead.h won't fail. You can do this easily in the BoardManager.

hockeyprogrammer

  • NewMember
  • *
  • Posts: 8
Re: RFGateway M4 issue with RadioHead Library
« Reply #9 on: May 20, 2021, 07:58:00 PM »
That did it thank you very much I really appreciate it and the quick responses.