当存储数据到数据库时,Arduino GPS和GPRS Shield停止工作

时间:2013-06-27 17:22:51

标签: database gps arduino gprs

我正在尝试为汽车进行实时跟踪。我用过GPS Shield和GPRS Shield。它可以获得GPS纬度和长度,它可以将数据存储到我的数据库中。但问题是,当它获得gps坐标时,我的GPS Shield会在GPRS Shield完成时将数据存储在我的数据库中自动关闭。

我使用DFRobotcan的GPS / GSM / GPRS Shield和SeedStudio的GPRS Shield。我无法使用DFRobot的GPRS,因为我无法获得信号。这对我来说不是问题。

问题是为什么我的GPS防护罩在GPRS Shield完成存储数据时会降低?有人帮助我 这是我的业余代码。

#include <SoftwareSerial.h>
#include <String.h>
double lati, longati, latitudefix, longatitudefix;
String latu, longatu, latdir, londir, latitudebener, longatitudebener;
SoftwareSerial mySerial(7, 8);
String id="1310220001";

void setup()
{
  mySerial.begin(19200); // the GPRS baud rate
  pinMode(3,OUTPUT);//The default digital driver pins for the GSM and GPS mode
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  digitalWrite(5,HIGH);
  delay(1500);
  digitalWrite(5,LOW);

  digitalWrite(3,LOW);//Enable GSM mode
  digitalWrite(4,HIGH);//Disable GPS mode
  delay(2000);
  Serial.begin(9600);
  delay(5000);//GPS ready

  Serial.println("AT");  
  delay(2000);
  //turn on GPS power supply
  Serial.println("AT+CGPSPWR=1");
  delay(1000);
  //reset GPS in autonomy mode
  Serial.println("AT+CGPSRST=1");
  delay(1000);

  digitalWrite(4,LOW);//Enable GPS mode
  digitalWrite(3,HIGH);//Disable GSM mode
  delay(2000);

  Serial.println("$GPGGA statement information: ");
}
void loop()
{
  while(1)
  {
  Serial.print("UTC:");
  UTC();
  Serial.print("Lat:");
  latitude();
  Serial.print("Dir:");
  lat_dir();
  Serial.print("Lon:");
  longitude();
  Serial.print("Dir:");
  lon_dir();
  Serial.print("Alt:");
  altitude();
  convert();
  //Serial.println(latitudebener);
  //Serial.println(longatitudebener);
  Serial.println(' ');
  Serial.println(' ');


  if(latitudebener!="0.00000"&&longatitudebener!="0.00000"){
    sendlocation();
  }
  else{
  };
  }
}


void convert(){
  char bufferlat[20];
  latitudebener=dtostrf(latitudefix,5,5,bufferlat);
  char bufferlon[20];
  longatitudebener=dtostrf(longatitudefix,5,5,bufferlon);
}

void sendlocation(){
  mySerial.println("AT+CSQ");
  delay(100);

  mySerial.println("AT+CGATT?");
  delay(100);

  mySerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");//setting the SAPBR, the connection type is using gprs
  delay(1000);

  mySerial.println("AT+SAPBR=3,1,\"APN\",\"AXIS\"");//setting the APN, the second need you fill in your local apn server
  delay(4000);

  mySerial.println("AT+SAPBR=1,1");//setting the SAPBR, for detail you can refer to the AT command mamual
  delay(2000);;

  mySerial.println("AT+HTTPINIT"); //init the HTTP request
  delay(2000); 

  mySerial.println("AT+HTTPPARA=\"URL\",\"www.hamasa.cu.cc/editlocation.php?id="+id+"&latitude="+latitudebener+"&longatitude="+longatitudebener+"&latdir="+latdir+"&londir="+londir+"\"");
  delay(2000);

  mySerial.println("AT+HTTPACTION=0");//submit the request 
  Serial.println("8");
  delay(5000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.

  mySerial.println("AT+HTTPREAD");// read the data from the website you access
  delay(300);

  mySerial.println("");
  delay(100);
}

//void sms()
//{
//  mySerial.println("AT+CMGF=1\r"); //Because we want to send the SMS in text mode
//  delay(100);
//  ShowSerialData();
//  mySerial.println("AT + CMGS = \"+6283815535385\"");//send sms message, 
//  //be careful need to add a country code before the cellphone number
//
//  delay(100);
//  ShowSerialData();
//  mySerial.println("bisa bil, latitudenya:"+latitudebener+""+latdir+", terus longatitudenya:"+longatitudebener+""+londir);//the content of the message
//  delay(100);
//  ShowSerialData();
//  mySerial.println((char)26);//the ASCII code of the ctrl+z is 26
//  delay(100);
//  ShowSerialData();
//  mySerial.println();
//  ShowSerialData();
//}
void ShowSerialData()
{
  while(mySerial.available()!=0)
    Serial.write(mySerial.read());
}

double Datatransfer(char *data_buf,char num)//convert the data to the float type
{                                           //*data_buf:the data array                                      
  double temp=0.0;                           //the number of the right of a decimal point
  unsigned char i,j;

  if(data_buf[0]=='-')
  {
    i=1;
    //process the data array
    while(data_buf[i]!='.')
      temp=temp*10+(data_buf[i++]-0x30);
    for(j=0;j<num;j++)
      temp=temp*10+(data_buf[++i]-0x30);
    //convert the int type to the float type
    for(j=0;j<num;j++)
      temp=temp/10;
    //convert to the negative numbe
    temp=0-temp;
  }
  else//for the positive number
  {
    i=0;
    while(data_buf[i]!='.')
      temp=temp*10+(data_buf[i++]-0x30);
    for(j=0;j<num;j++)
      temp=temp*10+(data_buf[++i]-0x30);
    for(j=0;j<num;j++)
      temp=temp/10 ;
  }
  return temp;
}

char ID()//Match the ID commands
{
  char i=0;
  char value[6]={
    '$','G','P','G','G','A'            };//match the gps protocol
  char val[6]={
    '0','0','0','0','0','0'            };

  while(1)
  {
    if(Serial.available())
    {
      val[i] = Serial.read();//get the data from the serial interface
      if(val[i]==value[i]) //Match the protocol
      {   
        i++;
        if(i==6)
        {
          i=0;
          return 1;//break out after get the command
        }
      }
      else
        i=0;
    }
  }
}

void comma(char num)//get ','
{  
  char val;
  char count=0;//count the number of ','

  while(1)
  {
    if(Serial.available())
    {
      val = Serial.read();
      if(val==',')
        count++;
    }
    if(count==num)//if the command is right, run return
      return;
  }

}
void UTC()//get the UTC data -- the time
{
  char i;
  char time[9]={
    '0','0','0','0','0','0','0','0','0'
  };
  double t=0.0;

  if( ID())//check ID
  {
    comma(1);//remove 1 ','
    //get the datas after headers
    while(1)
    {
      if(Serial.available())
      {
        time[i] = Serial.read();
        i++;
      }
      if(i==9)
      {
        i=0;
        t=Datatransfer(time,2);//convert data
        t=t+70000.00;//convert to the chinese time GMT+8 Time zone
        Serial.println(t);//Print data
        return;
      } 
    }
  }
}
void latitude()//get latitude
{
  char i;
  char lat[10]={
    '0','0','0','0','0','0','0','0','0','0'};


  if( ID())
  {
    comma(2);
    while(1)
    {
      if(Serial.available())
      {
        lat[i] = Serial.read();
        i++;
      }
      if(i==10)
      {
        i=0;
        char buzz[20];
        lati=Datatransfer(lat,5);
        latu=dtostrf(lati,5,5,buzz);
        String latijam=latu.substring(0,1);
        String latimin=latu.substring(1,3);
        String latidet=latu.substring(4,9);
        double latijamd = latijam.toInt();
        double latimind = latimin.toInt();
        double latidetd = latidet.toInt();
        double latmin = latimind+(latidetd*0.00001);
        latitudefix= latijamd+(latmin/60);
        Serial.println(latitudefix,5); 
        return;
      } 
    }
  }
}
void lat_dir()//get dimensions
{
  char i=0,val;

  if( ID())
  {
    comma(3);
    while(1)
    {
      if(Serial.available())
      {
        val = Serial.read();
        latdir=String(val);
        Serial.print(latdir);
        Serial.println();
        i++;
      }
      if(i==1)
      {
        i=0;
        return;
      } 
    }
  }
}
void longitude()//get longitude
{
  char i;
  char lon[11]={
    '0','0','0','0','0','0','0','0','0','0','0'      };

  if( ID())
  {
    comma(4);
    while(1)
    {
      if(Serial.available())
      {
        lon[i] = Serial.read();
        i++;
      }
      if(i==11)
      {
        i=0;
        //Serial.println(Datatransfer(lon,5),5);
        char buffer[20];
        longati=Datatransfer(lon,5);
        longatu=dtostrf(longati,5,5,buffer);
        String longatijam=longatu.substring(0,3);
        String longatimin=longatu.substring(3,5);
        String longatidet=longatu.substring(6,11);
        double longatijamd = longatijam.toInt();
        double longatimind = longatimin.toInt();
        double longatidetd = longatidet.toInt();
        double longmin = longatimind+(longatidetd*0.00001);
        longatitudefix= longatijamd+(longmin/60);
        Serial.println(longatitudefix,5);
        return;
      } 
    }
  }
}
void lon_dir()//get direction data
{
  char i=0,val;

  if( ID())
  {
    comma(5);
    while(1)
    {
      if(Serial.available())
      {

        val = Serial.read();
        londir=String(val);
        Serial.print(londir);
        Serial.println();
        i++;
      }
      if(i==1)
      {
        i=0;
        return;
      } 
    }
  }
}

void altitude()//get altitude data
{
  char i,flag=0;
  char alt[8]={
    '0','0','0','0','0','0','0','0'
  };

  if( ID())
  {
    comma(9);
    while(1)
    {
      if(Serial.available())
      {
        alt[i] = Serial.read();
        if(alt[i]==',')
          flag=1;
        else
          i++;
      }
      if(flag)
      {
        i=0;
        Serial.println(Datatransfer(alt,1),1);//print altitude data
        return;
      } 
    }
  }
}

3 个答案:

答案 0 :(得分:0)

尝试使用外部电源。这有用吗?

答案 1 :(得分:0)

实际上它应该是:

digitalWrite(3,LOW);//Enable GSM mode
digitalWrite(4,HIGH);//Disable GPS mode

答案 2 :(得分:-1)

问题可能是软件串口冲突,因为GPSGSM屏蔽都使用串口。在你的情况下,你有耦合盾牌。在收到GPS信号时,GSM会被关闭,当获取信号时,GPS会关闭,GSM开始工作。