Why isn't the client.available function working in this code

#include <SPI.h>
#include <MFRC522.h>
#include <WiFi.h>

#define SS_PIN 10 // RFID SS Pin
#define RST_PIN 9 // RFID RST Pin

// Replace with your network credentials
const char* ssid = "DolceShot-2.4G";        // Change this to your WiFi SSID
const char* password = "Dolce@2023"; // Change this to your WiFi password

// Replace with your server URL
const char*serverUrl= "https://script.google.com/macros/s/AKfycbyPeD6cVu1ond1pIno6_20T3UXPpUNDaR3Pf1vLsv1AQ6_6_AKw0xGeuwI2qx_XLjJY/exec"; // Change to your actual Google Apps Script URL

// Create MFRC522 instance
MFRC522 rfid(SS_PIN, RST_PIN); 

// Define a structure for users
struct User {
  String uid;
  String name;
  String studentID;
};

// Define your users
User  users[] = {
  {"F3D74CA8", "Alice", "003"},
  {"430D96A6", "Bob", "002"},
  // Add more users as needed
};

bool sendUIDToServer(String uid, String name, String studentID) {
  Serial.print("Sending UID: ");
  Serial.print(uid);
  Serial.print(", Name: ");
  Serial.print(name);
  Serial.print(", Student ID: ");
  Serial.println(studentID);


  if (WiFi.status() != WL_CONNECTED) {
        Serial.println("WiFi Disconnected"); // Log the disconnection
        return false; // Exit the function, cannot send data
    }

    WiFiClient client; // Create a WiFiClient object

    // Attempt to connect to the server
    if (client.connect("script.google.com", 443)) { // Connect to the server
        // Construct the URL with parameters
        String url = String(serverUrl) + "?studentID=" + studentID + "&name=" + name;
        Serial.println(url);
        
        // Send the GET request
        client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                     "Host: script.google.com\r\n" +
                     "Connection: close\r\n\r\n");
        

        // Wait for the server's response
        unsigned long timeout = millis();
      while ((client.connected() || client.available()) && millis() - timeout < 5000) { // 5-second timeout
          if (client.available()) {
              String line = client.readStringUntil('\n');
              Serial.println(line); // Print the response from the server
          }
      }

      if (millis() - timeout >= 5000) {
          Serial.println("Response timeout.");
      }

        client.stop(); // Close the connection
        return true; // Successfully sent
    } else {
        Serial.println("Connection to server failed."); // Log connection failure
        return false; // Failed to connect
    }
}




const int numUsers = sizeof(users) / sizeof(users[0]);

void setup() {
  
  Serial.begin(9600);
  delay(1000);
  Serial.println("Starting setup...");
  SPI.begin(); // Initialize SPI bus
  rfid.PCD_Init(); // Initialize RFID reader
  Serial.println("RFID Reader Initialized.");

  // Connect to WiFi
  WiFi.begin(ssid, password);
  
  Serial.print("Connecting to WiFi");
  
  unsigned long startAttemptTime = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 10000) { // 10 seconds timeout
    delay(500);
    Serial.print(".");
  }
  
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Connected to WiFi");
  } else {
    Serial.println("Failed to connect to WiFi");
  }
  
  
}

void loop() {
  // Look for a new RFID card
  if (!rfid.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if (!rfid.PICC_ReadCardSerial()) {
    return;
  }

  // Read the UID
  String uid = "";
  for (byte i = 0; i < rfid.uid.size; i++) {
    uid += String(rfid.uid.uidByte[i], HEX);
  }

  uid.toUpperCase(); // Convert to uppercase for consistency
  Serial.print("Card UID: ");
  Serial.println(uid); // Print the read UID for debugging]
  

  // Check if the UID is in the valid UIDs list
  bool isValid = false;
  String name = "";
  String studentID = "";
  
  for (int i = 0; i < numUsers; i++) {
    if (uid.equals(users[i].uid)) {
      isValid = true;
      name = users[i].name;
      studentID = users[i].studentID;
      break;
    }
  }

  if (isValid) {
    // Send UID, name, and studentID to server
    if (sendUIDToServer(uid, name, studentID)) {
      
      Serial.println(sendUIDToServer(uid, name, studentID));
      Serial.println("UID sent to server successfully.");
    } else {
      Serial.println("Failed to send UID to server.");
    }
  } else {
    Serial.println("Invalid UID. Access Denied.");
  }
  sendUIDToServer(uid, name, studentID);
  // Stop reading the card
  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();
}

hey guys what i want the code to do is when i swipe the rfid card automatically send the
user data to google sheet and i cant find why isn't it working so if there is something missing please tell me

Which Arduino? Which Wifi device? How are they connected?
Please use code tags. Makes it easier to read.

Hello

until you fix your first post, you are not likely to get many answers... To awkward to read...

➜ do yourself a favour and please read How to get the best out of this forum and fix post#1 accordingly (use the small pencil below the post to edit it)

What isn't working excatly? What did you expect to happen? What happened instead? Any error messages?

Which mcu/board are you using?

i fixed it so i hope you can help me now

Since you didn't mention which wifi device, I will presume the WiFi Shield.

On both boards, pin 10 is used to select the HDG204 and pin 4 for the SD card.

So this would cause real problems.

#define SS_PIN 10 // RFID SS Pin

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