squix78 / json-streaming-parser

Arduino library for parsing potentially huge json streams on devices with scarce memory
MIT License
205 stars 88 forks source link

ESP8266 WiFi Sleep/wake loop & >1st parsing issue #10

Closed essenemari closed 7 years ago

essenemari commented 7 years ago

Hello squix78, First, thank You a lot this great work. I am trying to apply your library into my home project. I would like to periodically read a specific value from the EasyIoT server. Using ESP8266 with WiFi library.

As I can see, the parsing process runs only once as a very first. Then, on the second and next loops, it is not parsing anything, however, as I can see, char c is read and can be printed on Serial Monitor.

Tried to stop clients but it did not help. Could you, please, take a look and provide any hint how to solve that?

ExampleParser.cpp - without modifications (yet).

void switch1RpiCheck()  {
  parser.setListener(&listener);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  while (!rPiClient.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
    Serial.println("connection failed");
  }
  Serial.println("Debug1: Connected to EasyIoT");
  String url = "";
  url = "/esp8266/4";

  //Serial.print("POST data to URL: ");
  //Serial.println(url);

  rPiClient.print(String("GET ") + url + " HTTP/1.1\r\n" +
                  "Host: " + EIOT_IP_ADDRESS + "\r\n" +
                  "Connection: close\r\n" +
                  "Authorization: Basic " + unameenc + " \r\n" +
                  "Content-Length: 0\r\n" +
                  "\r\n"
                 );

  boolean isBody = false;
  char c;
  int size = 0;
  while (rPiClient.connected()) {
    while ((size = rPiClient.available()) > 0) {
      c = rPiClient.read();
      //Serial.println(c);
      if (c == '{' || c == '[') {
        isBody = true;
        //Serial.println("Debug2: isBody=true");
      }
      if (isBody) {
        //Serial.println(c);
        Serial.println("Debug3: parsing(c)");
        parser.parse(c);
      }
    }
  }
parse.reset(); //ATTENTION - SOLUTION FOUND: Adding this line fixed the issue.
};

Thank You in advance! Best regards, Artur

essenemari commented 7 years ago

Hello, Sorry for bothering you with that case. It has been fixed by adding parse.reset(); function. Thanks! Artur