Failure of JSON parsing in Arduino and ESP8266

0 votes

I need to read JSON data from here and then I have to parse it. I have to extract an Ajax response from this REST API but I'm kinda stuck here. Kindly help me out.

So, I've given my code below. Do have a look at it and tell me how can i read a JSON response for the url. Also, How can I parse it in a string?

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

            }

Thanks!

Dec 24, 2018 in IoT (Internet of Things) by Shubham
• 12,270 points
130 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I'm assuming it's because of the garbage value, that ArduinoJson library was not able to parse it. 

After a lot of failed efforts, I finally  tried using the HttpClient to read the response.It returned the clear Json which ArduinoJson is able to parse and now the code is working just fine.  

answered Dec 24, 2018 by Upasana
• 7,680 points
can you put your coding here?

Related Questions In IoT (Internet of Things)

0 votes
0 answers
0 votes
1 answer

Faster communication between two ESP8266 in client-server setup

Try following. Server loop void loop() { // ...READ MORE

answered Jul 13, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
519 views
0 votes
1 answer

How to get Unicast, Dns and Gateway Address in UWP?

Try this code Snippet I found here: https://social.msdn.microsoft.com/Forums/en-US/27a8b7a8-8071-4bc1-bbd4-e7c1fc2bd8d7/windows-10-iot-core-how-do-you-create-a-tcp-server-and-client?forum=WindowsIoT ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
84 views
+14 votes
17 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7 in Java by Suresh
• 700 points
52,535 views
0 votes
1 answer

How to convert a JSON String into Object in Java?

We can easily convert a JSON String ...READ MORE

answered Jun 8, 2018 in Java by parth295
• 4,590 points
163 views
0 votes
1 answer

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

You can use: new JSONObject(map); READ MORE

answered Jun 27, 2018 in Java by Akrati
• 3,150 points
1,262 views
0 votes
1 answer

How can we return a JSON object from a Java Servlet?

response.setContentType("application/json"); // Get the printwriter object from response ...READ MORE

answered Jul 6, 2018 in Java by sharth
• 3,310 points
979 views
0 votes
1 answer
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.