here we go @RWB this sample code if very far from be perfect and efficient … but it works.
Particle photon server side.
I am reading once again your previous request and I am not sure if the below what you really need…
//This is the server running on PArticle Photon
#include <RHMesh.h>
#include <RH_RF95.h>
#define SERVER_ADDRESS 1
char publishString[100];
RH_RF95 driver;//(10,2); //moteino mega
//RHReliableDatagram manager(driver, CLIENT_ADDRESS);
RHMesh manager(driver, SERVER_ADDRESS);
#define DELAY_MS 40
#define Doutput7 7 //D7 on Photon
int ledState = HIGH;
//put here coordinates of your home address
const float Mylatitude = 29.53443 ; //fake
const float Mylongitude =-81.5555 ;
//data struct that hold the data from the cliente via Rfm96
struct Gpsdata{
float latitude; // I had problem with ESP8266 declaring Lat and long as float, So I have to use DOUBLE.
float longitude;
//int data;
};
Gpsdata MyGpsdata;
// Global variables
char sendBuf[256];
bool InputTrigger;
uint8_t*GpsPayload;
//int gps_on(String command);
void setup() {
pinMode(Doutput7,OUTPUT); //blue led
digitalWrite(Doutput7,HIGH);
delay(DELAY_MS);
digitalWrite(Doutput7,LOW);
delay(DELAY_MS);
digitalWrite(Doutput7,HIGH);
delay(DELAY_MS);
digitalWrite(Doutput7,LOW);
delay(DELAY_MS);
Serial.begin(9600);
// while (!Serial) {
// ; // wait for serial port to connect. Needed for Leonardo only
// }
///delay(10000);
if (!manager.init())
Particle.publish("init Failed!");
Serial.println("init failed");
Serial.println("GPS_TRACKER_SERVER");
Particle.publish("GPS_TRACKER_SERVER");
driver.setTxPower(10, false);
driver.setFrequency(915.0); //Set Freuency rfm96 915
GpsPayload =(uint8_t*)(&MyGpsdata);
}
void loop()
{
if (manager.available())
{
uint8_t len = sizeof( MyGpsdata);
uint8_t from;
if (manager.recvfromAckTimeout(GpsPayload, &len, 3000, &from))
{
float distances= calc_dist( Mylatitude, Mylongitude, MyGpsdata.latitude, MyGpsdata.longitude);
int rssi= driver.lastRssi();
sprintf(publishString, "{\"From_mobile\":%d, \"Latitude\":%.6f, \"Longitude\":%.6f, \"RSSI\":%d, \"Dist\":%.2f}",from, MyGpsdata.latitude, MyGpsdata.longitude, rssi, distances );
Spark.publish("GPSDATA",publishString);
Serial.println(F("Latitude, Longitude, rssi, from, Distance"));
Serial.print(MyGpsdata.latitude,6);
Serial.print(", ");
//Particle.publish("Longitude", String(MyGpsdata.longitude,6));
Serial.print(MyGpsdata.longitude,6);
Serial.print(", ");
Serial.print(driver.lastRssi(), DEC);
Serial.print(", ");
Serial.print(from);
Serial.print(", ");
Serial.print(distances,2);
Serial.println();
digitalWrite(Doutput7,HIGH);
delay(DELAY_MS);
digitalWrite(Doutput7,LOW);
}
}
}
/*************************************************************************
* //Function to calculate the distance between two waypoints
*************************************************************************/
float calc_dist(float flat1, float flon1, float flat2, float flon2)
{
float dist_calc=0;
float dist_calc2=0;
float diflat=0;
float diflon=0;
//I've to spplit all the calculation in several steps. If i try to do it in a single line the arduino will explode.
diflat=radians(flat2-flat1);
flat1=radians(flat1);
flat2=radians(flat2);
diflon=radians((flon2)-(flon1));
dist_calc = (sin(diflat/2.0)*sin(diflat/2.0));
dist_calc2= cos(flat1);
dist_calc2*=cos(flat2);
dist_calc2*=sin(diflon/2.0);
dist_calc2*=sin(diflon/2.0);
dist_calc +=dist_calc2;
dist_calc=(2*atan2(sqrt(dist_calc),sqrt(1.0-dist_calc)));
dist_calc*=6371000.0; //Converting to meters
//Serial.println(dist_calc);
return dist_calc;
}
============================================
the next is the Client, I use Moteino, so you need to adapt the code & libraries for particle photon.
============================================
//GPS client ID 2
#include <RHMesh.h>
#include <RH_RF95.h>
#include <SPI.h>
#define SERVER_ADDRESS 1
#define CLIENT2_ADDRESS 2
#define CLIENT3_ADDRESS 3
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
static const int RXPin = 3, TXPin = 14;
static const uint32_t GPSBaud = 9600;
// Create a TinyGPS++ object called "gps"
TinyGPSPlus gps;
// Create a software serial port called "gpsSerial"
SoftwareSerial ss(RXPin, TXPin);
RH_RF95 driver; //moteino
RHMesh manager(driver, CLIENT2_ADDRESS);
#define Douput9 9 //
const int SendInterval= 60; //
int SendIntervalCount = 0;
int ledState = HIGH;
int replicar1 =0;
double oldlat1 ;
double oldlong1;
struct Gpsdata
{
float latitude; // I had problem with ESP8266 when defining latitude and longitude as float, I had to change that to DOUBLE
float longitude;
//int otherdata;
};
Gpsdata MyGpsdata;
//bool InputTrigger;
uint8_t*GpsPayload;
unsigned long previousMillis = 0; // will store last time LED was updated
void setup() {
pinMode(Douput9,OUTPUT);
digitalWrite(Douput9,HIGH);
delay(2000);
digitalWrite(Douput9,LOW);
Serial.begin(115200); //com 1 en el arduino
ss.begin(GPSBaud);
if (!manager.init())
Serial.println(F("RFM_96 init failed"));
delay(1000);
Serial.println(F("GPS SENDER-915-CLIENT #2"));
driver.setTxPower(5, false); //5dbm---> 1.5km
driver.setFrequency(915.0); // Set frecuency
GpsPayload = (uint8_t*)(&MyGpsdata);
}
void loop() {
while (ss.available() > 0)
if (gps.encode(ss.read()))
{
SendIntervalCount++;
if(SendIntervalCount > SendInterval) //check gps data based on interval given by counting
{
SendIntervalCount = 0;
if (gps.location.isValid()) //check if GPS location is valid
{
//Gps_sending();
//READ current latitude and longidute
double lat1 =gps.location.lat();
double lon1 =gps.location.lng();
//calculate the distance bewten current pos and previous position
double distance = gps.distanceBetween(lat1,lon1,oldlat1,oldlong1);
Serial.print("Distance: ");
Serial.println(distance,2);
//only goin to send GPS position to server is distance is greater than a value meters that one consider as apropiate
// for my test i set that value =2meters,
if (distance >= 2) {
oldlat1=lat1;
oldlong1=lon1;
Serial.println("Sending data to Server:");
Gps_sending();
}
}
else
{
//no gps fix blue led blink
Serial.println(F("Location not valid"));
// Blink(Doutput2, 20, 4); //blink LED 3 times, 40ms between blinks
}
}
}
}
void Gps_sending(){
//feed the struture with current gps data
MyGpsdata.latitude = gps.location.lat();
MyGpsdata.longitude = gps.location.lng();
// MyGpsdata.data =2345;
unsigned long now1 =millis();
//uint8_t from;
if (manager.sendtoWait(GpsPayload, sizeof(MyGpsdata), SERVER_ADDRESS) == RH_ROUTER_ERROR_NONE)
{
unsigned long now2=millis();
unsigned long timeflight= now2-now1;
// Serial.println(F("Mensaje sent OK "));
// Serial.print(F("delay:= "));
// Serial.print(timeflight);
// Serial.print(" : Last Rssi: ");
// Serial.println(driver.lastRssi(), DEC);
// Blink(Doutput1, 20, 4); //blink LED 3 times, 40ms between blinks
}
else
{
Serial.println(F("Mensaje has failed"));
// Serial.print(F("Latitude: "));
// Blink(Doutput3, 30, 4); //blink LED 3 times, 40ms between blinks
}
}
void Blink(byte PIN, byte DELAY_MS, byte loops)
{
for (byte i=0; i<loops; i++)
{
digitalWrite(PIN,LOW);
delay(DELAY_MS);
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
}
}