ArduinoJson + WLAN ...... ich bin am verzweifeln

Moin,

seit 3-4 Monaten knobel ich immer wieder an diesem blöden Problem rum und schaffe es einfach nicht die Nuss zu knacken :frowning:

Ich möchte auf einem ESP8266 ein JSON von einem Server einlesen und parsen und dann auf dem seriellen Monitor ausgeben.
Dazu hab ich mir die folgende Libraries geholt und eingebaut:
ArduinoJson
ESP8266WiFi
WiFiClient
WiFiUdp

Dann hab ich mir die Beispiele angeschaut.
Ich verstehe zwar nicht jede Zeile, aber einzeln funktioniert alles.
Dann habe ich damit begonnen alles zusammenzuwürfeln und das zu löschen was ich nicht brauche.
Die einzelnen Funktionen habe ich für mich mit Kommentarzeilen optisch deutlicher voneinander getrennt, damit ich schneller/besser erkennen kann wo die einzelnen Funktionen beginnen und enden.
Soweit so gut.
In dem jetzigen Code wird ein JSON vom Server für mich sehr unverständlich geparst, das funktioniert aber einwandfrei.
Ebenso wird ein weiteres JSON, welches zum Testen im Code definiert ist ( const char* json = "{"version":"0.3","data":{"tuples":[[1508690222959,10.25,1]],"uuid":"05ce3700-97c7-11e6-acf4-754d9577033c","from":1508690177219,"to":1508690222959,"min":[1508690222959,10.25],"max":[1508690222959,10.25],"average":10.25,"rows":2}}";" ) geparst. Das funktioniert auch wunderbar, und da verstehe ich auch wie das geht.

Ich möchte nun EINFACH nur das JSON vom Server so parsen wie das JSON aus dem Code geparst wird. ...mehr nicht.

Das Parsen des JSONs vom Server ist irgendwie viel zu kompliziert mit diesem "struct".
Das muss weg und durch die viel einfachere zweite Variante ersetzt werden.
Der Code ist noch in der Entwicklungsphase und noch lange nicht vollständig.

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>
WiFiClient client;
const char WiFiSSID[] = "xxxxxxxx";     //### your Router SSID
const char WiFiPSK[]  = "xxxxxxxxxxxxxxx"; //### your Router Password
const char* server = "192.168.xxx.xx";  // server's address
const char* resource = "/middleware.php/channel/xxxxxxxxxxxxxxxxxxxxxx.json";      // http resource Kanalinfo Temperatur AUSSEN
const unsigned long HTTP_TIMEOUT = 10000;  // max respone time from server
const size_t MAX_CONTENT_SIZE = 512;       // max size of the HTTP response
const long interval = 5000;   // Interval zum sequentiellen Aufruf der Werte (milliseconds)
const int AnzahlWerte = 6;
unsigned long lastCall;
int i;
float Wert[7];

// The type of data that we want to extract from the page
struct UserData {
  char entity[32];
  char uuid[32];
  char type[32];
  char color[32];
  char title[32];
};

//********************************************************************************************************************************
bool isConnected(long timeOutSec) {
  timeOutSec = timeOutSec * 1000;  int z = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print(".");
    if (z == timeOutSec / 200) { return false; }
    z++;  }
    return true;}
//********************************************************************************************************************************
bool connect(const char* hostName) {                 // Open connection to the HTTP server
  Serial.print("Connect to ");
  Serial.println(hostName);
  bool ok = client.connect(hostName, 80);
  Serial.println(ok ? "Connected" : "Connection Failed!");
  return ok;}
//********************************************************************************************************************************
bool sendRequest(const char* host, const char* resource) {                  // Send the HTTP GET request to the server
  Serial.print("GET ");
  Serial.println(resource);
  client.print("GET ");
  client.print(resource);
  client.println(" HTTP/1.0");
  client.print("Host: ");
  client.println(host);
  client.println("Connection: close");
  client.println();
  return true;}
//********************************************************************************************************************************
bool skipResponseHeaders() {                  // Skip HTTP headers so that we are at the beginning of the response's body
  // HTTP headers end with an empty line
  char endOfHeaders[] = "\r\n\r\n";
  client.setTimeout(HTTP_TIMEOUT);
  bool ok = client.find(endOfHeaders);
  if (!ok) {
    Serial.println("No response or invalid response!");
  }
  return ok;}
//********************************************************************************************************************************
bool readReponseContent(struct UserData* userData) {
  // Compute optimal size of the JSON buffer according to what we need to parse.
  // See https://bblanchon.github.io/ArduinoJson/assistant/
  const size_t BUFFER_SIZE =
      JSON_OBJECT_SIZE(2)    // the root object has 2 elements
      + JSON_OBJECT_SIZE(9)  // the "entity" object has 5 elements
      + MAX_CONTENT_SIZE;    // additional space for strings

  // Allocate a temporary memory pool
  DynamicJsonBuffer jsonBuffer(BUFFER_SIZE);
  JsonObject& root = jsonBuffer.parseObject(client);
  if (!root.success()) {
    Serial.println("JSON parsing failed!");
    return false;
  }

// Here were copy the strings we're interested in
  strcpy(userData->uuid, root["entity"]["uuid"]);
  strcpy(userData->type, root["entity"]["type"]);
  strcpy(userData->color, root["entity"]["color"]);
  strcpy(userData->title, root["entity"]["title"]);
  
// It's not mandatory to make a copy, you could just use the pointers
// Since, they are pointing inside the "content" buffer, so you need to make
// sure it's still in memory when you read the string
  return true;}
//********************************************************************************************************************************
// Print the data extracted from the JSON
void printUserData(const struct UserData* userData) {
  Serial.print("uuid = ");  Serial.println(userData->uuid);
  Serial.print("type = ");  Serial.println(userData->type);
  Serial.print("color = ");  Serial.println(userData->color);
  Serial.print("title = ");  Serial.println(userData->title);
}
//********************************************************************************************************************************
float WertLesen(int i) {
  if (connect(server)) {
    if (sendRequest(server, resource) && skipResponseHeaders()) {
      UserData userData;
      if (readReponseContent(&userData)) {
        printUserData(&userData);       }
    }
  }
Serial.println("Disconnect");
client.stop(); // Close the connection with the HTTP server


const size_t bufferSize = JSON_ARRAY_SIZE(1) + 2*JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(8) + 220;const char* json = "{\"version\":\"0.3\",\"data\":{\"tuples\":[[1508690222959,10.25,1]],\"uuid\":\"05ce3xxxxxxxxxxxxxxxxx77033c\",\"from\":1508690177219,\"to\":1508690222959,\"min\":[1508690222959,10.25],\"max\":[1508690222959,10.25],\"average\":10.25,\"rows\":2}}";
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.parseObject(json);
JsonObject& data = root["data"];
JsonArray& data_tuples0 = data["tuples"][0];
float Temp1 = data_tuples0[1]; // 10.25
return Temp1;
}
//********************************************************************************************************************************
void setup() {
Serial.begin(74880);
WiFi.mode(WIFI_STA);
WiFi.begin(WiFiSSID, WiFiPSK);
if (isConnected(30)) {
  Serial.println(F("WLAN läuft"));
  }
}
//********************************************************************************************************************************
void loop() {
     Wert[1]=WertLesen(1);
     Serial.print("Temperatur ");
     Serial.print(": ");
     Serial.println(Wert[1]);
  }

Nun scheitert es daran, dass ich ein paar Zeilen Code nicht nachvollziehen kann und nicht verstehe.

Ich scheitere an den Zeilen:
if (connect(server)) {
if (sendRequest(server, resource) && skipResponseHeaders()) {
UserData userData;
if (readReponseContent(&userData)) {
printUserData(&userData); }
}
}

Ich verstehe überhaupt nicht was das tut.
Irgendwie wird dort das JSON vom Server in ein UserData struct (was auch immer das ist) geschrieben....vermute ich.
Ich möchte aber, das das JSON vom Server in die Variable "json" geschrieben wird. Das parsen der Variable "json" funktioniert ja jetzt auch.
Der Rest funktioniert glaubich.

Vielleicht kann mir ja jemand helfen diese Nuss zu knacken?

Danke und lieben Gruß,
Chris