Call to server.available() fails if client is a global variable

I need to run the Web Server example with client as a global variable, as in:

WiFiServer server(80);
WiFiClient client;
... etc
void loop() {
  // listen for incoming clients
  client = server.available();
  if (client) {
    Serial.println("new client");
    // an HTTP request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
... etc

If I make this change with the example program at:

https://docs.arduino.cc/tutorials/giga-r1-wifi/giga-wifi#web-server

I get an MbedOS error (red flashing LED) at the call to server.available().

I need to do this to fit in with an existing program I am already successfully running on other Wi-Fi platforms, such as ESP32 and Raspberry Pi Pico W.

Why does it cause an error if client is global, and what can I do to solve this?

Hi, I'm having exactly the same problem and also need the WiFiClient global. Did you find a fix for this? Do you know how to escalate?

Thanks
Steve

Glad to hear you've reproduced the problem. No, I haven't found a fix, and basically gave up on this.

Thanks for confirming. I've raised this issue that might move this forward.

https://github.com/arduino/ArduinoCore-mbed/issues/753

For what it is worth, I was able to reproduce this. I had it running under Ozone debugger so

It faulted in: MbedClient::configureSocket
called from WiFiClient::copyClient

It faulted trying to call _s->set_timeout

hi I am having an error with my lcd i2c circuit and using arduino code can you please help me?
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};

byte rowPins[ROWS] = { 11, 10, 9, 8 };
byte colPins[COLS] = { 7, 6, 5, 4 };

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int operation_count=0;
char selected_operation[4];
char questions [4][20]={
"555+10=?",
"36-10=?",
"4231x2=?",
"24/4=?"
};
char answers [4][5]={
"565",
"26",
"8462",
"6"
};
void setup() {
lcd.begin();
Serial .begin(9600); // or lcd.begin();
lcd.setCursor(0, 0);
lcd.print("press any key");
Serial.println("press any key");
lcd.setCursor(0,1);
lcd.print("to start");
Serial.println("to start");
delay(1000);
lcd.backlight();

}

void loop() {
char key = keypad.getKey();
char operation;
if (key){
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("A: Add B: Sub");
Serial.println("A: Add B: Sub");
lcd.setCursor(0, 1);
lcd.print("C: Multi D: Div");
Serial.println("C: Multi D: Div");
while (true){
char input=keypad.getKey();
if (input=='A' || input=='B' || input=='C' || input=='D'){
operation=input;
break;
}
}
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("enter 1st number");
Serial.println("enter 1st number");
lcd.setCursor(0, 1);
lcd.print("then press #");
Serial.println("then press #");
int num1 = readNumber();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("num1=");
Serial.println("num1=");
lcd.setCursor(5,0);
lcd.print(num1);
Serial.println(num1);
delay(2000);
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("enter 2nd number");
Serial.println("enter 2nd number");
lcd.setCursor(0, 1);
lcd.print("then press #");
Serial.println("then press #");
int num2 = readNumber();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("num2=");
Serial.println("num2=");
lcd.setCursor(5,0);
lcd.print(num2);
Serial.println(num2);
delay(2000);
lcd.setCursor(0, 0);
lcd.clear();
float result;
char operation_symbol;
switch(operation){
case 'A':
result=num1+num2;
operation_symbol='+';
break;
case 'B':
result=num1-num2;
operation_symbol='-';
break;
case 'C':
result=num1*num2;
operation_symbol='x';
break;
case 'D':
if (num2!=0){
result=num1/num2;
operation_symbol='/';
}
else {
lcd.print("Error: Division by zero");
Serial.println("Error: Division by zero");
delay(2000);
lcd.clear();
break; // Exit the switch
}
break;
default:
lcd.print("Invalid operation");
Serial.println("Invalid operation");
delay(2000);
lcd.clear();
break; // Exit the switch
}
lcd.setCursor(0,0);
lcd.print("Result:");
Serial.println("Result:");
lcd.print(num1);
Serial.println(num1);
lcd.print(operation_symbol);
Serial.println(operation_symbol);
lcd.print(num2);
Serial.println(num2);
lcd.setCursor(0,1);
lcd.print("=");
Serial.println("=");
lcd.setCursor(2,1);
lcd.print(result);
Serial.println(result);
delay(3000);
lcd.clear();

selected_operation[operation_count]=operation;
operation_count++;
if (operation_count>=4){
  for (int i = 0; i < 4; i++) {
    lcd.setCursor(0, 0);
    lcd.print("Q");
    Serial.println("Q");
    lcd.print(i + 1);
    Serial.println(i + 1);
    lcd.print(": ");
    Serial.println(": ");
    lcd.print(questions[i]);
    Serial.println(questions[i]);
    lcd.setCursor(0, 1);
    lcd.print("Your answer: ");
    Serial.println("Your answer: ");
    char userAnswer[5];
    int answerIndex = 0;
    while (true) {
      char input = keypad.getKey();
      if (input >= '0' && input <= '9') {
        userAnswer[answerIndex] = input;
        lcd.print(input);
        Serial.println(input);
        answerIndex++;
      } else if (input == '#') {
        userAnswer[answerIndex] = '\0';
        break;
      }
    }
    if (strcmp(userAnswer,answers[i])==0){
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Correct!");
      Serial.println("Correct!");
    }
     else {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Incorrect.");
      Serial.println("Incorrect.");
    }
    delay(3000);
    lcd.clear();
}
operation_count=0;
lcd.clear();












lcd.print("Press #");
Serial.println("Press #");
lcd.setCursor(0,1);
lcd.print("to try again");
Serial.println("to try again");


while (true){
  char input=keypad.getKey();
  if (input=='#'){
    lcd.clear();
    break;    
  }
}}
lcd.print("press any key");
Serial.println("press any key");

}

}
int readNumber() {
int num=0;
while (true){
char key=keypad.getKey();
if(key >= '0' && key <= '9'){
num = num * 10 + (key - '0');
}
else if (key=='#'){
break;
}
}
return num;
}

I am also using a keypad.So basicilly initially the lcd display is blank and not powered and when I try clicking on the buttons lcd gets on but does not display anything this is the error please help me.and thank you in advance

Hello Lily,

My guess is you might be better off, if you opened this up as it's own forum thread, as it does not appear to be related to the issue discussed in this thread.

Also it would be nice if all of the code was in code tags as sort of hard to read.

Questions:
Which display are you using? What voltage does it need? What voltages are you supplying to it?

How do you have your I2C stuff hooked up?
Specifically are you setup on the Wire1 pins?
i2cpins.png (1920×1080) (arduino.cc)

image

Power and GND