HELP - Why HTTP client reply seems to hang or become unresponsive

Hello everyone. I know that its not a Wifi Shield but i don't know where to fit this question but i guess it's about a simple HTTP Client question so maybe someone experienced on it can help. (its the first time experiencing with http)

I have an ESP8266 where i control a LED Strip from a Raspberry Pi running Homebridge npm node.js script.
The idea is to control it from the IOS Home app. The Raspberry Pi is the gateway between the two.

For those who don't know about "Homebridge", it's fairly simple. You run it with plugins on pi, who acts as a gateway for the IOS app. Then you edit a .json config file where you state what accessories you want to simulate.

I have a code similar to this i'm about to post running all ok with one accessory (1 led strip) , and i just "duplicated" the functions for the second simulated led strip. Notice that i used FASTLED library and i am only running 1 physical led strip and just "coded" it into separated "virtual" strips.
1 Accessory i can get it up and running and it "refreshes" well when i open the "home" app on the ios, like all should be.

But with 2 Accessories sometimes it starts right away and works ok, but when i reopen the "home" app it just stays refreshing and after a big while, sometimes, it gets back working.

I started debugging and i noticed that the ESP kind of hangs or gets really slow replying to the first request from the PI when refreshing the "home" app.

With 2 Acc. the pi asks 2x3 questions to the ESP. (state, color and brightness).
Usually the first one is color, and i see that serial output reads the request but i've put a serial output after client.serial occurs for debugging and it takes some long time to reply. Seems like it does not have it's .available when it should or something like that.

Can someone see whats wrong in my code? It's based on
https://www.studiopieters.nl/apple-homebridge-neopixel-light/
and i know i have everything running well besides the http part.

#include <ESP8266WiFi.h>
//#include <Adafruit_NeoPixel.h>
//#include <math.h>
#include "FastLED.h"



////////////////////////////////////////////////////////////////////
// Which pin On which pin is the ESP8266-12E connected to the NeoPixels
#define DATA_PIN 2

// How many NeoPixels are attached to the ESP8266-12E
#define NUM_LEDS      6

WiFiServer server(80); //Set server port

String readString;           //String to hold incoming request
String hexString = "000000"; //Define inititial color here (hex value)
String hexString2 = "000000"; //Define inititial color here (hex value)

float R2;
float G2;
float B2;
int state;
int estado;
int Bright;
int Bright2;

int r;
int g;
int b;
int r2;
int g2;
int b2;


float R;
float G;
float B;

int x;
int V;
int x2;
int Vdois;

CRGB leds[NUM_LEDS];


///// WiFi SETTINGS - Replace with your values /////////////////
const char* ssid = "DQTF";
const char* password = "1forrest1";
IPAddress ip(192, 168, 1, 10);   // set a fixed IP for the NodeMCU
IPAddress gateway(192, 168, 1, 254); // Your router IP
IPAddress subnet(255, 255, 255, 0); // Subnet mask
////////////////////////////////////////////////////////////////////

void WiFiStart() {
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  WiFi.config(ip, gateway, subnet);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print("_");
  }
  Serial.println();
  Serial.println("Done");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("");

  server.begin();
}

void allOff() {
  for (int i = 0; i < 3; i++) {
    leds[i] = CRGB(0, 0, 0);
  }
  state = 0;
  //LEDS.setBrightness(0);
  // FastLED.show();

}

void allOff2() {
  for (int i = 4; i < 6; i++) {
    leds[i] = CRGB(0, 0, 0);
  }
  estado = 0;
  //LEDS.setBrightness(0);
  // FastLED.show();

}


//Write requested hex-color to the pins
void setHex() {
  state = 1;
  long number = (long) strtol( &hexString[0], NULL, 16);
  r = number >> 16;
  g = number >> 8 & 0xFF;
  b = number & 0xFF;

  for (int i = 0; i < 3; i++)
  {
    leds[i] = CRGB(g, r, b);
  }
  //FastLED.show();
}

//Write requested hex-color to the pins
void setHex2() {
  estado = 1;
  long number2 = (long) strtol( &hexString2[0], NULL, 16);
  r2 = number2 >> 16;
  g2 = number2 >> 8 & 0xFF;
  b2 = number2 & 0xFF;

  for (int i = 4; i < 6; i++)
  {
    leds[i] = CRGB(g2, r2, b2);
  }
  //FastLED.show();
}

//Compute current brightness value
void getV() {
  R = roundf(r / 2.55);
  G = roundf(g / 2.55);
  B = roundf(b / 2.55);
  x = _max(R, G);
  V = _max(x, B);
  //LEDS.setBrightness(V);
  // FastLED.show();
}

void getV2() {
  R2 = roundf(r2 / 2.55);
  G2 = roundf(g2 / 2.55);
  B2 = roundf(b2 / 2.55);
  x2 = _max(R2, G2);
  Vdois = _max(x2, B2);
  //LEDS.setBrightness(Vdois);
  // FastLED.show();
}

//For serial debugging only
void showValues() {
  Serial.print("Status on/off: ");
  Serial.println(state);
  Serial.print("RGB color: ");
  Serial.print(r);
  Serial.print(".");
  Serial.print(g);
  Serial.print(".");
  Serial.println(b);
  Serial.print("Hex color: ");
  Serial.println(hexString);
  getV();
  Serial.print("Brightness: ");
  Serial.println(V);
  Serial.println("");
}

void setup() {
  Serial.begin(115200);
  setHex(); //Set initial color after booting. Value defined above
  WiFi.mode(WIFI_STA);
  WiFiStart();
  showValues(); //Uncomment for serial output
  LEDS.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
  FastLED.show(); // Initialize all pixels to 'off'
  LEDS.setBrightness(100);
}

void loop() {
  //Reconnect on lost WiFi connection
  if (WiFi.status() != WL_CONNECTED) {
    WiFiStart();
  }

  WiFiClient client = server.available();

  if (!client) {
    return;
  }

  while (client.connected() && !client.available()) {
    delay(1);
  }

  //Respond on certain Homebridge HTTP requests
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 100) {
          readString += c;
        }
        if (c == '\n') {
          Serial.print("Request: "); //Uncomment for serial output
          Serial.println(readString); //Uncomment for serial output

          //Send reponse
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          //On
          if (readString.indexOf("on") > 0) {
            setHex();
            //showValues();
          }

          //Off
          if (readString.indexOf("off") > 0) {
            allOff();
            //showValues();
          }

          //Set color
          if (readString.indexOf("set") > 0) {
            hexString = "";
            hexString = (readString.substring(9, 15));
            setHex();
            showValues();
          }

          //Status on/off
          if (readString.indexOf("status") > 0) {
            client.println(state);
          }


          //Status color (hex)
          if (readString.indexOf("color") > 0) {
            client.println(hexString);
          }

          if (readString.indexOf("bright") > 0 ) {
            getV();
            client.println(V);
          }


          if (readString.indexOf("li") > 0) {
            setHex2();
            // Serial.println("LIGADORESPONDE");
            //showValues();
          }


          if (readString.indexOf("fud") > 0) {
            allOff2();
            //showValues();
            //Serial.println("DESLIGADO");
          }

          if (readString.indexOf("def") > 0) {
            hexString2 = "";
            hexString2 = (readString.substring(9, 15));
            setHex2();
            showValues();
          }



          if (readString.indexOf("estado") > 0) {
            client.println(estado);
          }



          if (readString.indexOf("tinta") > 0) {
            client.println(hexString2);
          }



          //Status brightness (%)
          if (readString.indexOf("brilho") > 0 ) {
            getV2();
            client.println(Vdois);
          }


          delay(1);

          client.stop();
          readString = "";
        }
      }
    }

    //LEDS.setBrightness(100);
    FastLED.show()

Can someone point me out what am i doing wrong?

They sould respond to the following:
"http://192.168.1.10:80/status"
"http://192.168.1.10:80/estado"
"http://192.168.1.10:80/color"
"http://192.168.1.10:80/tinta"
"http://192.168.1.10:80/bright"
"http://192.168.1.10:80/brilho"

Also, if i test it via safari browser it responds without hanging.
I've read that homebridge sends many requests "at the same time" or maybe faster than the ESP can reply.
Can i cache them or slow them down?
Thank you so very much in advanced.

Hi noiasca!

Thanks for your help.
After i've posted this i've been fiddling with Async webserver for ESP , and the only example i find is FSexample that i guess stands for fileserver.
I've just ran it out to the esp and without the fastled it can handle all the questions and replies from the PI.
The only problem is that i will have to crack my head trying to understand it and find out how to hack it to put the answers i need accordingly to the resquests....ahahah.

But i'll check your suggestion first because maybe its more of a thing i can understand. As far as i can go, i guess that async is C object oriented language that doesn't even run in the loop and i think i can find a way to use it but maybe its more than i can chew.

At least i've sorted out what seems to be the problem ! A little victory for myself. :smiley:

the last lines should be a copy/paste error.

thanks

I compared it against the "HelloServer" and "WifiWebServer". Your code seems to be completely different (more of the style how you find examples for UNO webservers).

As you don't need so much functionality I suggest you do it completely new. Take WifiWebServer as an example and just throw in your neopixel/fastled . I guess even as a beginner you will have a better result in 2h.

Well actually i've took a look at it and WifiWebServer i've had already checked and did not know that was it. it's identical to mine, won't work.
HelloServer is different, but i've uploaded it and when i set a "mood" on Home it will also get stuck on updating because it won't handle the requests. I don't know exactly how many requests setting a "mood" generates, only know that's 8 when it refreshes, but at least those won't work as with async it seemed to be working. i have to explore.