2013-06-27 26 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開始工作。