Pages: [1]   Go Down
Author Topic: help need: upload GPS data to MySQL server with Sim900  (Read 42 times)
0 Members and 1 Guest are viewing this topic.
Johannesburg, South Africa
Offline Offline
Full Member
***
Karma: 0
Posts: 144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I wonder if anyone can help me with this one, please. And, yes, it's a programming question I have.

My environment is as follows:
Arduino MEGA2560
Ultimate GPS breakout boad, with GPS connected to Serial3
Sim900 module, with Sim900 connected to Serial2
Micro-SD card module with 4GB micro-SD card.
PHP+MySQL  web server to store and view GPS data


The idea is to have a GPS tracking device in a vehicle, which can sometimes to out of 3G/GPRS coverage, but keep saving the data on a micro-SD card, and then upload to a PHP web page, in order to save to a MySQL server.

Right now, the Arduino is saving GPS information (lat, lon, date, time, speed, direction) to a micro-sd card every 2 minutes, into a .csv.

The vehicle operates in the bushes and the driver doesn't know much about this kind of technology. But I don't want him to even know it there's, so the "GPS tracker" should work autonomously.

I need to be able to view the data in real-time, or as close to real-time as possible. It won't always be possible if there's no GPRS/3G coverage, but that's fine, as long as I could at least get the last-known location and direction.


Now, onto the code:

The Arduino MEGA2560 saves the GPS data into a .CSV file, every 2 minutes, as follows:

Quote
void loop()
{

  while (!Serial1.available()) {
    unsigned long currentMillis = millis();

    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;   

      // if the LED is off turn it on and vice-versa:
      if (ledState == 0)
        ledState = 255;
      else
        ledState = 0;
      // set the LED with the ledState of the variable:
      analogWrite(GPS_Error, ledState);
    }
  }
  // read the OptoCoupler input pin:
  if (digitalRead(Opto1) == LOW)
  {
    analogWrite(Opto_Enable, 255);
    Serial.println("Opto Enabled.");
    bool newdata = false;
    unsigned long start = millis();
    // Every second we print an update
    do
    {
      if (feedgps())
        newdata = true;
    } while (millis() - start < LOG_INTERVAL*1000);

    gpsdump(gps); 
   
  }
analogWrite(Opto_Enable, 0);
}


static void gpsdump(TinyGPS &gps)
{
  float flat, flon;
  unsigned long age, date, time, chars = 0;
  gps.f_get_position(&flat, &flon, &age);
  //  Serial.print("Latitude: "); print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 9, 5);
  //  Serial.print(" Longitude: "); print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 10, 5);
  //  Serial.print(" Date: "); print_date(gps);
  //  Serial.print(" Speed: "); print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
  //  Serial.println();

  // SD Card logging
  dataFile = SD.open("gps_log.csv", FILE_WRITE);

  int year;
  byte month, day, hour, minute, second, hundredths;
  // unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
  {
    Serial.println("No GPS Fix");
    analogWrite(GPS_Error, 255);
   
    // Activate GPS_Error Debug LED
    /*
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;   
      // if the LED is off turn it on and vice-versa:
      if (ledState == 0)
        ledState = 255;
      else
        ledState = 0;
      // set the LED with the ledState of the variable:
      analogWrite(GPS_Error, ledState);
    } // END Activate GPS_Error Debug LED */
  }
  else {
    analogWrite(GPS_Error, 0);
    if(dataFile)                                                             // if datafile is available, write to it
    {                                                                     // otherwise just write to pc terminal
      //   Serial.println("SD data file opened ok");
      dataFile.print(flat,5);
      dataFile.print(",");
      dataFile.print(flon,5);
      dataFile.print(",");
      dataFile.print(year);
      dataFile.print("-");
      dataFile.print(month);
      dataFile.print("-");
      dataFile.print(day);
      dataFile.print(",");
      dataFile.print(hour+2);
      dataFile.print(":");
      dataFile.print(minute);
      dataFile.print(":");
      dataFile.print(second);
      dataFile.print(",");
      if(gps.f_speed_kmph()>1) {
        dataFile.println(gps.f_speed_kmph());
      } else {
         dataFile.println("0");
      }
    }
    Serial.print("Data written to SD Card:  ");
    Serial.print(flat,5);
    Serial.print(", ");
    Serial.print(flon,5);
    Serial.print(", ");
    Serial.print(year);
    Serial.print("-");
    Serial.print(month);
    Serial.print("-");
    Serial.print(day);
    Serial.print(",");
    Serial.print(hour+2);
    Serial.print(":");
    Serial.print(minute);
    Serial.print(":");
    Serial.print(second);
    Serial.print(",");
    Serial.println(gps.f_speed_kmph());
    //   Serial.print(" ,");
    //   Serial.println(age);
    dataFile.close();

    // Activate SD_Write Debug LED
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;   
      // if the LED is off turn it on and vice-versa:
      if (ledState == 0)
        ledState = 255;
      else
        ledState = 0;
      // set the LED with the ledState of the variable:
      analogWrite(SD_Write, ledState);       
    }
    // END SD_Write Debug LED
  }   
}


I simply don't know where to begin with this, and have searched the forums and google for a sample but couldn't find anything.

Can anyone please help me with this?
I just need some sample code, or even a tutorial / wiki / blog article, showing how a .CSV file is opened and the data is posted to a PHP file on a webserver, sequentially.


 

I know how to to submit a single string to a web page, but not a few hundred, or thousand stings.
Lastly, I need a way to know where the data submission has stopped.

Let's say the .CSV file has 500 lines, and it submits strings to the PHP webpage, but the GRPS signal is lost during transmission (i.e. data plan has run out, vehicle moves into non-coverage area, problems with GRPS provider, etc.) how will I know where it stopped, and where to continue?

I suppose the data would need to be submitted, say every 5 or so minutes?
Logged

Johannesburg, South Africa
Offline Offline
Full Member
***
Karma: 0
Posts: 144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can anyone help me with this, please?
Logged

Pages: [1]   Go Up
Jump to: