Esp32 json kodi

Hello everyone
Can anyone help me with my problem?
I want to get information from KODI (coreelec) using JSON and read it over wifi (ESP32) so I can display it on the LCD. I have the basic code in python but I don't know how to do it in arduino.

import time
import requests
import json


def main():
    base_url = "http://192.168.0.150:8081"
    rpc_url   = base_url + "/jsonrpc"
    headers  = {'content-type': 'application/json'}    


    payload = {
            "jsonrpc": "2.0",        
            "method"  : "XBMC.GetInfoLabels",
            "params"  : {"labels": [ "System.Date" ]},
            "id"      : 5,
    }
    response = requests.post(rpc_url, data=json.dumps(payload), headers=headers).json()

    print( response ) # output: {'id': 5, 'jsonrpc': '2.0', 'result': {'System.Date': '11. marca 2025'}}
    
    info = response['result']
    
#    print (info [ 'System.Date' ]) # output:  11. marca 2025
    
    
while True:
    main()
    time.sleep(1) 

This is the link I want to extract from. No matter what I do, no output. I just need to point to a specific library and a way to extract, in this case System.Date.
I'm not asking anyone to write me the whole code.

http://192.168.0.150:8081/jsonrpc?request={"jsonrpc":"2.0","method":"XBMC.GetInfoLabels","params":{"labels":["System.Date"]},"id":5}

This is the output in a web browser.

{"id":5,"jsonrpc":"2.0","result":{"System.Date":"12. marca 2025"}}

Post the code that you've tried.

I tried it differently and I got an output.

{"id":5,"jsonrpc":"2.0","result":{"System.Date":"12. marca 2025"}}

Now I need to clean out the exit. Remove everything that doesn't belong there.
The next step should be to use one link to get multiple different data and then just select them.
"VideoPlayer.ChannelName",
"System.Date"
and so on.

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h> //JSON


#define WIFI_SSID     "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"

void setup()
    {
      Serial.begin(115200);
      Serial.println();

      WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
      Serial.print("Connecting to Wi-Fi");

      while (WiFi.status() != WL_CONNECTED)
           {
             Serial.print(".");
             delay(300);
           }

      Serial.println();
      Serial.print("Connected with IP: ");
      Serial.println(WiFi.localIP());
      Serial.println();
    }

void loop()
    {
      if ((WiFi.status() == WL_CONNECTED))
        {
          HTTPClient http; //Instanz von HTTPClient starten
              
          http.begin("http://192.168.0.150:8081/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22XBMC.GetInfoLabels%22,%22params%22:{%22labels%22:[%22System.Date%22]},%22id%22:5}");
    
          int httpCode = http.GET(); //Antwort des Servers abrufen
    
          if (httpCode == 200)
            {
              String payload = http.getString(); //Daten in eine Variable speichern     
              Serial.println(payload);
            }
        }
      delay(1000);
  
   }

Done.
Now just a few small adjustments for Display.


// Kodi JSON Info Labels                                 by:Jojo

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>


#define WIFI_SSID     "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"

void setup()
    {
      Serial.begin(115200);
      Serial.println();

      WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
      Serial.print("Connecting to Wi-Fi");

      while (WiFi.status() != WL_CONNECTED)
           {
             Serial.print(".");
             delay(300);
           }

      Serial.println();
      Serial.print("Connected with IP: ");
      Serial.println(WiFi.localIP());
      Serial.println();
    }

  String inRaw;
  int httpCode;
 
void loop()
    {
      if ((WiFi.status() == WL_CONNECTED))
        {
          HTTPClient http;
          
          http.begin("http://192.168.0.150:8081/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22XBMC.GetInfoLabels%22,%22params%22:{%22labels%22:[%22System.Date%22,%22VideoPlayer.ChannelName%22,%22VideoPlayer.Title%22,%22VideoPlayer.Year%22,%22PVR.EpgEventDuration%22,%22PVR.EpgEventElapsedTime%22,%22PVR.EpgEventFinishTime%22]},%22id%22:5}"); 
          httpCode = http.GET();
          inRaw = http.getString();


//---- Deserialization + Filter -----

          StaticJsonDocument<0> filter;
          filter.set(true);

          StaticJsonDocument<512> doc;

          DeserializationError error = deserializeJson(doc, inRaw, DeserializationOption::Filter(filter));

          if (error)
            {
              Serial.print("deserializeJson() failed: ");
              Serial.println(error.c_str());
              return;
            }

          int id = doc["id"]; // 5
          const char* jsonrpc = doc["jsonrpc"]; // "2.0"

          JsonObject result = doc["result"];
          const char* result_PVR_EpgEventDuration = result["PVR.EpgEventDuration"];
          const char* result_PVR_EpgEventElapsedTime = result["PVR.EpgEventElapsedTime"];
          const char* result_PVR_EpgEventFinishTime = result["PVR.EpgEventFinishTime"];
          const char* result_System_Date = result["System.Date"];
          const char* result_VideoPlayer_ChannelName = result["VideoPlayer.ChannelName"];
          const char* result_VideoPlayer_Title = result["VideoPlayer.Title"];
          const char* result_VideoPlayer_Year = result["VideoPlayer.Year"];


          Serial.println(result_PVR_EpgEventDuration);
          Serial.println(result_PVR_EpgEventElapsedTime);
          Serial.println(result_PVR_EpgEventFinishTime);
          Serial.println(result_System_Date);
          Serial.println(result_VideoPlayer_ChannelName);
          Serial.println(result_VideoPlayer_Title);
          Serial.println(result_VideoPlayer_Year);

        }

      delay(1000);
  
   }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.