I am trying to build a GPS device for a truck.I have connected the GPS device to the arduino Mega and use GPRS Shield to send the Geo co-ordinates to the server.This device will be running continuously and logging the data to the server.The device is connected to the trucks battery using some regulators.
Once the Truck start to move the device is not able to get the co-ordinates
What do you mean. Does the unit stop sending data via serial to the Arduino ?
Have you confirmed isolated the problem to movement, or just that it doesn't work when the engine is running, e.g this could indicate electrical interference on the power supply etc
Please give more details, including things you have already tried to resolve this problem
As of now ,I am testing the Module fitted in my bike and poweringwith the a 12V ,7Ah battery after the module didn't function in the truck.I am able to get the GPS co-ordinates from the GPS module while on the move directly and through the arduino.The problem now ,I think is with the code.Below is my code.I am working on this right now to fix it.
#include <TinyGPS++.h>
#include <SD.h>
// The TinyGPS++ object
TinyGPSPlus gps;
//SD Shield
const int chipSelect = 53;
const int MaxChars = 37;
void setup() {
Serial.begin(19200); //Terminal
delay(100);
Serial1.begin(9600); //GPS Module
delay(100);
pinMode(53, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
}
void loop()
{
smartDelay(100);
String fn_gps_str=gps_sensor();
Serial.print("The Msg: ");
Serial.println(fn_gps_str);
int tot_len=(int)fn_gps_str.length()+1;
char gps_char[tot_len];
fn_gps_str.toCharArray(gps_char,tot_len);
char* msg_gps=&gps_char[0];
logger(tot_len,msg_gps);
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while(true);
}
}
void logger(int tot_len,char* gps_char)
{
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
for(int ind=0; ind < tot_len; ind++){
dataFile.print(gps_char[ind]);
delay(10);}
dataFile.println("");
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
String gps_sensor()
{
String msg="UNIT1001,TRK7737,";
if(gps.altitude.isUpdated() || gps.location.isUpdated() || gps.time.isUpdated()){
if (gps.location.isValid())
{
char buffer_lat[10]; char buffer_lng[10];
char buffer_alt[5]; char buffer_kmph[5];
String lat_st = dtostrf(gps.location.lat(), 3, 6, buffer_lat);
String lng_st = dtostrf(gps.location.lng(), 3, 6, buffer_lng);
String alt=dtostrf(gps.altitude.meters(), 6, 2, buffer_lng);
String kmph_gps = dtostrf(gps.speed.kmph(), 3, 2, buffer_kmph);
msg.concat(lat_st); msg.concat(",");
msg.concat(lng_st); msg.concat(",");
msg.concat(alt); msg.concat(",");
msg.concat(kmph_gps);msg.concat(",");
}
else
{
Serial.print(F("INVALID Co-ordinates"));
}
if (gps.date.isValid())
{
if (gps.date.month() < 10) msg.concat("0");
msg.concat(gps.date.month()); msg.concat("/");
if (gps.date.day() < 10) msg.concat("0");
msg.concat(gps.date.day()); msg.concat("/");
msg.concat(gps.date.year()); msg.concat("_");
}
else
{
Serial.print(F("INVALID Date"));
}
if (gps.time.isValid())
{
if (gps.time.hour() < 10) msg.concat("0");
msg.concat(gps.time.hour()); msg.concat(":");
if (gps.time.minute() < 10) msg.concat("0");
msg.concat(gps.time.minute()); msg.concat(":");
if (gps.time.second() < 10) msg.concat("0");
msg.concat(gps.time.second());
}
else
{
Serial.print(F("INVALID Time"));
}
}
return msg;
}
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
while (millis() - start < ms)
{
while (Serial1.available())
gps.encode(Serial1.read());
}
}
Why do you say it is the code? Have you tried to power your setup with a separate battery from truck and keep it inside the truck instead of on your bike? I've used the EM406A module I bought from sparkfun.com and recorded my trips on highways in my car.
I tried it on bike with a separate battery only as I couldn't get hold of a truck today.I could see that the GPS module when connected via RS232 port am able to read all the co-ordinates while on the move.Then,I again tried with arduino,with a simple program like one below and it worked too while on the move.
void loop()
{
// If any characters have arrived from the GPS,
// send them to the TinyGPS++ object
while (Serial1.available() > 0)
gps.encode(Serial1.read());
if (gps.location.isUpdated() || gps.altitude.isUpdated())
{
Serial.print("Location: ");
Serial.print(gps.location.lat(), 6);
Serial.print(",");
Serial.print(gps.location.lng(), 6);
Serial.print(" Altitude: ");
Serial.print(gps.altitude.meters());
Serial.print(" Speed: ");
Serial.println(gps.speed.kmph());
}
}
So,I think it is the problem with my initial code.
You are in the best position to debug your code, as you have the GPS hardware and know when it doesn't work.
I suggest you follow normal debugging methods. E.g. Rather than writing all your code in one go and not knowing why it is failing, write in smaller modules, and test each module at a time.
I have made a sketch like this ..1.take the GPS co-ordinates and log it in a SD card and then start the GPRS connections and send the co-ordinates to the web server. After a drive ,I could see that the GPS co-ordinates are logged in the SD card properly and the GPRS connection is having some problem.I could feel that the Arduino Mega is heating up while on the move.I am using GPRS Shield V2http://www.seeedstudio.com/wiki/GPRS_Shield_V2.0#Improvement_Details_of_GPRS_Shield_V2.0 on top of Arduino Mega 2560.The SIM 900 GSM/GPRS module requires 2A when it is transmitting.I guess the Arduino Mega 5V pin can't supply it.Please correct me if my understanding is wrong.I now planning to use a stand-alone SIM900 module now to see if that is the real problem.
You want to dynamically track a truck. Also with respects to batteries ON/OFF condition.
To achieve above, you are interested in logging in the SD card and then transmitting it to the Server.
I am assuming that, you have latitude and longitude values received from the GPS and stored in the SD card. Next, you need to upload these data to the server.
Things to note:
What server you use?
What Database you use?
You need a connection interface between Hardware (that you have) with Server through JDBC or php.
You could consider connecting a GPIO pin to the GPS module reset input, give it a GPIO controlled software reset whenever powering up GPS. This might fix problem of slow rise time of power supply sometimes causing GPS module to not correctly restart.
Hi all,
I have finally fixed the issues and the GPS data logger is working as it is intended.Thank you all for the inputs.I have powered the GPRS shield through a separate connection and re-wrote the Sketch a bit to response(from SIM900) driven and it's working now.