HTTPS + JSON + OLED

Hi Guys,

I’ve attached my Schetch where I can connect to a HTTPS server and collect the information and display it on the screen but I cannot pass it to any JSON library than the screen. this is currently on an ESP32 board with built in OLED display

Any help greatly appreciated.

Thanks,
Jonny

esp32_Wifi_Https_Load.ino (4.31 KB)

but I cannot pass it to any JSON library than the screen.

What is “it” that you are trying to pass? To what library? What happens when you try?

What IS in payload? Is it really a valid JSON object?

I'm trying to use the ArduinoJson library from GitHub - bblanchon/ArduinoJson: 📟 JSON library for Arduino and embedded C++. Simple and efficient.

ive tried 2 different json pages one being: https://www.howsmyssl.com/a/check

{"given_cipher_suites":["TLS_GREASE_IS_THE_WORD_2A","TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256","TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256","TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256","TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256","TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA","TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA","TLS_RSA_WITH_AES_128_GCM_SHA256","TLS_RSA_WITH_AES_256_GCM_SHA384","TLS_RSA_WITH_AES_128_CBC_SHA","TLS_RSA_WITH_AES_256_CBC_SHA","TLS_RSA_WITH_3DES_EDE_CBC_SHA"],"ephemeral_keys_supported":true,"session_ticket_supported":true,"tls_compression_supported":false,"unknown_cipher_suite_supported":false,"beast_vuln":false,"able_to_detect_n_minus_one_splitting":false,"insecure_cipher_suites":{},"tls_version":"TLS 1.2","rating":"Probably Okay"}

The other being the actual use case for home automation (Homeassistant) where the url returns:

{"attributes": {"friendly_name": "Bedroom Temperature", "unit_of_measurement": "\u00b0C"}, "entity_id": "sensor.bedroom_temperature", "last_changed": "2017-08-10T03:06:25.232929+00:00", "last_updated": "2017-08-10T03:06:25.232929+00:00", "state": "23.8"}

Ive tested both Json outputs and the look valid to online checkers like https://jsonlint.com/

I still don't know what the problem is. I don't know how you got the output that you showed.

I HATE anonymous printing. You learn so much more with a few extra keystrokes.

   Serial.print("payload: [");
   Serial.print(payload);
   Serial.println("]");

Then, we can extract the text between the [ and the ], and determine that it is valid.

Without the delimiters, we can't tell if the payload contains extra white space at the beginning or the end.

Of course, that assumes that what you posted IS the contents of payload AND that the problem involves passing payload to the JSON library's constructor.

"The actual use case" does not have an object named "ephemeral_keys_supported" at the root level.

I've PM'd you the address of the live data

and i've made the changed the serial.print

this now outputs:

payload: [{"attributes": {"friendly_name": "Bedroom Temperature", "unit_of_measurement": "\u00b0C"}, "entity_id": "sensor.bedroom_temperature", "last_changed": "2017-08-10T03:06:25.232929+00:00", "last_updated": "2017-08-10T03:06:25.232929+00:00", "state": "23.8"}]
[HTTP] begin...
[HTTP] GET...

Oh and its querying friendly_name:

JsonObject& root = jsonBuffer.parseObject(payload);

const char* friendly_name = root[“friendly_name”];

display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawStringMaxWidth(0, 0, 128, friendly_name );

// Test if parsing succeeds.
if (!root.success()) {
Serial.println(“parseObject() failed”);
Serial.print(“payload: [”);
Serial.print(payload);
Serial.println("]");
return;
}

USE_SERIAL.println(friendly_name);

The URL that you are getting data from is not that important. What is important is that you tell us what the problem is.

               const char* friendly_name = root["friendly_name"];

"friendly_name" is NOT a root level attribute.

this does the same for state as well, i was just trying all of the options

The examples for the ArduinoJson library warn that any String passed in will need to be copied in order to be parsed. Do you have enough memory for two copies?

The root object has a success() method that the examples use, but you don't. Perhaps a clue might present itself, if you did.

Could be option to do it twice as its running on a ESP32 but i got the https section from the http client example

https://bblanchon.github.io/ArduinoJson/assistant/

Paste your JSON into the assistant and get back Arduino code to parse your JSON.

const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(5) + 240;
DynamicJsonBuffer jsonBuffer(bufferSize);

const char* json = "{\"attributes\":{\"friendly_name\":\"Bedroom Temperature\",\"unit_of_measurement\":\"°C\"},\"entity_id\":\"sensor.bedroom_temperature\",\"last_changed\":\"2017-08-10T03:06:25.232929+00:00\",\"last_updated\":\"2017-08-10T03:06:25.232929+00:00\",\"state\":\"23.8\"}";

JsonObject& root = jsonBuffer.parseObject(json);

const char* attributes_friendly_name = root["attributes"]["friendly_name"]; // "Bedroom Temperature"
const char* attributes_unit_of_measurement = root["attributes"]["unit_of_measurement"]; // "°C"

const char* entity_id = root["entity_id"]; // "sensor.bedroom_temperature"
const char* last_changed = root["last_changed"]; // "2017-08-10T03:06:25.232929+00:00"
const char* last_updated = root["last_updated"]; // "2017-08-10T03:06:25.232929+00:00"
const char* state = root["state"]; // "23.8"