2017-09-25 21 views
-1

[this REST API] [1]からAjaxレスポンスを抽出する必要があります。このためのコードスニペットを入力してくださいArduinoとESP8266でJSON構文解析が失敗する

http://tutor4study.com/forms/ajaxDeviceValueからJSONデータを読み込み、それを解析する必要があります。

enter code here 
#include <ESP8266WiFi.h> 
#include <WiFiClient.h> 
#include <ArduinoJson.h> 

const char* ssid = "ssid"; 
const char* password = "password"; 

const char* host = "tutor4study.com"; 
const int httpsPort = 80; 
WiFiClient client; 
WiFiClient readClient; 
String sensorValue1 = "5555"; 
String sensorValue2 = "9999"; 
String readUrl = ""; 
char readLine; 
String readResponse =""; 
String readJsonResponse =""; 



void setup() { 
       Serial.begin(115200); 
       Serial.println(); 
       Serial.print("connecting to "); 
       Serial.println(ssid); 
       WiFi.begin(ssid, password); 
       while (WiFi.status() != WL_CONNECTED) { 
                 delay(500); 
                 Serial.print("."); 
                 } 
       Serial.println(""); 
       Serial.println("WiFi connected"); 
       Serial.println("IP address: "); 
       Serial.println(WiFi.localIP()); 
       Serial.print("connecting to "); 

       pinMode(4, OUTPUT); 
       pinMode(5, OUTPUT); 
       // yield(); 

      } 

StaticJsonBuffer<200> jsonBuffer; 



void readConnect(){ 
        if(!readClient.connect(host,httpsPort)){ 
                  Serial.println("connection failed for readCLient"); 
                  ESP.reset(); 
                  return; 
        } 
        readUrl = "/forms/ajaxDeviceValue"; 
        Serial.print("requesting URL: "); 
        Serial.println(readUrl); 
        readClient.print(String("GET ")+readUrl+" HTTP/1.1\r\n"+ 
        "Host: "+host+"\r\n"+ 
        "Connection: close\r\n\r\n"); 
        while(readClient.connected()){ 
                readLine = readClient.read(); 
                Serial.print(readLine); 
                readResponse += readLine;     
         } 

         JsonObject& root = jsonBuffer.parseObject(readResponse); 

    if (!root.success()) { 
    Serial.println("parseObject() failed"); 
    return; 
    } 

    } 

void loop() { 
       readConnect(); 

      } 

私のコード。コードを見て、url/ajaxDeviceValueのJSONレスポンスを読み取って文字列で解析する方法を教えてください。

+0

何を試しましたか?いくつかのコードがありますか? – sheplu

+0

[このページ](https://www.arduino.cc/en/Tutorial.WiFi101WeatherAudioNotifier)は、ArduinoJsonライブラリを使用してArduinoを解析するjsonのチュートリアルです。 –

+0

"このコードスニペットを「Nope、nope and nope」に入力してください。これは "私のためのコードを書く"ではありません... – dda

答えて

0

私は多くのヒットと裁判の後に解決策を見つけました。私はWiFiClientを読んでいました.Jsonのレスポンスでごみの価値はほとんどありませんでした。ごみの値ArduinoJsonライブラリは解析できませんでした。私は応答を読み取るためにHttpClientを使いました。そして、ArduinoJsonが解析することができる明確なJsonを返しています。そして現在のコードは正常に動作しています。