Hi,
i'm trying to decode some information from an Octopus energy API. Board is
WEMOS D1.
this one
I can get the data back , but cannot decode the json file. This is all a new area to me !!
I'm using Arduinojson , and the on line assistant to porduce the code and the document size ( which it gives as 256).
My full code is attached and also the output ( Ive taken the IP and connection data out) is below. I've tried increasing the doc size ( to 512) but I still get the no memory error:
Any ideas ?
WiFi connected
IP address:
XXXXXXXXXXXXXXXXXXX
reply was:
==========
{"count":51,"next":null,"previous":null,"results":[{"value_exc_vat":4.0,"value_inc_vat":4.2,"valid_from":"2023-07-02T01:00:00Z","valid_to":"2023-07-02T01:30:00Z","payment_method":null},{"value_exc_vat":4.0,"value_inc_vat":4.2,"valid_from":"2023-07-02T00:30:00Z","valid_to":"2023-07-02T01:00:00Z","payment_method":null},{"value_exc_vat":0.6,"value_inc_vat":0.63,"valid_from":"2023-07-02T00:00:00Z","valid_to":"2023-07-02T00:30:00Z","payment_method":null},{"value_exc_vat":-1.2,"value_inc_vat":-1.26,"valid_from":"2023-07-01T23:30:00Z","valid_to":"2023-07-02T00:00:00Z","payment_method":null},{"value_exc_vat":0.94,"value_inc_vat":0.987,"valid_from":"2023-07-01T23:00:00Z","valid_to":"2023-07-01T23:30:00Z","payment_method":null},{"value_exc_vat":-2.0,"value_inc_vat":-2.1,"valid_from":"2023-07-01T22:30:00Z","valid_to":"2023-07-01T23:00:00Z","payment_method":null},{"value_exc_vat":8.98,"value_inc_vat":9.429,"valid_from":"2023-07-01T22:00:00Z","valid_to":"2023-07-01T22:30:00Z","payment_method":null},{"value_exc_vat":13.26,"value_inc_vat":13.923,"valid_from":"2023-07-01T21:30:00Z","valid_to":"2023-07-01T22:00:00Z","payment_method":null},{"value_exc_vat":19.2,"value_inc_vat":20.16,"valid_from":"2023-07-01T21:00:00Z","valid_to":"2023-07-01T21:30:00Z","payment_method":null},{"value_exc_vat":15.68,"value_inc_vat":16.464,"valid_from":"2023-07-01T20:30:00Z","valid_to":"2023-07-01T21:00:00Z","payment_method":null},{"value_exc_vat":16.0,"value_inc_vat":16.8,"valid_from":"2023-07-01T20:00:00Z","valid_to":"2023-07-01T20:30:00Z","payment_method":null},{"value_exc_vat":15.52,"value_inc_vat":16.296,"valid_from":"2023-07-01T19:30:00Z","valid_to":"2023-07-01T20:00:00Z","payment_method":null},{"value_exc_vat":17.18,"value_inc_vat":18.039,"valid_from":"2023-07-01T19:00:00Z","valid_to":"2023-07-01T19:30:00Z","payment_method":null},{"value_exc_vat":15.3,"value_inc_vat":16.065,"valid_from":"2023-07-01T18:30:00Z","valid_to":"2023-07-01T19:00:00Z","payment_method":null},{"value_exc_vat":15.3,"value_inc_vat":16.065,"valid_from":"2023-07-01T18:00:00Z","valid_to":"2023-07-01T18:30:00Z","payment_method":null},{"value_exc_vat":25.8,"value_inc_vat":27.09,"valid_from":"2023-07-01T17:30:00Z","valid_to":"2023-07-01T18:00:00Z","payment_method":null},{"value_exc_vat":22.88,"value_inc_vat":24.024,"valid_from":"2023-07-01T17:00:00Z","valid_to":"2023-07-01T17:30:00Z","payment_method":null},{"value_exc_vat":21.56,"value_inc_vat":22.638,"valid_from":"2023-07-01T16:30:00Z","valid_to":"2023-07-01T17:00:00Z","payment_method":null},{"value_exc_vat":16.56,"value_inc_vat":17.388,"valid_from":"2023-07-01T16:00:00Z","valid_to":"2023-07-01T16:30:00Z","payment_method":null},{"value_exc_vat":15.8,"value_inc_vat":16.59,"valid_from":"2023-07-01T15:30:00Z","valid_to":"2023-07-01T16:00:00Z","payment_method":null},{"value_exc_vat":11.0,"value_inc_vat":11.55,"valid_from":"2023-07-01T15:00:00Z","valid_to":"2023-07-01T15:30:00Z","payment_method":null},{"value_exc_vat":0.5,"value_inc_vat":0.525,"valid_from":"2023-07-01T14:30:00Z","valid_to":"2023-07-01T15:00:00Z","payment_method":null},{"value_exc_vat":0.0,"value_inc_vat":0.0,"valid_from":"2023-07-01T14:00:00Z","valid_to":"2023-07-01T14:30:00Z","payment_method":null},{"value_exc_vat":-1.0,"value_inc_vat":-1.05,"valid_from":"2023-07-01T13:30:00Z","valid_to":"2023-07-01T14:00:00Z","payment_method":null},{"value_exc_vat":-0.32,"value_inc_vat":-0.336,"valid_from":"2023-07-01T13:00:00Z","valid_to":"2023-07-01T13:30:00Z","payment_method":null},{"value_exc_vat":-1.0,"value_inc_vat":-1.05,"valid_from":"2023-07-01T12:30:00Z","valid_to":"2023-07-01T13:00:00Z","payment_method":null},{"value_exc_vat":0.2,"value_inc_vat":0.21,"valid_from":"2023-07-01T12:00:00Z","valid_to":"2023-07-01T12:30:00Z","payment_method":null},{"value_exc_vat":-1.0,"value_inc_vat":-1.05,"valid_from":"2023-07-01T11:30:00Z","valid_to":"2023-07-01T12:00:00Z","payment_method":null},{"value_exc_vat":0.8,"value_inc_vat":0.84,"valid_from":"2023-07-01T11:00:00Z","valid_to":"2023-07-01T11:30:00Z","payment_method":null},{"value_exc_vat":-0.44,"value_inc_vat":-0.462,"valid_from":"2023-07-01T10:30:00Z","valid_to":"2023-07-01T11:00:00Z","payment_method":null},{"value_exc_vat":0.12,"value_inc_vat":0.126,"valid_from":"2023-07-01T10:00:00Z","valid_to":"2023-07-01T10:30:00Z","payment_method":null},{"value_exc_vat":-0.06,"value_inc_vat":-0.063,"valid_from":"2023-07-01T09:30:00Z","valid_to":"2023-07-01T10:00:00Z","payment_method":null},{"value_exc_vat":4.0,"value_inc_vat":4.2,"valid_from":"2023-07-01T09:00:00Z","valid_to":"2023-07-01T09:30:00Z","payment_method":null},{"value_exc_vat":6.4,"value_inc_vat":6.72,"valid_from":"2023-07-01T08:30:00Z","valid_to":"2023-07-01T09:00:00Z","payment_method":null},{"value_exc_vat":7.48,"value_inc_vat":7.854,"valid_from":"2023-07-01T08:00:00Z","valid_to":"2023-07-01T08:30:00Z","payment_method":null},{"value_exc_vat":10.44,"value_inc_vat":10.962,"valid_from":"2023-07-01T07:30:00Z","valid_to":"2023-07-01T08:00:00Z","payment_method":null},{"value_exc_vat":7.86,"value_inc_vat":8.253,"valid_from":"2023-07-01T07:00:00Z","valid_to":"2023-07-01T07:30:00Z","payment_method":null},{"value_exc_vat":11.56,"value_inc_vat":12.138,"valid_from":"2023-07-01T06:30:00Z","valid_to":"2023-07-01T07:00:00Z","payment_method":null},{"value_exc_vat":7.59,"value_inc_vat":7.9695,"valid_from":"2023-07-01T06:00:00Z","valid_to":"2023-07-01T06:30:00Z","payment_method":null},{"value_exc_vat":8.96,"value_inc_vat":9.408,"valid_from":"2023-07-01T05:30:00Z","valid_to":"2023-07-01T06:00:00Z","payment_method":null},{"value_exc_vat":2.4,"value_inc_vat":2.52,"valid_from":"2023-07-01T05:00:00Z","valid_to":"2023-07-01T05:30:00Z","payment_method":null},{"value_exc_vat":7.6,"value_inc_vat":7.98,"valid_from":"2023-07-01T04:30:00Z","valid_to":"2023-07-01T05:00:00Z","payment_method":null},{"value_exc_vat":6.0,"value_inc_vat":6.3,"valid_from":"2023-07-01T04:00:00Z","valid_to":"2023-07-01T04:30:00Z","payment_method":null},{"value_exc_vat":1.84,"value_inc_vat":1.932,"valid_from":"2023-07-01T03:30:00Z","valid_to":"2023-07-01T04:00:00Z","payment_method":null},{"value_exc_vat":4.56,"value_inc_vat":4.788,"valid_from":"2023-07-01T03:00:00Z","valid_to":"2023-07-01T03:30:00Z","payment_method":null},{"value_exc_vat":3.66,"value_inc_vat":3.843,"valid_from":"2023-07-01T02:30:00Z","valid_to":"2023-07-01T03:00:00Z","payment_method":null},{"value_exc_vat":5.0,"value_inc_vat":5.25,"valid_from":"2023-07-01T02:00:00Z","valid_to":"2023-07-01T02:30:00Z","payment_method":null},{"value_exc_vat":3.61,"value_inc_vat":3.7905,"valid_from":"2023-07-01T01:30:00Z","valid_to":"2023-07-01T02:00:00Z","payment_method":null},{"value_exc_vat":5.6,"value_inc_vat":5.88,"valid_from":"2023-07-01T01:00:00Z","valid_to":"2023-07-01T01:30:00Z","payment_method":null},{"value_exc_vat":9.0,"value_inc_vat":9.45,"valid_from":"2023-07-01T00:30:00Z","valid_to":"2023-07-01T01:00:00Z","payment_method":null},{"value_exc_vat":13.5,"value_inc_vat":14.175,"valid_from":"2023-07-01T00:00:00Z","valid_to":"2023-07-01T00:30:00Z","payment_method":null}]}
==========
closing connection
deserializeJson() failed: NoMemory
//HTTP_GET you tube download .
// this was tested and works 7/7/23 now trying to make sense of json message.
// using board :LOLIN(WeMos)D1 R1 with Wemos D1 board.
/*******************************************************************
A sample project for making a HTTP/HTTPS GET request on an ESP8266
It will connect to the given request and print the body to
serial monitor
Parts:
D1 Mini ESP8266 * - http://s.click.aliexpress.com/e/uzFUnIe
* * = Affilate
If you find what I do usefuland would like to support me,
please consider becoming a sponsor on Github
https://github.com/sponsors/witnessmenow/
Some Written by Brian Lough
YouTube: https://www.youtube.com/brianlough
Tindie: https://www.tindie.com/stores/brianlough/
Twitter: https://twitter.com/witnessmenow
*******************************************************************/
// ----------------------------
// Standard Libraries
// ----------------------------
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <strings.h>
#include <ArduinoJson.h>
char ssid[] = "XXXXXXXXXXXXX"; // network SSID (name)
char password[] = "XXXXXXXXXXXX"; // network key
//For HTTPS requests
WiFiClientSecure client;
// Just the base of the URL you want to connect to
#define TEST_HOST "api.octopus.energy"
// OPTIONAL - The finferprint of the site you want to connect to.
#define TEST_HOST_FINGERPRINT "4F 01 5C 5F CC E5 31 35 83 91 D4 69 90 9A 52 97 00 FE 45 69"
// The finger print will change every few months.
void setup()
{
Serial.begin(115200);
// Connect to the WiFI
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println(F("GO!"));
// Attempt to connect to Wifi network:
Serial.print(F("Connecting Wifi: "));
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
IPAddress ip = WiFi.localIP();
Serial.println(ip);
//--------
check the fingerprint
client.setFingerprint(TEST_HOST_FINGERPRINT);
makeHTTPRequest();
}
void makeHTTPRequest()
{
// Opening connection to server (Using 443 as port HTTPS)
if (!client.connect(TEST_HOST, 443))
{
Serial.println(F("Connection failed"));
return;
}
// give the esp a breather
yield();
// Send HTTP request
client.print(F("GET "));
// This is the second half of a request (everything that comes after the base URL)
//client.print(F(" https://api.octopus.energy/v1/products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-C/standard-unit-rates/?period_from=2023-07-01T00:00Z&period_to=2023-07-02T01:29Z&order_by=period")); // %2C == ,
client.print(F(" https://api.octopus.energy/v1/products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-C/standard-unit-rates/?period_from=2023-07-01T00:00Z&period_to=2023-07-02T01:29Z"));
// HTTP 1.0 is ideal as the response wont be chunked
// But some API will return 1.1 regardless, so we need
// to handle both.
client.println(F(" HTTP/1.0"));
//Headers
client.print(F("Host: "));
client.println(TEST_HOST);
client.println(F("Cache-Control: no-cache"));
if (client.println() == 0)
{
Serial.println(F("Failed to send request"));
return;
}
// Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n";
if (!client.find(endOfHeaders))
{
Serial.println(F("Invalid response"));
return;
}
// While the client is still available read each
// byte and print to the serial monitor
Serial.println(F("reply was:"));
Serial.println(F("=========="));
String line;
while (client.available())
//**********************
{
line = client.readStringUntil('\n'); //Read Line by Line
Serial.println(line); //Print response
}
Serial.println(F("=========="));
Serial.println(F("closing connection"));
// Stream& input;
StaticJsonDocument<256> doc;
DeserializationError error = deserializeJson(doc, line);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
for (JsonObject item : doc.as<JsonArray>()) {
float value_exc_vat = item["value_exc_vat"]; // 5, 6.2
float value_inc_vat = item["value_inc_vat"]; // 5.25, 6.51
const char* valid_from = item["valid_from"]; // "2020-05-06T21:30:00Z", "2020-05-06T21:00:00Z"
const char* valid_to = item["valid_to"]; // "2020-05-06T22:00:00Z", "2020-05-06T21:30:00Z"
}
}
void loop()
{
}