I'm basically trying to make a device that pulls the GPS information from a SIM808 and pushes it to a server online via GPRS.
I am sorry as I am not good at programming so ive spent days trying to debug this but i have not been able to figure out whats wrong.
Ill get the basics out of the way.
Board: Mega2560 (I know its overkill but its what i had in hand) SIM+GPS sensor: SIM808 Module TinyGPS v13 RTC: DS1307 Arduino IDE v1.8.8
Down to the specifics, I started with SoftwareSerial Library but i heard it was inefficient so i switched to AltSoftSerial. The serial library is working as I do get output using AT commands. The problem is that when i load up the stock TinyGPS example sketch sometimes it works sometimes it gives me error saying the GPS module wasn't detected.
i tried using AT commands directly or a different GPS library but i cant figure out how to pass on data to a database over GPRS.
but basically I am sure the wiring isn't faulty nor is the GPS module (i hope) as it gives an output at ATCommands, even the RTC module is confirmed working. I know the URL in the ATCommand is fake but that is for demonstration purposes.
Coming to the code below. If i change the bool newData = false; to bool newData = true; the the code works just once with wrong GPS information.
any help would be greatly appreciated. Thank You
Edit 1: The problem has been solved, it wasn't working when i used both GPS and GPRS on SIM808 i switched to NeoGPS 6m module and GPRS from SIM800/808 it worked. Thank You.
#include <AltSoftSerial.h>
#include <TinyGPS.h>
#include <Wire.h>
#include <DS1307new.h>
TinyGPS gps;
SoftwareSerial sGPS (46,48);
uint16_t startAddr = 0x0000; //Start address to store in NVRam
uint16_t lastAddr; //New address to store in NVRam
uint16_t TimeIsSet = 0xaa55; //Helper that time must not be set again
int currentDay = 0;
int currentMonth = 0;
int currentYear = 0;
int currentHour = 0;
int currentMinute = 0;
float flat, flon;
float previousFLat = 0.0;
float previousFLon = 0.0;
void setup()
{
Serial.begin(9600);
sGPS.begin(9600);
/*
Control the clock.
Clock will only be set if NV-RAM Address does not contain 0xaa.
DS1307 should have a battery backup.
*/
RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
if (TimeIsSet != 0xaa55)
{
RTC.stopClock();
RTC.fillByYMD(2011,4,8);
RTC.fillByHMS(22,7,0);
RTC.setTime();
TimeIsSet = 0xaa55;
RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
RTC.startClock();
}
else
{
RTC.getTime();
}
/*
Control Register for SQW pin which can be used as an interrupt.
*/
RTC.ctrl = 0x00; // 0x00=disable SQW pin, 0x10=1Hz,
// 0x11=4096Hz, 0x12=8192Hz, 0x13=32768Hz
RTC.setCTRL();
delay(5000);
}
void loop()
{
RTC.getTime();
currentDay = RTC.day;
currentMonth = RTC.month;
currentYear = RTC.year;
currentHour = RTC.hour;
currentMinute = RTC.minute;
bool newData = false;
//For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (sGPS.available())
{
char c = sGPS.read();
Serial.write(c); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}
if (newData)
{
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
if ((flat !=previousFLat)||(flon !=previousFLon))
{
previousFLat = flat;
previousFLon = flon;
Serial.print (previousFLat,6);
Serial.println(previousFLon,6);
SendSQL();
}
}
}
void SendSQL()
{
//Serial.println("Start Send");
Serial.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
delay(500);
Serial.println("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"");
delay(500);
Serial.println("AT+SAPBR=1,1");
delay(500);
Serial.println("AT+SAPBR=2,1");
delay(3000);
Serial.println("AT+HTTPINIT");
delay(500);
Serial.println("AT+HTTPARA=\"CID\",1");
delay(500);
Serial.print("AT+HTTPARA=\"URL\",\"www.google.com/add_data.php?");
Serial.print("addDay=");
if (currentDay <10)
{
Serial.print("0");
Serial.print(currentDay);
}
else
{
Serial.print(currentDay);
}
Serial.print("/");
if (currentMonth < 10)
{
Serial.print("0");
Serial.print(currentMonth);
}
else
{
Serial.print(currentMonth);
}
Serial.print("/");
Serial.print(currentYear);
Serial.print("&addHour=");
if (currentHour <10)
{
Serial.print("0");
Serial.print(currentHour);
}
else
{
Serial.print(currentHour);
}
Serial.print(":");
if (currentMinute <10)
{
Serial.print("0");
Serial.print(currentMinute);
}
else
{
Serial.print (currentMinute);
}
Serial.print ("&addLati=");
Serial.print(previousFLat, 6);
Serial.print("&addLongti=");
Serial.print(previousFLon, 6);
Serial.println("\"");
delay(3000);
Serial.println("AT+HTTPACTION=0");
delay(15000);
Serial.println("AT+HTTPTERM");
delay(500);
Serial.println("AT+SAPBR=0,1");
delay(500);
Serial.println("Finish");
}
if (currentDay <10)
block with only two linesif (currentDay <10) Serial.print("0");
andSerial.print(currentDay);
...... do the similarly for the otherif
blocks