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
