我买了this shield。
我的项目是使用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脚本&lt;我只是想从服务器获取响应&gt;
<?php
var_dump($_REQUEST["gps_data"]);
?>
请帮助我!
答案 0 :(得分:-1)
您正在打开TCP连接并尝试将其作为HTTP读取,它不会以这种方式工作。如果要在PHP中获取数据,则必须改为打开HTTP连接。