Skip to main content
Mod Moved Comments To Chat
added 1700 characters in body
Source Link
static char buff[512] = { 0 };
while (http.connected()) {
  size_t size = client->available();
  if (size) {
    int c = client->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
    //if i dont uncomment this, program is able to reach next if statement: 
    //Serial.write(buff, c); Serial.println(); Serial.println();
    if (char * pch = strstr (buff, "str")) {
      Serial.println("found!");
      //program never gets here
    }
  }
}
#include <ESP8266WiFi.h>


#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>

#define WIFI_SSID "ssid"
#define WIFI_PASSWORD "password"

void setup() {
  Serial.begin(9600);
  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();
  ////////////////////////////////////////////

  std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
  client->setInsecure();
  HTTPClient http;

  http.begin(*client, "https://jsonplaceholder.typicode.com/photos");
  http.addHeader("Accept", "*/*");


  int httpCode = http.GET();
  Serial.print("HTTP CODE: "); Serial.println(httpCode);

  //reading response with buffer
  static char buff[128] = { 0 };
  while (http.connected() ) {
    size_t size = client->available();
    if (size) {
      int c = client->readBytes(buff, ((size > sizeof(buff) ) ? sizeof(buff) : size));
      //if i uncomment this next line, everything is fine.
      //Serial.write(buff, c); Serial.println();
      

      //but if i dont use serial.write program never gets here
      if (char * pch = strstr (buff, "quis fuga")) {
        char str[20] = { 0 }; int g =   0 ;
        while (pch[g] != '\"') {
          str[g] = pch[ g];
          g++;
        }
        Serial.print("FOUND IT: "); Serial.println(str);
      }

    }
    else if (!size) {
      Serial.println( "reading done");
      break;
    }
    delay(1);
  }


}

void loop() {

  delay(222222);

}


IEDIT here i created a compilable runnable code. basically i am reading thetrying to find a word in a http response by buffer as shown above but when I search something. my real examples response isnt json and it is as long as this examples response. one other problem is i cant read all of the repsponse. it unexpectedly finishes reading sometimes after a few lines sometimes after a few hundred. in this particular request my nodemcu cant read after id 258. or id 8. object in the json response: https://jsonplaceholder.typicode.com/photos

if i dont use serial.write print buffer without printing itcontent, I can't get anythingit finishes reading immideatly and thats exactly where i have the problem. What is wrong with my code?

static char buff[512] = { 0 };
while (http.connected()) {
  size_t size = client->available();
  if (size) {
    int c = client->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
    //if i dont uncomment this, program is able to reach next if statement: 
    //Serial.write(buff, c); Serial.println(); Serial.println();
    if (char * pch = strstr (buff, "str")) {
      Serial.println("found!");
      //program never gets here
    }
  }
}

I am reading the response by buffer as shown above but when I search something in the buffer without printing it, I can't get anything. What is wrong with my code?

#include <ESP8266WiFi.h>


#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>

#define WIFI_SSID "ssid"
#define WIFI_PASSWORD "password"

void setup() {
  Serial.begin(9600);
  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();
  ////////////////////////////////////////////

  std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
  client->setInsecure();
  HTTPClient http;

  http.begin(*client, "https://jsonplaceholder.typicode.com/photos");
  http.addHeader("Accept", "*/*");


  int httpCode = http.GET();
  Serial.print("HTTP CODE: "); Serial.println(httpCode);

  //reading response with buffer
  static char buff[128] = { 0 };
  while (http.connected() ) {
    size_t size = client->available();
    if (size) {
      int c = client->readBytes(buff, ((size > sizeof(buff) ) ? sizeof(buff) : size));
      //if i uncomment this next line, everything is fine.
      //Serial.write(buff, c); Serial.println();
      

      //but if i dont use serial.write program never gets here
      if (char * pch = strstr (buff, "quis fuga")) {
        char str[20] = { 0 }; int g =   0 ;
        while (pch[g] != '\"') {
          str[g] = pch[ g];
          g++;
        }
        Serial.print("FOUND IT: "); Serial.println(str);
      }

    }
    else if (!size) {
      Serial.println( "reading done");
      break;
    }
    delay(1);
  }


}

void loop() {

  delay(222222);

}


EDIT here i created a compilable runnable code. basically i am trying to find a word in a http response. my real examples response isnt json and it is as long as this examples response. one other problem is i cant read all of the repsponse. it unexpectedly finishes reading sometimes after a few lines sometimes after a few hundred. in this particular request my nodemcu cant read after id 258. or id 8. object in the json response: https://jsonplaceholder.typicode.com/photos

if i dont use serial.write print buffer content, it finishes reading immideatly and thats exactly where i have the problem.

added 1 character in body
Source Link
static char buff[512] = { 0 };
while (http.connected()) {
  size_t size = client->available();
  if (size) {
    int c = client->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
    //if i dont uncomment this, program is able to reach next if statement: 
    //Serial.write(buff, c); Serial.println(); Serial.println();
    if (char * pch = strstr (buff, "str")) {
      Serial.println("found!");
      //program never gets here
    }
  }
}

I am reading the response by buffer as shown above but when I search something in the buffer without printing it, I can't get anything. What is wrong with my code?

static char buff[512] = { 0 };
while (http.connected()) {
  size_t size = client->available();
  if (size) {
    int c = client->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
    //if i dont uncomment this program is able to reach next if statement: 
    //Serial.write(buff, c); Serial.println(); Serial.println();
    if (char * pch = strstr (buff, "str")) {
      Serial.println("found!");
      //program never gets here
    }
  }
}

I am reading the response by buffer as shown above but when I search something in the buffer without printing it, I can't get anything. What is wrong with my code?

static char buff[512] = { 0 };
while (http.connected()) {
  size_t size = client->available();
  if (size) {
    int c = client->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
    //if i dont uncomment this, program is able to reach next if statement: 
    //Serial.write(buff, c); Serial.println(); Serial.println();
    if (char * pch = strstr (buff, "str")) {
      Serial.println("found!");
      //program never gets here
    }
  }
}

I am reading the response by buffer as shown above but when I search something in the buffer without printing it, I can't get anything. What is wrong with my code?

i cant I can't use buffer without printing in my Nodemcu esp8266

static char buff[512] = { 0 }; while (http.connected()) { size_t size = client->available(); if (size) { int c = client->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); //if i dont uncomment this program is able to reach next if statement: //Serial.write(buff, c); Serial.println(); Serial.println(); if (char * pch = strstr (buff, "str")) { Serial.println("found!"); //program never gets here } } }

static char buff[512] = { 0 };
while (http.connected()) {
  size_t size = client->available();
  if (size) {
    int c = client->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
    //if i dont uncomment this program is able to reach next if statement: 
    //Serial.write(buff, c); Serial.println(); Serial.println();
    if (char * pch = strstr (buff, "str")) {
      Serial.println("found!");
      //program never gets here
    }
  }
}

iI am reading the response by buffer as shown above but when iI search something in the buffer without printing it, i cantI can't get anything. whatWhat is wrong with my code.?

i cant use buffer without printing in my Nodemcu esp8266

static char buff[512] = { 0 }; while (http.connected()) { size_t size = client->available(); if (size) { int c = client->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); //if i dont uncomment this program is able to reach next if statement: //Serial.write(buff, c); Serial.println(); Serial.println(); if (char * pch = strstr (buff, "str")) { Serial.println("found!"); //program never gets here } } }

i am reading the response by buffer as shown above but when i search something in the buffer without printing it, i cant get anything. what is wrong with my code.

I can't use buffer without printing in my Nodemcu esp8266

static char buff[512] = { 0 };
while (http.connected()) {
  size_t size = client->available();
  if (size) {
    int c = client->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
    //if i dont uncomment this program is able to reach next if statement: 
    //Serial.write(buff, c); Serial.println(); Serial.println();
    if (char * pch = strstr (buff, "str")) {
      Serial.println("found!");
      //program never gets here
    }
  }
}

I am reading the response by buffer as shown above but when I search something in the buffer without printing it, I can't get anything. What is wrong with my code?

Source Link
Loading