Skip to content

Instantly share code, notes, and snippets.

@Dygear Dygear/VecLoc.ino
Created Jun 1, 2019

Embed
What would you like to do?
Vehicle Location using FONA + Ultimate GPS
/**************************************************************************/
/*!
* Vehicle Location Tracking
* Using the Adafruit Feather 32u4 FONA with the Ultimate GPS FeatherWing.
*
* @product Vehicle Location Tracking
* @version 0.1.0
* @copyright 2019 - MimoCAD, Inc.
* @author Mark 'Dygear' Tomlin, CEO MimoCAD, Inc.
******************************************************************************/
/**
* FONA Hardware Setup
*/
#include <Adafruit_FONA.h>
// Define the correct GPIO Pins for RX / TX and Reset
#define FONA_RX 9
#define FONA_TX 8
#define FONA_RST 4
#define FONA_RI 7
// Serial Interface
#include <SoftwareSerial.h>
// Raw Software Serial Interface
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *FONASerial = &fonaSS;
// Define the name of the Software Serial Port
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
// Global Variables
char IMEI[15] = {0};
/**
* GPS Hardware Setup
*/
#include <Adafruit_GPS.h>
// Define the name of the Hardware Serial Port
#define GPSSerial Serial1
// Connect to the GPS on the hardware port
Adafruit_GPS GPS(&GPSSerial);
/**
* @global unit32_t timer;
*/
uint32_t timer = millis();
/**
* @global struct VecLocFrame
*/
struct VecLocFrame {
char IMEI[15]; // FONA Hardware IEMID
struct GPS {
struct Fix {
uint8_t Fix; // TRUE If GPS Has Fix
uint8_t GPS; // TRUE If Fix Type = GPS
uint8_t DGPS; // TRUE If Fix Type = DGPS
uint8_t FONA; // TRUE If Fix Type = FONA
uint8_t Satellites; // Number of Satellites
} Fix;
char DateTime[19]; // YYYYMMDDTHHMMSS.000
char NS; // North / South
float Latitude; // Latitude Decimal Degrees
char EW; // East / West
float Longitude; // Longitude Decimal Degrees
float Knots; // Speed Over Ground In Knots
float Angle; // Course in Degrees from True North
float Altitude; // Altitude in Meters Above MSL
} GPS;
char Padding1;
char Padding2;
char Padding3;
} VecLocFrame;
/**
* Setup Entry Function
*
* @return void
*/
void setup() {
/**
* Serial Interface Initialization
*/
// Wait for the Hardware Serial to Initiate
while (!Serial);
// Set Serial Baud Rate
Serial.begin(115200);
// Set GPS Baud Rate
GPS.begin(9600);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_ALLDATA);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
GPS.sendCommand(PGCMD_ANTENNA);
FONASerial->begin(4800);
if (!fona.begin(*FONASerial)) {
Serial.println(F("Couldn't find FONA"));
while (1);
}
uint8_t imeiLen = fona.getIMEI(IMEI);
if (imeiLen > 0) {
Serial.print("Module IMEI: "); Serial.println(IMEI);
}
fona.setGPRSNetworkSettings(F("wholesale"), F(""), F(""));
fona.setHTTPSRedirect(true);
while(!fona.getNetworkStatus()); // Wait for Network to be available
}
/**
* Main Program Loop
*
* @return void
*/
void loop() {
GPS.read();
// if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA()))
return;
}
// Wrap Around Timer Correction
if (timer > millis())
timer = millis();
// Every 10 seconds print and send out the current status.
static unsigned nextInterval = 10000;
if (millis() - timer > nextInterval) {
timer = millis();
static bool GPRSConnection = false;
static bool TCPConnection = false;
if (GPRSConnection) {
/**
* Create VecLoc Frame and Populate Data
*/
struct VecLocFrame Frame;
strcpy(Frame.IMEI, IMEI);
Frame.GPS.Fix.Satellites = GPS.satellites;
Frame.GPS.Fix.Fix = GPS.fix;
Frame.GPS.Fix.GPS = GPS.fixquality & 1;
Frame.GPS.Fix.DGPS = GPS.fixquality & 2;
Frame.GPS.Fix.FONA = false;
static char DateTime[19];
sprintf(DateTime, "20%02d%02d%02d%02d%02d%02d.%03d", GPS.year, GPS.month, GPS.day, GPS.hour, GPS.minute, GPS.seconds, GPS.milliseconds);
strcpy(Frame.GPS.DateTime, DateTime);
Frame.GPS.NS = GPS.lat;
Frame.GPS.Latitude = GPS.latitude;
Frame.GPS.EW = GPS.lon;
Frame.GPS.Longitude = GPS.longitude;
Frame.GPS.Knots = GPS.speed;
Frame.GPS.Angle = GPS.angle;
Frame.GPS.Altitude = GPS.altitude;
Frame.Padding1 = '\0';
Frame.Padding2 = '\0';
Frame.Padding3 = '\0';
/**
* Convert the VecLoc Frame into Char Array.
*/
int16_t FrameLen = sizeof(Frame);
char FrameBuf[FrameLen] = {0};
memcpy(FrameBuf, &Frame, sizeof(VecLocFrame));
static char TCP_SERVER[] = "your.url";
static uint16_t TCP_PORT = 31337;
if (!TCPConnection) {
// Reconnect if Disconnected
if (TCPConnection = fona.TCPconnect(TCP_SERVER, TCP_PORT)) {
Serial.println("TCP Connection Established");
} else {
Serial.println("TCP Connection Failed");
}
} else {
fona.TCPsend((char *) FrameBuf, FrameLen);
}
} else {
if (GPRSConnection = fona.enableGPRS(true)) {
Serial.println("GPRS Connection Established");
} else {
Serial.println("GPRS Connection Failed");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.