horace:
do the basic Rx and Tx example programs using Radiohead work OK?
RFM9X Test | Adafruit RFM69HCW and RFM9X LoRa Packet Radio Breakouts | Adafruit Learning System
I have used code from this library that worked. Arduino-Profile-Examples/libraries/Dragino at master · dragino/Arduino-Profile-Examples · GitHub
The default RadioHead samples for RF95 did not work.
The two codes were in the Arduino-Profile-Examples/libraries/Dragino/examples/LoRa/LoRa_Simple_Client_Arduino/
and
Arduino-Profile-Examples/libraries/Dragino/examples/LoRa/LoRa_Simple_Server_Arduino/
directories.
-dev:
You can't use delay
like that. While the Arduino is stuck at the delay
statement, GPS data continues to arrive. It will eventually overflow the input buffer, and you will not get any fixes.
The NeoGPS examples show the correct program structure. Your task, SendValues
, is always dependent on new GPS data, so your sketch must constantly check gps.available()
until it is true. This is just like waiting for Serial.available()
before calling Serial.read()
to get one character.
I would suggest a loop structure like this:
// rf95_payload client
// Sends data to the Base
// LoRa Simple Server for Arduino :
// Support Devices: LoRa Shield/GPS + Arduino
#include <GPSport.h>
#include <NMEAGPS.h>
NMEAGPS gps;
NeoGPS::Location_t base( -253448688L, 1310324914L ); // Ayers Rock, AU
#include <RHReliableDatagram.h>
#include <RH_RF95.h>
#include <SPI.h>
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
// Singleton instance of the radio driver
RH_RF95 rf95;
// Class to manage message delivery and receipt
RHReliableDatagram manager(rf95, CLIENT_ADDRESS);
float frequency = 915.0; // Change the frequency here.
struct dataStruct {
float dist;
unsigned long counter;
} SensorReadings;
// RF communication, Dont put this on the stack:
byte buf[sizeof(SensorReadings)] = {0};
void setup()
{
DEBUG_PORT.begin(115200);
DEBUG_PORT.println( F("LoRa Distance Payload") ); // F macro saves RAM
if (!manager.init())
DEBUG_PORT.println("LoRa init failed");
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
rf95.setFrequency(frequency);
rf95.setTxPower(13, false);
rf95.setCADTimeout(10000);
DEBUG_PORT.println( F("Waiting for radio to setup") );
delay(1000);
DEBUG_PORT.println( F("Setup completed") );
delay(1000);
DEBUG_PORT.println( F("Looking for GPS device on " GPS_PORT_NAME) );
gpsPort.begin(9600);
// SensorReadings.dist = 0;
SensorReadings.counter = 0;
} // Setup
void loop()
{
// Check for GPS characters to parse. A new fix may become available.
if (gps.available( gpsPort )) {
// A new fix structure is ready. This happens once per second.
gps_fix fix = gps.read(); // save the latest
// When we have a location, calculate how far away we are from the base location.
if (fix.valid.location) {
float range = fix.location.DistanceMiles( base );
DEBUG_PORT.print( F("Range: ") );
DEBUG_PORT.print( range );
DEBUG_PORT.println( F(" Miles") );
SensorReadings.dist = range;
SendValues();
}
else
{
DEBUG_PORT.println( F("No location.") );
}
}
// // 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, 2000, &from))
// {
// DEBUG_PORT.print("got reply from : 0x");
// DEBUG_PORT.print(from, HEX);
// DEBUG_PORT.print(": ");
// DEBUG_PORT.println((char*)buf);
// DEBUG_PORT.print("RSSI: ");
// DEBUG_PORT.println(rf95.lastRssi(), DEC);
// }
// else
// {
// DEBUG_PORT.println("No reply, is base running?");
// }
// }
// else
// DEBUG_PORT.println("sendtoWait failed - this is payload");
// delay(500);
} // loop
//RF communication
void SendValues()
{
//Load message into data-array
byte zize=sizeof(SensorReadings);
memcpy (buf, &SensorReadings, zize);
DEBUG_PORT.println( F("Sending to Base") );
// Send a message to manager_server
if (manager.sendto(buf, zize, SERVER_ADDRESS))
{
DEBUG_PORT.println( F("Message sent") );
}
else
{
DEBUG_PORT.println( F("sendto failed") );
}
SensorReadings.counter = SensorReadings.counter + 1;
}
This is not good coding practice:
distance(); //Read temp sensor
There is no temperature sensor, so this is very confusing.
I apologize for the confusing comments. I have copy pasted from several files trying to learn. Thank you helping out with delay information. I will take some time to look at the code you provided.