Code:
#include <NMEAGPS.h>
NMEAGPS gps;
gps_fix fix;
#define gpsPort Serial1
//*********************************************************************************************
// *********** IMPORTANT SETTINGS - YOU MUST CHANGE/ONFIGURE TO FIT YOUR HARDWARE *************
//*********************************************************************************************
#define NETWORKID 100 //the same on all nodes that talk to each other
#define NODEID 1
//Match frequency to the hardware version of the radio on your Feather
#define FREQUENCY RF69_915MHZ
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HCW true // set to 'true' if you are using an RFM69HCW module
//*********************************************************************************************
#define SERIAL_BAUD 9600
// for Feather M0
#define RFM69_CS 10
#define RFM69_IRQ 14
#define RFM69_IRQN 14
#define RFM69_RST 15
#include <RFM69.h> //get it here: https://www.github.com/lowpowerlab/rfm69
RFM69 radio(RFM69_CS, RFM69_IRQ, IS_RFM69HCW, RFM69_IRQN);
//____________________________________________________________________
#include <SPI.h>
#include <SD.h>
Sd2Card card;
SdVolume volume;
SdFile root;
File myFile;
const int chipSelect = BUILTIN_SDCARD;
char charNam[10]="SD100.txt";
unsigned long lastCloseTime;
//____________________________________________________________________
void setup() {
Serial.begin(SERIAL_BAUD);
Serial.println("Feather RFM69HCW Receiver");
// Hard Reset the RFM module
pinMode(RFM69_RST, OUTPUT);
digitalWrite(RFM69_RST, HIGH);
delay(100);
digitalWrite(RFM69_RST, LOW);
delay(100);
// Initialize radio
if (radio.initialize(FREQUENCY, NODEID, NETWORKID)) {
Serial.print("radio initialized ");
}
if (IS_RFM69HCW) {
radio.setHighPower(); // Only for RFM69HCW & HW!
}
radio.setPowerLevel(31); // power output ranges from 0 (5dBm) to 31 (20dBm)
radio.encrypt(ENCRYPTKEY);
Serial.print("\nListening at ");
Serial.print(FREQUENCY == RF69_433MHZ ? 433 : FREQUENCY == RF69_868MHZ ? 868 : 915);
Serial.println(" MHz");
// Initialize SD
pinMode(8, OUTPUT); pinMode(10, OUTPUT);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
int tim=micros();
byte SDx= tim%(tim/1000);
Serial.println(tim);
Serial.println(SDx);
randomSeed(SDx);
byte randNumber = random('A','U');
Serial.println(randNumber);
charNam[2]=randNumber;
charNam[4]='A';
myFile = SD.open(charNam, FILE_WRITE);
// Initialize GPS
gpsPort.begin(9600);//Base baud rate so we can talk to gps
Serial.println("start gps");
gps.send_P( &gpsPort, F("PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0") ); // RMC only
gps.send_P( &gpsPort, F("PMTK220,100") ); // Update frequency 10Hz
gps.send_P( &gpsPort, F("PGCMD,33,0") ); // No antenna status updates
gps.send_P( &gpsPort, F("PMTK251,57600") ); // baud rate
//Can go to adafruit_gps.h to see these settings
delay(1000); // wait for baud rate setting to take effect
gpsPort.end();delay(100);//Terminate gpsPort then open at the set baud rate
gpsPort.begin(57600);
Serial.println("gps restarted");
lastCloseTime = millis();
}
//____________________________________________________________________
void loop()
{
// Serial.print(millis());
// Check for GPS characters
if (gps.available( gpsPort )) {
// A new fix is available, update the current fix structure
fix = gps.read();
// Save some of the new fix on the SD card
if (fix.valid.location) {
myFile.print ( "Loc=" );
myFile.print ( fix.latitude(), 6 );
myFile.print ( ',' );
myFile.println( fix.longitude(), 6 );
}
}
// Check if something was received (could be an interrupt from the radio)
if (radio.receiveDone()) {
Serial.print(millis()); Serial.print(",");
Serial.print(radio.RSSI);Serial.print(",");
Serial.print(radio.SENDERID);
for(byte k1=0; k1<6; k1++){
Serial.print(",");
Serial.print(radio.DATA[k1]);
Serial.print(radio.DATA[k1]);
}
Serial.println();
myFile.println(".");
}
// Check if it's time to create a new file
if (millis() - lastCloseTime > 180000) { // 3 minutes
lastCloseTime = millis();
myFile.close();
Serial.println("file closed");
charNam[4]++;
if (charNam[4]=='Z') {
charNam[4]='A';
charNam[2]++;
}
Serial.println(charNam);
myFile = SD.open(charNam, FILE_WRITE);
}
}
If you want to try it, NeoGPS is available from the link above, or from the IDE Library Manager, under the menu