Failure of JSON parsing in Arduino and ESP8266

0 votes

I need to read JSON data 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
• 13,490 points
2,814 views

1 answer to this question.

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
• 8,620 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,240 points
6,830 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,240 points
1,213 views
+17 votes
25 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, 2019 in Java by Suresh
• 720 points
253,605 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
3,259 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,190 points
6,994 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,370 points
5,888 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP