HTTP request with switch function

Hi,

I'm very new with Arduino code.

I tried to call webpage with input parameters

It's works 3 times and after it's finished....

Did you have a idea ?

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,2,114); // Google

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  
  connectWS();
  
  // give the Ethernet shield a second to initialize:
  delay(1000);
}

void loop()
{
    //Read input
    int val = Serial.read() - '0';  
    
    switch(val) {
      case 0:

        Serial.println("OFF");
        client.println("GET /index.php?action=off HTTP/1.0");
        client.println();  
        break;
      case 1:
        Serial.println("ON");
        // Make a HTTP request:
        client.println("GET /index.php?action=on HTTP/1.0");
        client.println();
        break;
        default:
          Serial.println("wainting action");      
    }
    delay(1000);
}

String connectWS(){
  //port 80 is typical of a www page
  if (!client | !client.connected()) {
    if (client.connect(server, 80)) {
      Serial.println("connected");
      client.println();
    }else{
      return "connection failed";
    } 
  } else {
    Serial.println("Already connected");
  }
}
  
String disconnectWS() {
    if(client) {
      client.stop();
      Serial.println("disconnecting.");
    }
}

Serial.read() isn't a blocking call. Make sure there is sometime in the input buffer first. Use Serial.available().