Close

Results 1 to 1 of 1
  1. #1
    DF PwNagE beerman's Avatar
    Join Date
    May 2001
    Location
    29 Acacia Road
    Posts
    8,495
    Thanks
    1,093
    Thanked:        1,669
    Karma Level
    974

    Help Parsing JSON Array - Arduino

    I'm trying to create a little arduino monitor for Tautulli (plex-py). I'm pretty much a novice so any help would be appreciated.

    I have managed to work through the example JSON code, port it for my needs and get to this point:


    My problem is if I get more than 2 streams playing it won't parse the JSON. I know my array is the issue but don't have the skills to solve it.

    The problem is the JSON returned from the Tautulli api changes in size and I think this is where the issue lies.

    Here is the JSON with nothing playing.



    Code:
    {"response": {"message": null, "data": {"sessions": [], "stream_count": "0", "total_bandwidth": 0, "stream_count_transcode": 0, "wan_bandwidth": 0, "stream_count_direct_play": 0, "lan_bandwidth": 0, "stream_count_direct_stream": 0}, "result": "success"}}
    The only 2 values I care about are Wan_bandwidth and Stream_count.

    When something is playing it expands the "Sessions" and adds a load of crap I'm not interested in about the show.:



    Code:
    {"response": {"message": null, "data": {"sessions": [{"rating": "", "transcode_width": "", "labels": [], "stream_bitrate": "17845", "bandwidth": "18738", "optimized_version": 0, "video_language": "English", "parent_rating_key": "71416", "rating_key": "71434", "platform_version": "8.1.0", "transcode_hw_decoding": 1, "thumb": "/library/metadata/71434/thumb/1564403900", "title": "Baker's Dozen", "video_codec_level": "41", "tagline": "", "last_viewed_at": "1566442230", "audio_sample_rate": "48000", "user_rating": "", "transcode_height": "", "collections": [], "location": "lan", "transcode_container": "mpegts", "audio_channel_layout": "5.1(side)", "local": 1, "stream_subtitle_format": "", "stream_video_ref_frames": "", "transcode_hw_encode_title": "", "stream_container_decision": "transcode", "audience_rating": "", "full_title": "Orange Is the New Black - Baker's Dozen", "ip_address": "", "subtitles": 1, "stream_subtitle_language": "English", "channel_stream": 0, "video_bitrate": "11381", "is_allow_sync": 1, "stream_video_bitrate": "17071", "summary": "Neri takes Piper on a wilderness retreat. Taystee tutors Pennsatucky. Suzanne tends to the chicken coop. Nicky has a difficult conversation with Red.", "stream_audio_decision": "transcode", "aspect_ratio": "1.78", "audio_bitrate_mode": "", "transcode_hw_decode_title": "Windows (DXVA2)", "stream_audio_channel_layout": "5.1", "deleted_user": 0, "library_name": "TV Shows", "art": "/library/metadata/67107/art/1564403901", "stream_video_resolution": "1080", "video_profile": "high", "sort_title": "", "stream_video_codec_level": "", "stream_video_height": "1080", "year": "2019", "stream_duration": "3658400", "stream_audio_channels": "6", "video_language_code": "eng", "transcode_key": "/transcode/sessions/37cef9d076453e4f-com-plexapp-android", "transcode_throttled": 0, "container": "mkv", "stream_audio_bitrate": "774", "user": "", "selected": 1, "product_version": "7.19.2.11789", "subtitle_location": "embedded", "transcode_hw_requested": 1, "video_height": "1080", "state": "playing", "is_restricted": 0, "email": "", "stream_container": "mpegts", "transcode_speed": "2.8", "video_bit_depth": "8", "stream_audio_sample_rate": "", "grandparent_title": "Orange Is the New Black", "studio": "Netflix", "transcode_decision": "transcode", "video_width": "1920", "bitrate": "12023", "machine_id": "", "originally_available_at": "2019-07-26", "video_frame_rate": "23.976", "synced_version_profile": "", "friendly_name": "", "audio_profile": "", "optimized_version_title": "", "platform_name": "android", "stream_video_language": "English", "keep_history": 1, "stream_audio_codec": "aac", "stream_video_codec": "h264", "grandparent_thumb": "/library/metadata/67107/thumb/1564403901", "synced_version": 0, "transcode_hw_decode": "dxva2", "secure": 1, "user_thumb": "", "stream_video_width": "1920", "height": "1080", "stream_subtitle_decision": "burn", "audio_codec": "eac3", "parent_title": "Season 7", "guid": "com.plexapp.agents.thetvdb://264586/7/8?lang=en", "audio_language_code": "eng", "transcode_video_codec": "h264", "transcode_audio_codec": "aac", "stream_video_decision": "transcode", "user_id": , "platform": "Android", "transcode_hw_full_pipeline": 0, "throttled": "0", "quality_profile": "Original", "width": "1920", "live": 0, "stream_subtitle_forced": 1, "media_type": "episode", "video_resolution": "1080", "stream_subtitle_location": "segments-av", "do_notify": 0, "video_ref_frames": "4", "stream_subtitle_language_code": "eng", "audio_channels": "6", "stream_audio_language_code": "eng", "optimized_version_profile": "", "duration": "3658400", "rating_image": "", "is_home_user": 0, "is_admin": 1, "ip_address_public": "", "allow_guest": 0, "transcode_audio_channels": "6", "stream_audio_channel_layout_": "", "media_index": "8", "stream_video_framerate": "24p", "transcode_hw_encode": "", "grandparent_rating_key": "67107", "original_title": "", "added_at": "1564321907", "banner": "/library/metadata/67107/banner/1564403901", "bif_thumb": "/library/parts/282004/indexes/sd/0", "parent_media_index": "7", "live_uuid": "", "audio_language": "English", "stream_audio_bitrate_mode": "vbr", "username": "", "subtitle_decision": "burn", "children_count": "", "updated_at": "1564403900", "player": "ONEPLUS A5010", "subtitle_format": "", "file": "\\\\192.168.43.251\\tvshows\\Orange Is the New Black\\Season 7\\Orange Is the New Black - S07E08 - Baker's Dozen WEBDL-1080p.mkv", "file_size": "5499931837", "session_key": "72", "id": "585468", "subtitle_container": "", "genres": ["Comedy", "Crime", "Drama"], "stream_video_language_code": "eng", "relayed": 0, "indexes": 1, "video_decision": "transcode", "stream_audio_language": "English", "writers": [], "actors": ["Taylor Schilling", "Laura Prepon", "Yael Stone", "Uzo Aduba", "Selenis Leyva", "Laverne Cox", "Danielle Brooks", "Dascha Polanco", "Elizabeth Rodriguez", "Matt McGorry", "Pablo Schreiber", "Kate Mulgrew", "Taryn Manning", "Natasha Lyonne", "Constance Shulman", "Kimiko Glenn", "Michelle Hurst", "Barbara Rosenblat", "Beth Fowler", "Michael Harney", "Nick Sandow", "Lorraine Toussaint", "Adrienne C. Moore", "Vicky Jeudy", "Annie Golden", "Maria Dizzia", "Abigail Savage", "Laura Gómez", "Lea DeLaria", "Diane Guerrero", "Jackie Cruz", "Ruby Rose", "Lori Tan Chinn", "Alysia Reiner", "Madeline Brewer", "Michael Chernus", "Catherine Curtin", "Emma Myles", "Julie Lake", "Joel Marsh Garland", "Judith Roberts", "Alan Aisenberg", "Blair Brown", "Amanda Stephen", "Jolene Purdy", "Samira Wiley", "Evan Hall", "Lin Tucci", "Lauren Lapkus", "Matt Peters", "Dale Soules", "Brad William Henke", "Vicci Martinez", "Amanda Fuller", "Henny Russell", "Mackenzie Phillips", "Josh Segarra", "Shawna Hamic", "Nicholas Webber", "Branden Wellington", "Beth Dover", "Miguel Izaguirre", "Mike Houston"], "progress_percent": "0", "audio_decision": "transcode", "subtitle_forced": 1, "profile": "Android", "product": "Plex for Android (Mobile)", "view_offset": "0", "type": "3", "audience_rating_image": "", "audio_bitrate": "640", "section_id": "3", "stream_subtitle_codec": "", "subtitle_codec": "srt", "video_codec": "h264", "device": "ONEPLUS A5010", "stream_video_bit_depth": "", "video_framerate": "24p", "transcode_hw_encoding": 0, "transcode_protocol": "hls", "shared_libraries": ["5", "2", "1", "3", "6"], "stream_aspect_ratio": "", "content_rating": "TV-MA", "session_id": "37cef9d076453e4f-com-plexapp-android", "directors": [], "parent_thumb": "/library/metadata/71416/thumb/1564403901", "subtitle_language_code": "eng", "transcode_progress": 0, "subtitle_language": "English", "stream_subtitle_container": ""}], "stream_count": "1", "total_bandwidth": 18738, "stream_count_transcode": 1, "wan_bandwidth": 0, "stream_count_direct_play": 0, "lan_bandwidth": 18738, "stream_count_direct_stream": 0}, "result": "success"}}
    Here is my code:
    Code:
    #include <ESP8266WiFi.h>
    #include <WiFiClient.h> 
    #include <ESP8266HTTPClient.h>
    #include <ArduinoJson.h>
    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    
    #define SCREEN_WIDTH 128 // OLED display width, in pixels
    #define SCREEN_HEIGHT 64 // OLED display height, in pixels
    
    
    // Declaration for SSD1306 display connected using software SPI:
    #define OLED_MOSI  D7
    #define OLED_CLK   D5
    #define OLED_DC    D4
    #define OLED_CS    D8
    #define OLED_RESET D0
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
      OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
    
    
    const char* wifiName = "MYSSID";
    const char* wifiPass = "MYPASS";
    
    //Web Server address to read/write from 
    const char *host = "https://SERVERADDRESS/api/v2?apikey=xxxAPIKEYxxx&cmd=get_activity";
    
    void setup() {
      
      Serial.begin(115200);
      delay(10);
    
    // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
      if(!display.begin(SSD1306_SWITCHCAPVCC)) {
        Serial.println(F("SSD1306 allocation failed"));
        for(;;); // Don't proceed, loop forever
      }
    
    
      // Show initial display buffer contents on the screen --
      // the library initializes this with an Adafruit splash screen.
      display.display();
      delay(1000); // Pause for 1 second
    
      // Clear the buffer
      display.clearDisplay();
      Serial.println();
      
      Serial.print("Connecting to ");
      Serial.println(wifiName);
    
      WiFi.begin(wifiName, wifiPass);
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
    
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());   //You can get IP address assigned to ESP
    }
    
    void loop() {
      HTTPClient http;    //Declare object of class HTTPClient
    
      Serial.print("Request Link:");
      Serial.println(host);
      
      http.begin(host);     //Specify request destination
      
      int httpCode = http.GET();            //Send the request
      String payload = http.getString();    //Get the response payload from server
    
      Serial.print("Response Code:"); //200 is OK
      Serial.println(httpCode);   //Print HTTP return code
    
      Serial.print("Returned data from Server:");
      Serial.println(payload);    //Print request response payload
      
      if(httpCode == 200)
      {
        // Allocate JsonBuffer
        // Use arduinojson.org/v5/assistant to compute the capacity.
        const size_t capacity = JSON_ARRAY_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(8) + 16720;
       
        DynamicJsonBuffer jsonBuffer(capacity);
      
       // Parse JSON object
        JsonObject& root = jsonBuffer.parseObject(payload);
        if (!root.success()) {
          Serial.println(F("Parsing failed!"));
          delay(3000);
          return;
        }
    
        // Display data in serial monitor
        Serial.println(F("Response:"));
        Serial.println(root["response"]["data"]["wan_bandwidth"].as<char*>());
        Serial.println(root["response"]["data"]["stream_count"].as<char*>());
        
        //Display data 
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 0);
    display.println("Bandwidth Used");
    display.setCursor(0, 12);
    display.setTextSize(2);
    display.print(root["response"]["data"]["wan_bandwidth"].as<char*>());
    display.println(" kbps");
    display.setCursor(0, 35);
    display.setTextSize(1);
    display.println("No. of Streams");
    display.setCursor(0, 47);
    display.setTextSize(2);
    display.print(root["response"]["data"]["stream_count"].as<char*>());
    
    display.display(); 
    
    delay(1000);
      }
      else
      {
        Serial.println("Error in response");
      }
    
      http.end();  //Close connection
      
      delay(5000);  //GET Data at every 5 seconds
    }

    If I add the output of the JSON to arduinojson.org/v5/assistant when I have 1, 2, 3 or 4 streams playing I get different array sizes. None of them seem to be a one size fits all.

    My question is does anyone know how to create the array that omits the "sessions" section?
    Last edited by beerman; 22nd August 2019 at 05:14 PM.

Social Networking Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •