Go Down

Topic: HTTPS + JSON + OLED (Read 170 times) previous topic - next topic

Jonny190

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

PaulS

Quote
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?
The art of getting good answers lies in asking good questions.

Jonny190

I'm trying to use the ArduinoJson library from https://github.com/bblanchon/ArduinoJson

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

Quote
{"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:
Quote
{"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/




PaulS

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.

Code: [Select]
   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.
The art of getting good answers lies in asking good questions.

Jonny190

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

and i've made the changed the serial.print

this now outputs:

Quote
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...

Jonny190

Oh and its querying friendly_name:

Quote
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);

PaulS

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

Code: [Select]
               const char* friendly_name = root["friendly_name"];
"friendly_name" is NOT a root level attribute.
The art of getting good answers lies in asking good questions.

Jonny190

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

PaulS

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.

The art of getting good answers lies in asking good questions.

Jonny190

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

gdsports

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

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

Code: [Select]
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"

Go Up