Hi all,
I'm currently working on a project what I thought would be fairly simple, but is already driving me nuts for a week searching all over the web trying to find a solution. It's about how to capture a stream (JSON answer) from an online API and converting it into a variable/array that can then be deserialized (in my case by the ArduinoJSON library). I found multiple questions like mine across various forums remaining unsolved.
Hardware:
Arduino Uno (R3) with an Ethernet shield (W5100) attached to it, hooked up to my router via LAN. Internet access and sending/retrieving data works beautifully. No problems there.
Libraries:
#include <SPI.h>
#include <Ethernet.h>
#include "ArduinoJson.h"
Code:
The Arduino makes an HTTP-request to an online API, which responds with a JSON formatted answer. Example of an actual response:
{
"aral": {
"e5": 1.759,
"e10": 1.709,
"diesel": 1.339
},
"shell": {
"e5": 1.779,
"e10": 1.719,
"diesel": 1.349
}
}
During testing I tracked the APIs JSON response by printing it to the serial monitor using:
while (client.available()) {
char c = 0;
client.readBytes(&c, 1);
Serial.print(c);
}
This works great as the response shows up in full as expected without any problems. However client.readBytes is only able to capture the JSON response by each individual byte/character recieved.
What I would like is to put the full JSON response into a variable (let's say: const char json[] = "*the actual api json response*") so it can then be deserialized using the ArduinoJSON library for further use. I tried many things, of which this was the latest, but it didn't produce any results:
while (client.available()) {
char c = client.read();
readString += c; // store all individual characters into variable "readString"
}
I can get everything to work except for how to capture the JSON data stream sent by the API to the Ethernet shield and put it in some sort of variable/array for further use? I read a lot of ArduinoJSON documentation but the big hiatus remains the question above as the manual does not provide any insight on this. It basically starts from the point of having a filled array already present, but offers no intel on the step before of how to create this array from a JSON response received by the Arduino.
Can someone point me in the right direction on how to proceed?
(All my program is subsequently intended to do is outputting the petrol prices fetched from the API to a display. Extremely basic, nothing fancy. Done it a million times before; just not with a JSON API response like what I have to work with here.. That is actually my first time and I want to figure out how to do it.)
