Better way to send GPS coordinates via SMS

Hi all,

I am using a SIM808 with an AtMega328PB

The project that I have been working on has worked flawlessly now that I worked out a couple of kinks. With that said, my GPS code is not as efficient as it should be. As it is set up in the code right now, I have a char gps_string[ 140 ] in order to define the string and then later in the sketch I call fona.sendSMS ("5555555555", gps_string);I know there is a better way to do this, but I don't know what that better way is. I also need to update its location every so often and I can't figure out where I can implement that into my code either.

Here is the code that I have at the moment:

//buttons and lights
const byte wirePin = 5; //external pulldown resistor to take it low when the wire breaks
const byte cancelPin = 6; //wired from pin to ground, uses internal pullup
const byte wireGoodLed = 8;
const byte wireBrokenLed = 7;
const byte alertSentLed = 13;
const byte recordingPin = 9;

//millis stuff
unsigned long wireWasBrokenAt;
long timeOutInterval = 5000; //short value for testing
bool weHaveCancelled = false;
bool alertSent = false;

//GSM stuff
#include "Adafruit_FONA.h"

#define FONA_RX 3
#define FONA_TX 2
#define FONA_RST 4

#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

//strings
char gps_string[ 140 ];

void setup()
{
  Serial.begin(115200);
  Serial.println("setup() started");
  
  fonaSerial->begin(115200);
  fona.begin(*fonaSerial);
  fona.enableGPS(true);
  fona.getGPS( 0, gps_string, 140 );
  
  pinMode(wirePin, INPUT); // you said this is held high by the wire and has a pull down?
  pinMode(cancelPin, INPUT_PULLUP); //easier for me to wire this from pin to ground, so we look for a low when pressed
  pinMode(wireGoodLed, OUTPUT);
  pinMode(wireBrokenLed, OUTPUT);
  pinMode(alertSentLed, OUTPUT);
  pinMode(recordingPin, OUTPUT);

  Serial.println("The wire is good, we are sitting in setup()");
  digitalWrite(wireGoodLed, HIGH);
  while (digitalRead(wirePin) == HIGH) {} //it sticks here until the wire breaks and resistor pulls pin low
  
  Serial.print("The wire was broken at ");
  wireWasBrokenAt = millis();
  Serial.print(wireWasBrokenAt);
  
  Serial.println(", we are leaving setup()and heading to loop()");
  delay(500); //just to see message
  digitalWrite(wireGoodLed, LOW);
  digitalWrite(wireBrokenLed, HIGH);

}

void loop(){
  fona.getGPS(0, gps_string, 140);
  Serial.print(gps_string);
    
    if (!weHaveCancelled && millis() - wireWasBrokenAt < timeOutInterval) //haven't cancelled, and still in time
  {
      Serial.println("Waiting for cancel or timeout");
    if (!digitalRead(cancelPin)) // ! means not, so this is looking for a low, ie a press
    {
        Serial.println("Cancel was pressed");
        delay(500); //just to see message
        weHaveCancelled = true;
    }
  }

  if (!weHaveCancelled && !alertSent && millis() - wireWasBrokenAt >= timeOutInterval) //haven't cancelled, out of time, and not alrted before
  {
    digitalWrite(alertSentLed, HIGH);
    delay(2500);
    fona.sendSMS("5555555555", "This is John Doe and I have fallen from my treestand. Emergency services have been contacted. Here are my current coordinates");
    delay(2500);
    fona.sendSMS("5555555555", gps_string);
    delay(2500);
    fona.callPhone("5555555555");
    delay(10000);
    digitalWrite(recordingPin, HIGH);
    delay(15000);
    fona.hangUp();
    
    alertSent = true;
    Serial.print("Time up: alert sent at ");
    Serial.print(millis());
    delay(500); //just to see message
  }

  if (weHaveCancelled || alertSent) Serial.println("To prove the code didn't block");
 
}//loop

Any help at all would be greatly appreciated!

Thank you,
Joe M

Does the code at the beginning of loop update your GPS?

void loop(){
  fona.getGPS(0, gps_string, 140);
  Serial.print(gps_string);
  // ...

How is this not efficient? You have one buffer (gps_string) that you use to fetch your GPS and then pass that to the SMS?

It does not. The code only moves down to the loop if the wirePin goes low. It also isn't as efficient as I would like it to be because the way it is formatted when it texts my phone it just sends as a string of numbers...which I can decipher anyways, but would like a better interface. I would, in reality, like to see something like this:

Lat: 12.345
Long: 12.345
Time: 12:00 2/14/2020

I'll attach of image of what it is sending me below.

Maybe look at the GPS example that comes with the library. It prints out all sorts of useful GPS information
link