Hi,
I am using the Fona 808 shield on a vehicle to track it. Setup is as follows:
- voltage converter connected to battery of vehicle
- step down power to 4.1V to power both Arduino and Fona.
Most of the time everything works fine and I am able to post GPS co-ordinates to my database. Then randomly, sometimes after 1 day or sometimes after 4-5 days, data will stop posting. When i check the devices, both still have power, but the NET led on the fona flashes to tell me it still has connection with the cellular network. After i restart the devices, everything will resume working as normal again. Has anyone run into this problem? I am finding it difficult to debug this. Is there a value that is returned after posting to my database is completed? I could potentially use a timer to see if this value has been returned, and if, after x amount of seconds it has not been returned, then reboot the device?
Here is my code:
#include "Adafruit_FONA.h"
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
// this is a large buffer for replies
char replybuffer[255];
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
// put your setup code here, to run once:
while (!Serial);
Serial.begin(115200);
fonaSerial->begin(4800);
if (! fona.begin(*fonaSerial)) {
Serial.println(F("Couldn't find FONA"));
while (1);
}
Serial.println(F("FONA is OK"));
fona.setGPRSNetworkSettings(F("internet.keepgo.com"), F(""), F(""));
while(!fona.enableGPS(true)) {
Serial.println(F("GPS not enabled yet..."));
delay(5000);
}
//gps is now enabled. wait for 3D fix.
int8_t stat = 4;
while(stat != 3) {
Serial.println(F("Waiting for 3D fix..."));
delay(5000);
stat = fona.GPSstatus();
if(stat == 3)
Serial.println(F("3D fix found!"));
}
while(!fona.enableGPRS(true)) {
Serial.println(F("GPRS not enabled yet..."));
delay(5000);
}
delay(3000);
}
void loop() { //now the gps is on and has a fix. gprs also enabled.
Serial.println(F("Start of loop()"));
float longitude, latitude, latToPost, speed_kph, heading, speed_mph, altitude, longitude2, latitude2, speed_kph2, heading2, speed_mph2, altitude2;
char imei[16] = {0}; // MUST use a 16 character buffer for IMEI!
uint8_t imeiLen = fona.getIMEI(imei);
Serial.println(F("First GPS call"));
boolean gps_success = fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);
delay(20000); //pause between getting gps locations to see if gps has moved.
Serial.println(F("Second GPS call"));
boolean gps_success2 = fona.getGPS(&latitude2, &longitude2, &speed_kph2, &heading2, &altitude2);
boolean postData = false;
float dist_calc=0;
float dist_calc2=0;
float diflat=0;
float diflon=0;
//I've to spplit all the calculation in several steps. If i try to do it in a single line the arduino will explode.
diflat=radians((latitude2)-(latitude));
latToPost = latitude;
latitude=radians(latitude);
latitude2=radians(latitude2);
diflon=radians((longitude2)-(longitude));
dist_calc = (sin(diflat/2.0)*sin(diflat/2.0));
dist_calc2=cos(latitude);
dist_calc2*=cos(latitude2);
dist_calc2*=sin(diflon/2.0);
dist_calc2*=sin(diflon/2.0);
dist_calc +=dist_calc2;
dist_calc=(2*atan2(sqrt(dist_calc),sqrt(1.0-dist_calc)));
Serial.print(F("Longitude is: "));
Serial.println(longitude);
Serial.print(F("Latitude is: "));
Serial.println(latToPost);
dist_calc*=6371000.0; //Converting to meters
Serial.print(F("Distance between points is: "));
Serial.println(dist_calc);
if(dist_calc >= 50) { //only post data if distance between points is >= 50m
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["vehicle_name"] = "MNA 323 BL Excavator";
root["imei"] = imei;
root["user_id"] = "1";
root["longitude"] = longitude;
root["latitude"] = latToPost;
root.prettyPrintTo(Serial);
// Post data to website
uint16_t statuscode = 0;
int16_t length = 0;
char url[80] = "myurl.com/post";
char data[250] = "";
//flushSerial();
root.printTo((char*)data, root.measureLength() + 1);
Serial.print(F("DATA IS: "));
Serial.println(data);
Serial.println(F("****"));
if (!fona.HTTP_POST_start(url, F("application/json"), (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
Serial.println("Failed!");
}
while (length > 0) {
while (fona.available()) {
char c = fona.read();
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
#else
Serial.write(c);
#endif
length--;
if (! length) break;
}
}
Serial.println(F("\n****"));
fona.HTTP_POST_end();
}
delay(15000);
}