Last active
May 5, 2020 21:47
-
-
Save bprobbins/e602fec2720b7c211370a143873e6ab8 to your computer and use it in GitHub Desktop.
Photon Lora receiver utilizing //https://github.com/sandeepmistry/arduino-LoRa
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 <Particle.h> | |
#include <LoRa.h> //https://github.com/sandeepmistry/arduino-LoRa | |
/* | |
#define RFM95_CS A2 | |
#define RFM95_RST D4 | |
#define RFM95_INT D2 | |
*/ | |
#define RF95_FREQ 915.0 | |
SYSTEM_THREAD(ENABLED); | |
const int csPin = A2; // LoRa radio chip select | |
const int resetPin = D4; // LoRa radio reset | |
const int irqPin = D2; // change for your board; must be a hardware interrupt pin | |
char IPasStr[16]; | |
// below makes source filename accesible programmatically | |
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) | |
char gDeviceInfo[240]; | |
//for "soft delays" | |
unsigned long lastTime = 0; | |
unsigned long lastTime2 = 0; | |
//for resyncing Time | |
#define ONE_DAY_MILLIS (24 * 60 * 60 * 1000) | |
volatile int lastsyncTime = 0; | |
volatile int lastResetTime = 0; | |
int sysverInt = 0; | |
int FlashVersion = 200503; | |
int i; | |
bool justBooted = true; | |
IPAddress localIP; | |
//USAGE: bool daylightSavings = IsDST(Time.day(), Time.month(), Time.weekday()); | |
// Time.zone(daylightSavings? -7 : -8); //-7 in summer GMT-7 DST; -8 in winter GMT-8 and is PST not DST | |
bool IsDST(int dayOfMonth, int month, int dayOfWeek) { | |
// from @BulldogLowell on Particle forum | |
if (month < 3 || month > 11) { | |
return false; | |
} | |
if (month > 3 && month < 11) { | |
return true; | |
} | |
int previousSunday = dayOfMonth - (dayOfWeek - 1); | |
if (month == 3) { | |
return previousSunday >= 8; | |
} | |
return previousSunday <= 0; | |
}//bool IsDST(int dayOfMonth, int month, int dayOfWeek) | |
void setup() { | |
Particle.variable("deviceInfo",gDeviceInfo); | |
lastTime = millis(); | |
while (millis()-lastTime < 500) {Particle.process();} | |
bool daylightSavings = IsDST(Time.day(), Time.month(), Time.weekday()); | |
Time.zone(daylightSavings? -7 : -8); //-7 in summer GMT-7 DST; -8 in winter GMT-8 not DST | |
lastTime = millis(); | |
while (millis()-lastTime < 500) {Particle.process();} | |
lastResetTime = Time.now(); | |
lastsyncTime = Time.now(); | |
Serial.begin(9600); | |
Serial.println("LoRa Receiver"); | |
// override the default CS, reset, and IRQ pins (optional) | |
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin | |
if (!LoRa.begin(915E6)) { | |
Serial.println("Starting LoRa failed!"); | |
while (1); | |
} | |
LoRa.setSignalBandwidth(125000); | |
LoRa.setSpreadingFactor(12); | |
LoRa.setCodingRate4(5); | |
LoRa.setPreambleLength(8); | |
LoRa.enableCrc(); | |
LoRa.setFrequency(915E6); | |
LoRa.setTxPower(20, false); | |
}//setup | |
void loop() { | |
unsigned long nowforSynch = millis(); //update for syncing Time daily | |
if ((nowforSynch - lastTime2) >= ONE_DAY_MILLIS) { | |
if (Particle.connected()) { | |
// Request daily time synchronization from the Particle Cloud | |
lastTime2 = nowforSynch; | |
Particle.syncTime(); | |
//??wait 1 second for time syncing | |
lastTime = millis(); | |
while ((Particle.syncTimePending()) || (millis()-lastTime < 1000)) { Particle.process();} | |
lastsyncTime = Time.now(); | |
lastTime = millis(); | |
while (millis()-lastTime < 500) {Particle.process();} | |
bool daylightSavings = IsDST(Time.day(), Time.month(), Time.weekday()); | |
Time.zone(daylightSavings? -7 : -8); //-7 in summer GMT-7 DST; -8 in winter GMT-8 not DST | |
}//if ((Particle.connected()) | |
}//if ((nowforSynch - lastTime2) >= ONE_DAY_MILLIS) | |
if (justBooted) { | |
if (WiFi.ready() ) { | |
justBooted = false; | |
localIP = WiFi.localIP(); | |
uint8_t myFirstAddrByte = localIP[0]; | |
uint8_t mySecondAddrByte = localIP[1]; | |
uint8_t myThirdAddrByte = localIP[2]; | |
uint8_t myLastAddrByte = localIP[3]; | |
snprintf(IPasStr,sizeof(IPasStr), "%i.%i.%i.%i", | |
myFirstAddrByte,mySecondAddrByte,myThirdAddrByte,myLastAddrByte); | |
snprintf(gDeviceInfo, sizeof(gDeviceInfo) | |
,"{\"Application\":\"%s\",\"Time\":\"%s\",\"System_firmware\":\"%s\",\"SSID\":\"%s\",\"IP\":\"%s\",\"RSSI\":%i,\"version\":%i}" | |
,__FILENAME__ | |
,(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 | |
,(int8_t) WiFi.RSSI() | |
,FlashVersion | |
); | |
} | |
}//if (justBooted) | |
//try to parse and print packet | |
int packetSize = LoRa.parsePacket(); | |
if (packetSize) { | |
Serial.print((const char*)Time.timeStr()); | |
Serial.print(","); | |
// read packet | |
while (LoRa.available()) { | |
Serial.print((char)LoRa.read()); | |
} | |
// print RSSI of packet | |
Serial.print(","); | |
Serial.println(LoRa.packetRssi()); | |
}//if (packetSize) | |
}//loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment