Help With Json and Oled

Jonny190
Posts: 1
Joined: Wed Aug 09, 2017 11:08 am

Help With Json and Oled

Postby Jonny190 » Wed Aug 09, 2017 11:12 am

Hi Guys

Having issues parsing JSON in the below scetch:

Code: Select all

#include <Arduino.h>

#include <WiFi.h>
#include <WiFiMulti.h>

#include <HTTPClient.h>

#include <ArduinoJson.h>

#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`

#define USE_SERIAL Serial

SSD1306  display(0x3c, 5, 4);

WiFiMulti wifiMulti;

const char* ca =  
"-----BEGIN CERTIFICATE-----\n"   
"MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n"   
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n"   
"DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n"   
"SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n"   
"GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n"   
"AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n"   
"q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n"   
"SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n"   
"Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n"   
"a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n"   
"/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n"   
"AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n"   
"CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n"   
"bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n"   
"c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n"   
"VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n"   
"ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n"   
"MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n"   
"Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n"   
"AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n"   
"uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n"   
"wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n"   
"X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n"   
"PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n"   
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n"   
"-----END CERTIFICATE-----\n";

void setup() {
    display.init();
    
    display.flipScreenVertically();
    display.setFont(ArialMT_Plain_10);
    
    USE_SERIAL.begin(115200);

    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }

    wifiMulti.addAP("SSID", "PSK");

}

void loop() {
    // Initialising the UI will init the display too.
     display.clear();

    // wait for WiFi connection
    if((wifiMulti.run() == WL_CONNECTED)) {

        HTTPClient http;

        USE_SERIAL.print("[HTTP] begin...\n");
        // configure traged server and url
        http.begin("https://www.howsmyssl.com/a/check", ca); //HTTPS
        USE_SERIAL.print("[HTTP] GET...\n");
        // start connection and send HTTP header
        int httpCode = http.GET();

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                
               StaticJsonBuffer<200> jsonBuffer;

               JsonObject& root = jsonBuffer.parseObject(payload);

               const char* ephemeral_keys_supported = root["ephemeral_keys_supported"];
               
               display.setFont(ArialMT_Plain_10);
               display.setTextAlignment(TEXT_ALIGN_LEFT);
               display.drawStringMaxWidth(0, 0, 128, ephemeral_keys_supported );

               // Test if parsing succeeds.
                 if (!root.success()) {
                   Serial.println("parseObject() failed");
                   USE_SERIAL.println(payload);
                   return;
                 }
               
                USE_SERIAL.println(ephemeral_keys_supported);
                            
           // write the buffer to the display
           display.display();
            }
        } else {
            USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
    }

    delay(10000);
}

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Help With Json and Oled

Postby tele_player » Wed Aug 09, 2017 8:26 pm

JsonBuffer is too small to parse payload.

gdsports
Posts: 15
Joined: Wed Aug 02, 2017 12:17 am

Re: Help With Json and Oled

Postby gdsports » Fri Aug 11, 2017 12:02 am

ArduinoJson Assistant
How to use ArduinoJson?
How to compute the JsonBuffer size?
Paste your JSON in the box and you'll know...

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

Generates Arduino code to parse the JSON you paste into the input box.

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Help With Json and Oled

Postby tele_player » Sat Aug 12, 2017 10:38 pm

Yes, that can be useful, for an estimate.

The two problems in the original post are the use of StaticJsonBuffer and too small size. Unfortunately, the response from the server is too large to use StaticJsonBuffer, it overflows the stack. The server returns about 6800 bytes, the Json buffer needs to be bigger than that, exactly how much bigger depends on the actual content in the response.

Using DynamicJsonBuffer, you don't need to know the size, and in many or most real-world cases, you can't know the size. DynamicJsonBuffer will almost always consume more heap than is actually needed, so it's especially important to make sure it goes out of scope, so its destructor will free the memory.

StaticJsonBuffer is great for smaller Json strings, of known size.

Who is online

Users browsing this forum: No registered users and 113 guests