2016-02-11 108 views

回答

0

爲了處理HTTP請求,您可以使用RestClient庫而不是編寫所有低級請求。它節省了大量時間,並且不易出錯。

例如,對於GET請求,所有你需要做的是:

String response = ""; 
int statusCode = client.post("/", "foo=bar", &response); 

One good such library與SSL支持由用戶GitHub的書面DaKaz。

您可以將它用於您的GET請求。返回的響應將不包含HTTP頭。該函數將返回沒有標題的服務器的響應。

現在,您可以使用bblanchin的ArduinoJson庫來解碼JSON對象。

細節可以看出here.

或者你也可以做簡單的字符串manipuation得到的數值雖然不是採取推薦的路線,而且容易出錯。

0

這裏是經由HTTP庫發送JSON一個例子:

#include <ESP8266WiFi.h>  
    #include <ArduinoJson.h> 
    #include <ArduinoHttpClient.h> 

    #define JSON_BUF_SIZE  256 

    WiFiClient wifi; 
    HttpClient poster = HttpClient(wifi, IP, PORT); 

    void HTTPPost(){ 
     String contentType = "application/json"; 
     StaticJsonBuffer<JSON_BUF_SIZE> jsonBuffer; 
     JsonObject& jsonData = jsonBuffer.createObject(); 
     jsonData["valuename"] = "value"; 
     String postData = ""; 
     jsonData.printTo(postData); 
     poster.post("/", contentType, postData); 
     printf("Trace : ResponseCode : %d\n", poster.responseStatusCode()); 
     printf("Trace : Incoming Body : %s\n", poster.responseBody().c_str()); 
    }