Code slows down rapidly after first http connection WiFiNINA on Nano 33 IoT

Hi, This is my first project with an Arduino Nano 33 IoT and also my first post on this forum, so please let me know if theres any additional info i can provide.
I have a small servo controlling my window blinds, controlled by the arduino. it runs a very basic webserver that gets requests from homebridge, so i can control the position via apple home. My issue is, after the first http request, the code execution slows down by a factor of 20 roughly (i.e before the LED blinks every ~500ms and afterwards every ~10s ) and eventually the arduino doesnt respond at all. i added a small blinking script to the main loop to make this more visible. After some testing it seems like the server.available() call causes this. The issue persists, even when removing the entire control logic, thats why im just posting the code snippet without it.i cant find any info on why this happens exactly or how to fix it, any help is appreciated.

Stuff i've already tried:
replace all Strings with char[].
remove literally all code but the server.available() call
have client.stop() outside both if conditions

My Code:

int led_counter = 0;
bool led_status = 1;
int status = WL_IDLE_STATUS;
//more vars

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  myStepper.setSpeed(rolePerMinute);
  //Setup wifi
  while (status != WL_CONNECTED) {
    status = WiFi.begin(ssid, pass);
    delay(5000);
  }
  server.begin();
}

void loop() {
  led_counter += 1;
  if(led_counter>=50000){
    digitalWrite(LED_BUILTIN,led_status);
    led_counter = 0;
    led_status = !led_status;
  }
  WiFiClient client = server.available(); 
  if(client){
    if(client.available()){
      //read and parse request
      client.stop();
    }
  }
}

You made too many snips to your code do be able to understand its real structure. For example, we don't know what happens in between before "client.stop()", but I see here you just do "if(client.available()){": you need to check "client.connected()" first. This is the common way to do it:

while (client.connected()) {
  if (client.available()) {
    char c = client.read();
    ...and so on processing...

I don't know if this can solve your issue, but give it a try.
PS: "client.stop()" must be called after "while (client.connected())" loop exits.

@docdoc the construct is OK. it is not an esp8266 or esp32.

In proper Arduino WiFi library server.available only returns a 'false' client or a valid and connected client with data already available. If the client is not stopped, server.available returns the same client again when it has data.

and client.connected returns true until data are available (in the buffer).

the fix:

1 Like

this doesnt seem to fix it, but the issue was definitely what was mentioned in the PR. i found the file and just set _lastSock = NO_SOCKET_AVAIL. Seems to work for now (i know this is bad practice but i almost gave up on the project trying to fix this), thank you very much for pointing it out.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.