[vz-users] JSON parsen auf ESP8266 mit Arduino IDE

Christian Wulff christianwulff at gmx.de
Fri Jul 21 12:05:02 CEST 2017


Hallo Frank,

 

ja das weiss ich, das ist aber nur eine Variablendeklaration. Die stört nicht, oder?

Der Buffer für das JSON ist bereits korrekt an das JSON angepasst.

Aber dann geht’s irgendwie noch nicht weiter L

Wäre gut, wenn das schon mal eine gemacht hat.

 

Any idea?

 

Lieben Gruß,

Chris

 

Von: Frank Richter [mailto:frank.richter83 at gmail.com] 
Gesendet: Freitag, 21. Juli 2017 11:51
An: volkszaehler.org - users
Betreff: Re: [vz-users] JSON parsen auf ESP8266 mit Arduino IDE

 

Hallo Christian,

 

ich hab das auch noch nie gemacht, aber du solltest deinen Code schon irgendwie an die gelieferte JSON-Struktur anpassen. "name" und "company" wirst du hier wohl kaum finden...

 

Grüße

Frank

 

Am 21.07.2017 11:22 schrieb "Christian Wulff" <christianwulff at gmx.de>:

Moin,

 

Ich möchte auf einem kleinen Display mit WLAN Verbindung ein super einfach zu nutzendes Frontend bauen, das man überall platzieren kann und nur einschalten muss.

Das Display kriege ich angesteuert und Datum und Zeit werden bereits per ntp eingelesen und auf dem Display angezeigt.

Nun fehlen noch die Temperaturen auf dem Display.

Die Daten sollen über WLAN per http Request und JSON eingelesen und ausgewertet werden.

HTTP Request und JSON funktioniert, habe ich im Browser getestet und mir die Daten angesehen.

Dann habe ich mir die Bibliothek ArduinoJson installiert und mir das Beispiel JsonHttpClient angesehen. Dies habe ich von Ethernet bereits auf WLAN umgestrickt.

 

Ich glaube der HTTP Request funktioniert bereits, allerdings gibt es irgendein Problem beim parsen. Da komme ich auch nach einiger Recherche leider nicht weiter.

 

Vielleicht kann hier jemand helfen?

 

(Der Code ist noch nicht aufgeräumt, da sind noch Reste vom Beispiel drin, ich glaube aber nicht dass die das Problem verursachen)

 

**************************************************************************************************************************************************************************************

#include <ArduinoJson.h>

#include <ESP8266WiFi.h>

#include <WiFiClient.h>

#include <WiFiUdp.h>

WiFiClient client;

const char WiFiSSID[] = "Chriss 123456";     //### your Router SSID

const char WiFiPSK[]  = "1234567890132654"; //### your Router Password

const char* server = "192.168.178.43";  // server's address

const char* resource = "/middleware.php/channel/003321-9654-146-acf4-754654654.json";      // http resource Kanalinfo Temperatur AUSSEN

const unsigned long BAUD_RATE = 9600;                 // serial connection speed

const unsigned long HTTP_TIMEOUT = 10000;  // max respone time from server

const size_t MAX_CONTENT_SIZE = 512;       // max size of the HTTP response

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;}

 

// The type of data that we want to extract from the page

struct UserData {

  char name[32];

  char company[32];

  char type[32];

  char color[32];

  char title[32];

};

 

// ARDUINO entry point #1: runs once when you press reset or power the board

void setup() {

  initSerial();

  WiFi.mode(WIFI_STA);

  WiFi.begin(WiFiSSID, WiFiPSK);

  if (isConnected(30)) {

    Serial.println(F("WLAN läuft"));     }}

 

// ARDUINO entry point #2: runs over and over again forever

void loop() {

  if (connect(server)) {

    if (sendRequest(server, resource) && skipResponseHeaders()) {

      UserData userData;

      if (readReponseContent(&userData)) {

        printUserData(&userData);

      }

    }

  }

  disconnect();

  wait();

}

 

// Initialize Serial port

void initSerial() {

  Serial.begin(BAUD_RATE);

  while (!Serial) {

    ;  // wait for serial port to initialize

  }

  Serial.println("Serial ready");

}

 

// Open connection to the HTTP server

bool connect(const char* hostName) {

  Serial.print("Connect to ");

  Serial.println(hostName);

  bool ok = client.connect(hostName, 80);

  Serial.println(ok ? "Connected" : "Connection Failed!");

  return ok;

}

 

// Send the HTTP GET request to the server

bool sendRequest(const char* host, const char* resource) {

  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;

}

 

// Skip HTTP headers so that we are at the beginning of the response's body

bool skipResponseHeaders() {

  // 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;

}

 

// Parse the JSON from the input string and extract the interesting values

// Here is the JSON we need to parse

// {

//   "id": 1,

//   "name": "Leanne Graham",

//   "username": "Bret",

//   "email": "Sincere at april.biz",

//   "address": {

//     "street": "Kulas Light",

//     "suite": "Apt. 556",

//     "city": "Gwenborough",

//     "zipcode": "92998-3874",

//     "geo": {

//       "lat": "-37.3159",

//       "lng": "81.1496"

//     }

//   },

//   "phone": "1-770-736-8031 x56442",

//   "website": "hildegard.org",

//   "company": {

//     "name": "Romaguera-Crona",

//     "catchPhrase": "Multi-layered client-server neural-net",

//     "bs": "harness real-time e-markets"

//   }

// }

 

// {

//   "version": "0.3",

//    "entity": {

//      "uuid": "003321-9654-146-acf4-754654654",

//      "type": "temperature",

//      "active": false,

//      "color": "#009933",

//      "fillstyle": 0,

//      "public": true,

//      "style": "lines",

//      "title": "T01 Aussen",

//      "yaxis": "auto"

//    }

// }

 

 

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->name, root["name"]);

  strcpy(userData->company, root["company"]["name"]);

  // 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("Name = ");

  Serial.println(userData->name);

  Serial.print("Company = ");

  Serial.println(userData->company);

}

 

// Close the connection with the HTTP server

void disconnect() {

  Serial.println("Disconnect");

  client.stop();

}

 

// Pause for a 1 minute

void wait() {

  Serial.println("Wait 60 seconds");

  delay(60000);

}

**************************************************************************************************************************************************************************************

 

 

 

Als Fehler kommt da folgendes bei raus:

 

 

 

 

**************************************************************************************************************************************************************************************

Serial ready

...................................WLAN läuft

Connect to 192.168.178.43

Connected

GET /middleware.php/channel/003321-9654-146-acf4-754654654.json

 

Exception (28):

epc1=0x4000bf0e epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

 

ctx: cont 

sp: 3ffef9e0 end: 3ffefca0 offset: 01a0

 

>>>stack>>>

3ffefb80:  3ffe8550 00000000 3fff05a8 402024d5  

3ffefb90:  3ffefba4 3ffeeb28 3ffe0000 3ffefba4  

3ffefba0:  3fffda0a 3ffe8550 3ffeeb28 3fff059c  

3ffefbb0:  00000580 3ffe84d0 3ffeeb28 40201e03  

3ffefbc0:  0a0d0a0d 00000000 3ffeec44 3ffeec70  

3ffefbd0:  3fffdad0 00000000 3ffe836c 40202605  

3ffefbe0:  ffffff00 43ffffff 73697268 39333720  

3ffefbf0:  00000030 00000000 3ffeec44 401004d8  

3ffefc00:  feefeffe 33000001 39303632 33353237  

3ffefc10:  39383534 00000001 3ffe8715 4020391c  

3ffefc20:  401051fc 002266eb 3ffeeb88 3ffeec70  

3ffefc30:  3fffdad0 0000000b 3ffeec44 402032e1  

3ffefc40:  3ffe8714 3ffeec70 40203638 3ffeec80  

3ffefc50:  3ffefddc 0000000a 3ffeec44 4020330c  

3ffefc60:  3fffdad0 00000000 3ffeec44 40203330  

3ffefc70:  3fffdad0 00000000 3ffeeb50 40201d44  

3ffefc80:  feefeffe feefeffe 3ffeec68 40203684  

3ffefc90:  feefeffe feefeffe 3ffeec80 40100718  

<<<stack<<<

**************************************************************************************************************************************************************************************

 

Kann hier vielleicht  jemand weiterhelfen?

 

Lieben Gruß und lieben Dank,

Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://demo.volkszaehler.org/pipermail/volkszaehler-users/attachments/20170721/99814fc0/attachment-0001.html>


More information about the volkszaehler-users mailing list