Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By hypercoffee
#53961 Hello, I am trying to use the web client example in the Arduino IDE (1.6.5) to get a ESP8266 to read a single JSON entry from data.sparkfun.com.

I need to filter the response that I currently get and save it to a variable.

Code: Select all#include <ESP8266WiFi.h>

const char* ssid     = "Wi-Fi Network";
const char* password = "Password";

const char* host = "data.sparkfun.com";
const char* streamId   = "EJ4x0azK3pcOAbpxAMjM";

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  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());
}

void loop() {
  Serial.print("connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/output/";
  url += streamId;
  url += ".json?limit=1";

  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");

  // Read all the lines of the reply from server and print them to Serial
  // Response is shown between the lines
  Serial.println("_____________________________");
 
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println("_____________________________");

  Serial.println();
  Serial.println("closing connection");

  // Wait 5 seconds, then repeat
  delay(5000);
}


This will give me the following response:
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-Requested-With
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 28 Aug 2016 08:00:00 GMT
Connection: close
Set-Cookie: SERVERID=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/
Cache-control: private

56
[{"tanklevel_cm":"68","tanklevel_percent":"90","timestamp":"2016-08-28T07:59:33.751Z"}
1
]
0



All I need is the information within the brackets { }. Specifically all I want is the "tanklevel_percent" value. Can anyone give me any ideas on how to accomplish this?
User avatar
By hypercoffee
#54042 I've solved it. I am using the ArduinoJSON library along with built in string functions. Code is below.

https://github.com/bblanchon/ArduinoJson

Code: Select all  String line = "";
  while(client.available()){
    line = client.readString();
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");

  int firstbracket = line.indexOf('{');
  int secondbracket = line.indexOf('}', firstbracket + 1);
  line.remove(secondbracket+1);
  line.remove(0,firstbracket);
  char json[512];
//  json = line;
  Serial.println(line);

//-------------------------------
  StaticJsonBuffer<200> jsonBuffer; //JSON
  // Root of the object tree.
  //
  // It's a reference to the JsonObject, the actual bytes are inside the
  // JsonBuffer with all the other nodes of the object tree.
  // Memory is freed when jsonBuffer goes out of scope.
  JsonObject& root = jsonBuffer.parseObject(line);

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

  // Fetch values.
  //
  // Most of the time, you can rely on the implicit casts.
  // In other case, you can do root["time"].as<long>();
  int tanklevel_cm = root["tanklevel_cm"];
  int tanklevel_percent = root["tanklevel_percent"];

  // Print values.
  Serial.println(tanklevel_cm);
  Serial.println(tanklevel_percent);