2014-01-13 98 views
0

你好。我使用的這個例子中,從烹飪黑客做一個HTTP GET調用我的服務器。(在頁面底部)HTTP GET對於緩衝區太大

http://www.cooking-hacks.com/documentation/tutorials/arduino-3g-gprs-gsm-gps

我的最終目標將是從PHP頁面得到一個字我Arduino的烏諾。

但我遇到了問題,因爲我的Arduino有一個有限的緩衝區和頭大小比它的緩衝區大。

  • 我的頭的大小爲363
  • 我的下載大小爲12
  • 的最大尺寸是64

因爲這是我的代碼是行不通的。我只想得到12個,但不知道如何做到這一點,我希望有人能幫助我指出正確的方向。

* Description: This example shows how to do a GET method. So the buffer of 
* is limited, we recommend to use the GET method with short answer for the 
* requested webs. 
* This example shows the AT commands (and the answers of the module) used 
* to work with HTTP. For more information about the AT commands, 
* refer to the AT command manual. 
* 
* Copyright (C) 2013 Libelium Comunicaciones Distribuidas S.L. 
* http://www.libelium.com 
* 
* This program is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published by 
* the Free Software Foundation, either version 3 of the License, or 
* (at your option) any later version. 
* 
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
* GNU General Public License for more details. 
* 
* You should have received a copy of the GNU General Public License 
* along with this program. If not, see <http://www.gnu.org/licenses/>. 
* 
* Version 0.2 
* Author: Alejandro Gallego 
*/ 


int8_t answer; 
int onModulePin = 2, aux; 
int data_size = 0; 
int end_file = 0; 

char aux_str[100]; 

char data[250]; 
int x = 0; 
long previous; 

char url[ ]="rubenvandijk.com"; 
int port= 80; 
char request[ ]="GET /misc/twittertest5/get_tweet.php HTTP/1.1\r\nHost: rubenvandijk.com\r\n"; 



void setup(){ 

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

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

delay(3000); 

// sets the PIN code 
sendATcommand("AT+CPIN=0000", "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,\"IP\",\"prepaidinternet\"", "OK", 2000); 
sendATcommand("AT+CSOCKAUTH=1,1,\"\",\"\"", "OK", 2000); 




} 
void loop(){ 

// request the url 
sprintf(aux_str, "AT+CHTTPACT=\"%s\",%d", url, port); 
answer = sendATcommand(aux_str, "+CHTTPACT: REQUEST", 60000); 

if (answer == 1) 
{   
    Serial.println(request); 
    //Serial.println(host); 
    //Serial.println("Content-Length: 0"); 
    // Sends <Ctrl+Z> 
    aux_str[0] = 0x1A; 
    aux_str[1] = 0x00; 
    answer = sendATcommand(aux_str, "+CHTTPACT: DATA,", 60000); 

    x=0; 
    do{ 
     if (answer == 1) 
     { 
      data_size = 0; 
      while(Serial.available()==0); 
      aux = Serial.read(); 
      do{ 
       data_size *= 10; 
       data_size += (aux-0x30); 
       while(Serial.available()==0); 
       aux = Serial.read();   
      } 
      while(aux != 0x0D); 

      Serial.print("Data received: "); 
      Serial.println(data_size); 





      if (data_size > 0) 
      { 
       while(Serial.available() < data_size); 
       Serial.read(); 


       for (int y = 0; y < data_size; y++) 
       { 
        data[x] = Serial.read(); 
        x++; 
       } 
       data[x] = '\0'; 
      } 
      else 
      { 
       Serial.println("Download finished");  
      } 
     } 
     else 
     { 
      Serial.println("Error getting the url"); 
      data_size = 0; 
     } 

     answer = sendATcommand2("", "+CHTTPACT: DATA,", "+CHTTPACT:0", 20000); 

    }while (answer != 1); 

    if (answer == 2) 
    { 
     Serial.print("Data received hallo:"); 
     Serial.println(data); 
    } 
    else 
    { 
     Serial.println("Error getting data"); 
    } 
} 
else 
{ 
    Serial.println("Error waiting the request");  
} 

delay(10000); 

} 

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));  

return answer; 
} 

int8_t sendATcommand2(char* ATcommand, char* expected_answer1, 
    char* expected_answer2, 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; 
     } 
     // check if the desired answer is in the response of the module 
     if (strstr(response, expected_answer2) != NULL)  
     { 
      answer = 2; 
     } 
    } 
    // Waits for the asnwer with time out 
} 
while((answer == 0) && ((millis() - previous) < timeout));  

return answer; 
} 

現在返回

�Starting... 
AT 
AT+CPIN=0000 
AT+CREG? 
AT+CGSOCKCONT=1,"IP","prepaidinternet" 
AT+CSOCKAUTH=1,1,"","" 
AT+CHTTPACT="rubenvandijk.com",80 
GET /misc/twittertest5/get_tweet.php HTTP/1.1 
Host: rubenvandijk.com 
Data received: 524 

它卡在

if (data_size > 0) 
      { 
       while(Serial.available() < data_size); 
       Serial.read(); 

回答

0

我有幾個想法,你可以試試... ...

1)處理傳入的數據一行一次不要緩衝所有的事情。 2)添加一些特殊字符到您的Web服務器響應中,拋棄所有數據,直到看到該字符或序列。假設在你想要的數據前面添加一個#符號,因爲字節進入緩衝區之後只有#號。 3)使用一個循環緩衝區,並讓它覆蓋,直到接收到所有的數據,留下緩衝區中的最後X個字節,通過從緩衝區尾部向後處理。

數字1是最好的答案,數字2是最簡單的。

+0

第二個聽起來不錯,我的目的是否可以詳細說明如何做到這一點?我對這種腳本語言沒有太多的經驗。所以我只知道如何將#添加到我的php哈哈哈,這不會讓我感到非常遙遠。對我來說,任何詞可以谷歌或我可以嘗試一個示例代碼? –