Serving webpage over WiFi with a Pico W

Hey there I'm trying to send some temperature information to a webpage with my Pico W. I've been following the example here. Whenever I try to access the address that gets assigned to my Pico, nothing loads and I never see a "new client" message in the serial monitor. I suspect this has something to do with how I've configured the server. I get a warning saying server.available() is deprecated, but when I check the documentation for the WiFi library I don't see that.

Here is the relevant code that is not working:

void loop() {

// temp sensor logic ...

  WiFiClient client = server.available();

    if (client) {

      Serial.println("New client connected");

      String response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";

      response += "<!DOCTYPE html><html><body>";

      response += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";

      response += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";

      response += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";

      response += "p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";

      response += "</style>\n";

      response += "</head>\n";

      response += "<body>\n";

      response += "<h1>Raspberry Pi Pico W Weather Station</h1>";

      response += "<p>Temperature: <span id=\"temperature\"></span>" + String(temp)+ "&deg;C</p>";

      response += "<p>Humidity: <span id=\"humidity\"></span> " + String(humidity) + " %</p>";

      response += "</body></html>";

      client.print(response);

      delay(10);

      client.stop();

      Serial.println("Client disconnected");
  }
}

And here is the full code

#include <DHT11.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>

DHT11 dht11(16);
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int buttonPin = 15;
bool celsius = true;

int lastButtonState = LOW;
unsigned long lastPressTime = 0;
const unsigned long debounceDelay = 50;

const char* ssid = "Network ID goes here";
const char* password = "Network password goes here";

WiFiServer server(80);

unsigned long currentTime = millis();
unsigned long previousTime = 0;
// Define timeout time in milliseconds
const long timeoutTime = 10000;

String header;

void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();

  // Initialize wifi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to wifi...");
    delay(2500);
  }

  // Connection established
  Serial.println();
  Serial.print("Pico Dubya successfully connected to: ");
  Serial.println(WiFi.SSID());
  Serial.print("Assigned IP Address: ");
  Serial.println(WiFi.localIP());

  // Start web server
  server.begin();
}


void loop() {

// On screen temperature logic and button debounce

  int currentButtonState = digitalRead(buttonPin);

  // Check if the button state is HIGH (pressed) and if enough time has passed since the last press
  if (currentButtonState == HIGH && lastButtonState == LOW && (millis() - lastPressTime) > debounceDelay) {
    Serial.println("Button pressed!");
    celsius = !celsius;  // Toggle Celsius/Fahrenheit
    lastPressTime = millis();  // Update the last press time
  }

  lastButtonState = currentButtonState;

  // Read temperature and humidity from the sensor, only integers for this model =(
  int humidity = dht11.readHumidity();
  int temp = dht11.readTemperature();

  // Writing to LCD
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.setCursor(6, 0);
  lcd.print(celsius ? temp : (temp * 9 / 5 + 32));
  lcd.setCursor(8, 0);
  lcd.print((char)223);
  lcd.setCursor(10, 0);
  lcd.print(celsius ? "C" : "F");

  lcd.setCursor(0, 1);
  lcd.print("Humidity: ");
  lcd.setCursor(10, 1);
  lcd.print(humidity);
  lcd.setCursor(12, 1);
  lcd.print("%");

  delay(10);

  WiFiClient client = server.available();

    if (client) {

      Serial.println("New client connected");

      String response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";

      response += "<!DOCTYPE html><html><body>";

      response += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";

      response += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";

      response += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";

      response += "p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";

      response += "</style>\n";

      response += "</head>\n";

      response += "<body>\n";

      response += "<h1>Raspberry Pi Pico W Weather Station</h1>";

      response += "<p>Temperature: <span id=\"temperature\"></span>" + String(temp)+ "&deg;C</p>";

      response += "<p>Humidity: <span id=\"humidity\"></span> " + String(humidity) + " %</p>";

      response += "</body></html>";

      client.print(response);

      delay(10);

      client.stop();

      Serial.println("Client disconnected");
  }
}

If you get a Deprecated message especially with the latest hardware you need to find and use a better library. Have you tried using the board library WiFi examples? (see pic)
Screenshot 2024-11-09 at 12.14.42

Thanks mate, this set me on the path to a resolution.