2014-03-01 35 views
0

我買了this shieldArduino Tracker的3G + GPS護罩

我的項目是利用GPS進行跟蹤器和數據上傳到服務器

這是使用

int8_t answer; 
int onModulePin= 2; 
char gps_data[100]; 
int counter; 


char aux_str[50]; 

char server[ ]="***.***.net"; 
char port[ ]="80"; 


void setup(){ 

pinMode(onModulePin, OUTPUT); 
Serial.begin(115200); 

Serial.println("Starting..."); 
power_on(); 

delay(5000); 

// starts GPS session in stand alone mode 
answer = sendATcommand("AT+CGPS=1,1","OK",1000); 
if (answer == 0) 
{ 
Serial.println("Error starting the GPS"); 
Serial.println("The code stucks here!!"); 
while(1); 
} 

//sendATcommand("AT+CPIN=5512", "OK", 2000); 
//delay(3000); 

while((sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || 
sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0); 

// sets APN, user name and password 
sendATcommand("AT+CGSOCKCONT=1,\"\",\"jawalnet.com.sa\"", "OK", 2000); 
sendATcommand("AT+CSOCKAUTH=1,1,\"\",\"\"", "OK", 2000); 


} 
void loop(){ 

answer = sendATcommand("AT+CGPSINFO","+CGPSINFO:",1000); // request info from GPS 
if (answer == 1) 
{ 

counter = 0; 
do{ 
while(Serial.available() == 0); 
gps_data[counter] = Serial.read(); 
counter++; 
} 
while(gps_data[counter - 1] != '\r'); 
gps_data[counter] = '\0'; 
if(gps_data[0] == ',') 
{ 
Serial.println("No GPS data available"); 
} 
else 
{ 
Serial.print("GPS data:"); 
Serial.println(gps_data); 
Serial.println(""); 


sprintf(aux_str, "AT+NETOPEN=\"TCP\",%s", port); 
answer = sendATcommand(aux_str, "Network opened", 20000); 

if (answer == 1) 
{ 
Serial.println("Network opened"); 
sprintf(aux_str, "AT+TCPCONNECT=\"%s\",%s", server, port); 
answer = sendATcommand(aux_str, "Connect ok", 20000); 
if (answer == 1) 
{ 
Serial.println("Socket opened"); 
sprintf(aux_str, "AT+TCPWRITE=%d", strlen(gps_data)); 
answer = sendATcommand(aux_str, ">", 20000); 
if (answer == 1) 
{ 
sendATcommand(gps_data, "Send OK", 20000); 
} 
sendATcommand("AT+NETCLOSE", "OK", 20000); 
} 
else 
{ 
Serial.println("Error opening the socket"); 
sendATcommand("AT+NETCLOSE", "OK", 20000); 
} 
} 
else 
{ 
Serial.println("Error opening the nertwork"); 
sendATcommand("AT+NETCLOSE", "OK", 20000); 
} 

} 

} 
else 
{ 
Serial.println("Error"); 
} 

delay(5000); 
} 

void power_on(){ 

uint8_t answer=0; 

// checks if the module is started 
answer = sendATcommand("AT", "OK", 2000); 
if (answer == 0) 
{ 
// power on pulse 
digitalWrite(onModulePin,HIGH); 
delay(3000); 
digitalWrite(onModulePin,LOW); 

// waits for an answer from the module 
while(answer == 0){ 
// Send AT every two seconds and wait for the answer 
answer = sendATcommand("AT", "OK", 2000); 
} 
} 

} 


int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout) 
{ 

uint8_t x=0, answer=0; 
char response[100]; 
unsigned long previous; 

memset(response, '\0', 100); // Initialize the string 

delay(100); 

while(Serial.available() > 0) Serial.read(); // Clean the input buffer 

Serial.println(ATcommand); // Send the AT command 


x = 0; 
previous = millis(); 

// this loop waits for the answer 
do{ 

if(Serial.available() != 0){ 
response[x] = Serial.read(); 
x++; 
// check if the desired answer is in the response of the module 
if (strstr(response, expected_answer1) != NULL) 
{ 
answer = 1; 
} 
} 
// Waits for the asnwer with time out 
} 
while((answer == 0) && ((millis() - previous) < timeout)); 

Serial.println(response); 

return answer; 
} 

我不知道爲什麼它不連接到服務器的代碼即時通訊!

這是串行監視器上

AT+NETOPEN="TCP",80 

    Network opened 

    AT+TCPCONNECT="malik.basalamah.net",80 


    Connect ok 
    Socket opened 
    AT+TCPWRITE=55 

    > 
    2124.751744,N,03946.686557,E,010314,154812.0,267.0,0,0 

    ted</h1> 
    <p>AT+TCPWRITE=55 to /index.html not supported.<br /> 
    </p> 
    </body></html> 


    +IPCLOSE: 255, 1, 173.236.148.10, 80 

    +IPCLOSE: 255, 2, 173.236.148.10, 80 
    html><head> 
    <title>501 Method NotStarting... 

這是PHP腳本<我只是試圖從服務器的響應>

<?php 
var_dump($_REQUEST["gps_data"]); 
?> 

請幫幫我!

回答

-1

您正在打開TCP連接並嘗試將其作爲HTTP讀取,它不會以此方式工作。如果你想用PHP獲取數據,則必須改爲打開HTTP連接。

+0

你能分享一些可能有助於解決這個問題的代碼嗎? – rfornal

+0

HTTP實際上是基於TCP的。 – Danpe