首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino GPS和GPRS Shield在将数据存储到数据库时停止工作

问Arduino GPS和GPRS Shield在将数据存储到数据库时停止工作
EN

Stack Overflow用户
提问于 2013-06-28 01:22:51
回答 3查看 1.8K关注 0票数 1

我在试着为汽车做一个实时跟踪。我使用GPS Shield和GPRS Shield。它的作用是获取GPS纬度和经度,并将数据存储到我的数据库中。但是,问题是,当它获得gps坐标,然后我的GPS屏蔽自动关闭,当GPRS屏蔽完成存储在我的数据库中的数据。

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

问题是为什么我的GPS屏蔽关闭时,GPRS屏蔽做存储数据?有人能帮我吗?这是我的业余代码。

代码语言:javascript
复制
#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;
      } 
    }
  }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-07-01 18:37:30

尝试使用外部电源。这样行得通吗?

票数 0
EN

Stack Overflow用户

发布于 2013-07-15 09:25:03

实际上应该是:

代码语言:javascript
复制
digitalWrite(3,LOW);//Enable GSM mode
digitalWrite(4,HIGH);//Disable GPS mode
票数 0
EN

Stack Overflow用户

发布于 2014-02-05 08:48:09

问题可能是软件串行端口冲突,因为GPS和GSM屏蔽都使用串行端口。在你的例子中,你有耦合的防护罩。当接收到GPS信号时,GSM关断,并且当采集到信号时,GPS关断并且GSM开始工作。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17349519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档