2013-06-27 58 views
1

我试图对汽车进行实时跟踪。我使用GPS盾和GPRS盾。它可以获得GPS纬度和经度,并且它可以将数据存储到我的数据库。但是,问题是,当它获得GPS坐标时,我的GPS护罩在GPRS Shield完成将数据存储在我的数据库中时自动关闭。当存储数据到数据库时,Arduino GPS和GPRS Shield停止工作

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

问题是为什么我的GPS屏蔽在GPRS屏蔽完成存储数据时关闭?任何人都可以帮我 这是我的业余代码。

#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; 
     } 
    } 
    } 
} 
+1

需要什么才能让它重新开始?将重置Arduino做到这一点,还是你必须重新开机?如果重置Arduino的话,你的代码可能会卡住...... –

+0

是的,重置arduinoe可以让它重新开始。有没有让我的程序卡住的代码?我试图通过发送短信来改变从存储到数据库的方法。这行得通。但为什么存储到数据库可以让我的代码卡住?我检查了arduino中的pin。有没有冲突 –

回答

0

尝试使用外部电源。那是行得通的吗?

+0

当然,它的作品。非常感谢你 –

0

实际上它应该是:

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

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