Last active
May 4, 2020 16:56
-
-
Save bprobbins/fca8bb5d123e9e3e844ab761c1f4bf82 to your computer and use it in GitHub Desktop.
Particle photon reliable datagram server for gps client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <RF9X-RK.h> //https://github.com/rickkas7/RF9X-RK | |
#include <RHReliableDatagram.h> | |
#include <RH_RF95.h> | |
#include <SPI.h> | |
#define CLIENT_ADDRESS 1 | |
#define SERVER_ADDRESS 2 | |
#define RFM95_CS A2 | |
#define RFM95_RST D4 | |
#define RFM95_INT D2 | |
#define RF95_FREQ 915.0 | |
SYSTEM_THREAD(ENABLED); | |
RH_RF95 rf95 (A2, D2); | |
/* | |
Lora feather - photon | |
CS - A2 | |
IRQ - D2 | |
RST - NC | |
MISO - A4 | |
MOSI - A5 | |
SCK - A3 | |
On the rf95 Lora Featherwing: | |
Solder wires between: | |
IRQ & E (note: On the rfm95 module, DIO0 is also known as IRQ) | |
CS & A | |
RST & F but this one is unnecessary | |
Note that on the rf95 featherwing, (all feathers??) | |
MISO is the fourth pin from the ANT | |
MOSI is the Fifth from the ANT | |
SCK is the sixth from the ANT | |
*/ | |
RHReliableDatagram manager(rf95, SERVER_ADDRESS); | |
char data[RH_RF95_MAX_MESSAGE_LEN]; | |
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; | |
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) | |
char gDeviceInfo[240]; | |
int sysverInt = 0; | |
int FlashVersion = 1; | |
IPAddress myAddress(192,168,1,201); | |
IPAddress netmask(255,255,255,0); | |
IPAddress gateway(192,168,1,1); | |
IPAddress dns(192,168,1,1); | |
char IPasStr[16]; | |
//Particle cloud variables | |
double curLAT = 0.0; | |
double curLON = 0.0; | |
double curALT = 0.0; | |
int led = 7; | |
void setup() | |
{ | |
pinMode(RFM95_RST, OUTPUT); | |
digitalWrite(RFM95_RST, HIGH); | |
// IPAddress localIP = WiFi.localIP(); | |
Particle.variable("deviceInfo",gDeviceInfo); | |
Particle.variable("LAT", curLAT); | |
Particle.variable("LON", curLON); | |
Particle.variable("ALT", curALT); | |
uint8_t myFirstAddrByte = myAddress[0]; | |
uint8_t mySecondAddrByte = myAddress[1]; | |
uint8_t myThirdAddrByte = myAddress[2]; | |
uint8_t myLastAddrByte = myAddress[3]; //localIP[3]; | |
//setup Particle variable to show program filename, OS version etc etc | |
snprintf(IPasStr,sizeof(IPasStr), "%i.%i.%i.%i",myFirstAddrByte,mySecondAddrByte,myThirdAddrByte,myLastAddrByte); | |
snprintf(gDeviceInfo, sizeof(gDeviceInfo) | |
,"{\"Application\":\"%s\",\"Date\":\"%s\",\"Time\":\"%s\",\"System_firmware\":\"%s\",\"SSID\":\"%s\",\"IP\":\"%s\",\"RSSI\":%i,\"version\":%i}" | |
,__FILENAME__ | |
,__DATE__ | |
,(const char*)Time.timeStr() | |
,(const char*)System.version() // cast required for String | |
,(const char*)WiFi.SSID() // cast not required but always safe when in doubt if String or char* | |
,IPasStr //shows local ip as variable | |
,(int8_t) WiFi.RSSI() //Serial.printlnf("%d", (int8_t) WiFi.RSSI() ); | |
,FlashVersion | |
); | |
// ENABLE STATIC IP | |
WiFi.setStaticIP(myAddress, netmask, gateway, dns); | |
//use the configured IP | |
WiFi.useStaticIP(); | |
pinMode(led, OUTPUT); | |
Serial.begin(115200); | |
delay(100); | |
// manual reset | |
digitalWrite(RFM95_RST, LOW); | |
delay(10); | |
digitalWrite(RFM95_RST, HIGH); | |
delay(10); | |
while (!manager.init()) { | |
Serial.println("Datagram init failed"); | |
while (1); | |
} | |
Serial.println("Datagram init OK!"); | |
rf95.setSignalBandwidth(250000); | |
rf95.setSpreadingFactor(12); | |
rf95.setCodingRate4(6); | |
rf95.setPreambleLength(8); | |
rf95.setPayloadCRC(1); | |
if (!rf95.setFrequency(RF95_FREQ)) { | |
Serial.println("setFrequency failed"); | |
while (1); | |
} | |
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ); | |
// The default transmitter power is 13dBm, using PA_BOOST. | |
rf95.setTxPower(23, false); | |
manager.setTimeout(10000); | |
manager.setRetries(0); | |
}//setup | |
char buf2[200]; | |
char vBATstr[6]; | |
double vBATf; | |
char LATstr[13]; | |
double LATf; | |
char LONstr[13]; | |
double LONf; | |
char SATSstr[6]; | |
int SATSint; | |
char ALTstr[10]; | |
double ALTf; | |
unsigned long MSGnum; | |
char MSGnumstr[10]; | |
char label1[7],label2[7],label3[7],label4[7],label5[7],label6[7],label7[7]; | |
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)) { | |
if (manager.recvfromAckTimeout(buf, &len, 2000, &from)) { | |
Serial.print("got request from : 0x"); | |
Serial.print(from, HEX); | |
Serial.print(": "); | |
Serial.println((char*)buf); | |
// Send a reply back to client confirming address and data | |
sprintf(data,"ACK 0x%x %s", &from, buf); | |
if (!manager.sendtoWait((uint8_t*)data, sizeof((uint8_t*)data), from)) | |
Serial.println("reply failed"); | |
}//if (manager.recvfromAckTimeout(buf, &len, 2000, &from)) | |
int parsed = sscanf ((char*)buf, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]", | |
label1, vBATstr, label2, LATstr, label3, label4, LONstr, label5, label6, SATSstr, label7, ALTstr, MSGnumstr); | |
Serial.print("parsed = "); Serial.println(parsed); | |
Serial.print("label1 = "); Serial.println(label1); | |
Serial.print("vBATstr = "); Serial.println(vBATstr); | |
Serial.print("LATstr = "); Serial.println(LATstr); | |
Serial.print("LONstr = "); Serial.println(LONstr); | |
Serial.print("SATSstr = "); Serial.println(SATSstr); | |
Serial.print("ALTstr = "); Serial.println(ALTstr); | |
Serial.print("MSGnumstr = "); Serial.println(MSGnumstr); | |
Serial.printlnf("got packet from 0x%02x rssi=%d %s", from, rf95.lastRssi(), (char *)buf); | |
//update variables for possible cloud retrieval | |
curLAT = atof(LATstr); | |
curLON = atof(LONstr); | |
curALT = atof(ALTstr); | |
}//if (manager.available()) | |
}//loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment