2016-05-18 9 views
0

:Dこんにちは!私は素敵なプロジェクトの真っ只中です。町の水道のためのウォーターポンプの自動化についてです。だから私は仕事のためにArduinoを選んだ。私はインターネット接続を介してArduinoとAndroid Appを通信する予定です。私はすでにAPIとウェブサーバーの部分を作成し、またアプリケーションを作成しました(そして、アプリケーションでは私のDBデータを見ることができます)。今、私はArduino-Ethernetシールドの側です。私はGETメソッドをテストしましたが、それはPOSTメソッドでもうまく機能します。しかし、私がそれらを混ぜようとすると、彼らはただ仕事を止めました。私は別のチュートリアルやフォーラムでいくつかの(広範な)検索を行い、 "GET用のクライアントとPUT用のクライアントを作る"を読んで、PUTメソッドをもう一度働かせました(今は私のwebServerレスポンスを見ることができます)。 。??!同じスケッチでPOSTとGETを行う

/* 
 
    Web client sketch for IDE v1.0.1 and w5100/w5200 
 
    Uses POST method. 
 
    Posted November 2012 by SurferTim 
 
*/ 
 

 
#include <SPI.h> 
 
#include <Ethernet2.h> 
 

 
double lecturasensor1 =20.40; 
 
double lecturasensor2 =20.60; 
 
char var1[6]; 
 
char var2[6]; 
 
String a ; 
 
String b ; 
 

 
int id = 1; 
 

 
byte mac[] = { 
 
    0x90, 0xA2, 0xDA, 0x10, 0x5B, 0x2B }; 
 

 
//Change to your server domain 
 
char serverName[] = "asada-florencia.herokuapp.com"; 
 

 
// change to your server's port 
 
int serverPort = 80; 
 

 
// change to the page on that server 
 
char pageName[] = "/lecturas"; 
 

 
EthernetClient clientGET; 
 
EthernetClient clientPOST; 
 
IPAddress ip(172, 24, 46, 94); 
 
int totalCount = 0; 
 
// insure params is big enough to hold your variables 
 
char params[100]; 
 

 
// set this to the number of milliseconds delay 
 
// this is 30 seconds 
 
#define delayMillis 8000UL 
 

 
unsigned long thisMillis = 0; 
 
unsigned long lastMillis = 0; 
 

 
void setup() { 
 
    Serial.begin(9600); 
 

 
    // disable SD SPI no vamos a usar SD 
 
    //pinMode(4,OUTPUT); 
 
    //digitalWrite(4,HIGH); 
 

 
    if (Ethernet.begin(mac) == 0) { 
 
    Serial.println("Failed to configure Ethernet using DHCP"); 
 
    // no point in carrying on, so do nothing forevermore: 
 
    // try to congifure using IP address instead of DHCP: 
 
    Ethernet.begin(mac, ip); 
 
    } 
 
    // give the Ethernet shield a second to initialize: 
 
    delay(1000); 
 

 
    Serial.println("connecting..."); 
 

 
} 
 

 
void loop() 
 
{ 
 
//Pirmero el GET (por prioridad) 
 
    if (clientGET.connect(serverName, 80)) { 
 
    Serial.println("connected"); 
 
    // Make a HTTP request: 
 
    clientGET.println("GET /lecturas HTTP/1.1"); 
 
    clientGET.println("Host: asada-florencia.herokuapp.com"); 
 
    clientGET.println("Connection: close"); 
 
    clientGET.println(); 
 
    } 
 
    else { 
 
    // kf you didn't get a connection to the server: 
 
    Serial.println("connection failed"); 
 
    } 
 

 
    if (clientGET.available()) { 
 
    char c = clientGET.read(); 
 
    Serial.print(c); 
 
    } 
 

 
    // if the server's disconnected, stop the client: 
 
    if (!clientGET.connected()) { 
 
    Serial.println(); 
 
    Serial.println("disconnecting."); 
 
    clientGET.stop(); 
 

 
    // do nothing forevermore: 
 
    while (true); 
 
    } 
 
/* 
 
* final GET 
 
* 
 
------------------------------------------ 
 
* 
 
* Ahora el POST después de un tiempo 
 
*/ 
 
delay(3000); 
 

 
    thisMillis = millis(); 
 

 
    if(thisMillis - lastMillis > delayMillis) 
 
    { 
 
    lastMillis = thisMillis; 
 

 
    a = dtostrf(lecturasensor1, 4 , 4 , var1); 
 
    b = dtostrf(lecturasensor2, 4 , 4 , var2); 
 
    
 
    String jsonString = "{\"valor\" : "; 
 
    jsonString += a; 
 
    jsonString +=" , \"valor2\" : "; 
 
    jsonString += b; 
 
    jsonString +="\"}"; 
 

 
    clientPOST.print(jsonString); 
 
    Serial.print(jsonString); 
 
     
 
    if(!postPage(serverName,serverPort,pageName,params)) Serial.print(F("Fail ")); 
 
    else Serial.print(F("Pass ")); 
 
    totalCount++; 
 
    Serial.println(totalCount,DEC); 
 
    }  
 
} 
 

 
byte postPage(char* domainBuffer,int thisPort,char* page,char* thisData) 
 
{ 
 
    int inChar; 
 
    char outBuf[64]; 
 

 
    Serial.print(F("connecting...")); 
 

 
    if(clientPOST.connect(domainBuffer,thisPort) == 1) 
 
    { 
 
    Serial.println(F("connected")); 
 

 
    // send the header 
 
    sprintf(outBuf,"POST %s HTTP/1.1",page); 
 
    clientPOST.println(outBuf); 
 
    sprintf(outBuf,"Host: %s",domainBuffer); 
 
    clientPOST.println(outBuf); 
 
    clientPOST.println(F("Connection: close\r\nContent-Type: application/json")); 
 
    sprintf(outBuf,"Content-Length: %u\r\n",strlen(thisData)); //thisData solo toma en cuenta la medida del header por que el server lo necesita 
 
    clientPOST.println(outBuf); 
 

 
    // buscar cómo realizar el POST HTTP (sintaxis)-->thisData esta como puntero que contiene las variables a mostrar en el "body" 
 
    //esto que sigue es el envio de datos thisData 
 
    clientPOST.print(thisData); 
 
    Serial.println("Datos enviados"); 
 
    Serial.println(thisData); 
 
    } 
 
    else 
 
    { 
 
    Serial.println(F("failed")); 
 
    return 0; 
 
    } 
 

 
    int connectLoop = 0; 
 

 
    while(clientPOST.connected()) 
 
    { 
 
    while(clientPOST.available()) 
 
    { 
 
     inChar = clientPOST.read(); 
 
     Serial.write(inChar); 
 
     connectLoop = 0; 
 
    } 
 

 
    delay(1); 
 
    connectLoop++; 
 
    if(connectLoop > 10000) 
 
    { 
 
     Serial.println(); 
 
     Serial.println(F("Timeout")); 
 
     clientPOST.stop(); 
 
    } 
 
    } 
 

 
    Serial.println(); 
 
    Serial.println(F("disconnecting.")); 
 
    clientPOST.stop(); 
 
    return 1; 
 
}

+0

'if(clientGET.connect(serverName、80))'。だから、ArduinoはGETやPOSTリクエストをウェブサーバーに出していますか? Androidはどこに出ていますか?あなたはGETとPOSTで何を混ぜようとしていますか?あたかもそうした要求を受けているかのように話しました。コメントではなくあなたの投稿にすべての情報を追加してください。 – greenapps

答えて

0

それを解決::(機能していないあなたが私にそれらをミックスする方法についていくつかのアドバイスや任意のアイデアを与えることができるのおかげ これは、混合コードです!DでしたすべてのGETヘッダーで、ヘッダーの各部分にclientPOST.println()を使用しました。ありがとうございます。ありがとうございました。ありがとうございます。ありがとうございます。ありがとうございます。

// Header del GET 
 
    clientPOST.println("GET /yourendpoint HTTP/1.1"); 
 
    clientPOST.println("Host: yourhost.com"); 
 
    clientPOST.println("Accept: text/html,application/xhtml+xml,application/xml"); 
 
    clientPOST.println("Upgrade-Insecure-Requests: 1"); 
 
    clientPOST.println("User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"); 
 
    clientPOST.println("Accept-Encoding: gzip, deflate, sdch"); 
 
    clientPOST.println("Accept-Language: en-US,en"); 
 
    clientPOST.println();

ありがとうございます!

関連する問題