Help with printing data to HTML table

I am having some issues with my PIR sensor data handling. I need to print to a HTML table whether motion has been detected. At the moment I can print 0 and 1, 0 means that it has not detected movement and 1 means that it has detected movement. I have the following for loop running that is not working.

void loop(){

byte state = digitalRead(sensorPin2);
  if(state == 1){
  Serial2.println("Yes");
}
  else if(state == 0){
  Serial2.println("No");
}

I want to print that value in this HTML table:

client.println("<tr><td>Paikalla?</td><td><span class=\"sensor\">");
client.println(state);
client.println(" </span></td></tr>");

The full code can be seen here:

// Load Wi-Fi library
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <HardwareSerial.h>



#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

// Replace with your network credentials
const char* ssid     = "***********";
const char* password = "********";

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0; 
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

//Määritellään I/O pinnin numero ja alustetaan sensorValue arvo
const int sensorPin = 34; 
int sensorValue = 0;

byte sensorPin2 = 26;
byte redPin = 4; 

void setup() {
  Serial.begin(115200);
  bool status;

// uusi serial yhteys TEMT6000 sensoria varten, säädetään nopeus ja viive 
  Serial1.begin(115200);
  delay(500);    

//PIR sensori
  pinMode(sensorPin2,INPUT);
  pinMode(redPin,OUTPUT);
  Serial2.begin(9600); 
  byte state = digitalRead(sensorPin2);
  digitalWrite(redPin,state);
  delay(300); 
  

       
  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop(){

byte state = digitalRead(sensorPin2);
  if(state == 1){
  Serial2.println("Kyllä");
}
  else if(state == 0){
  Serial2.println("Ei");
}

sensorValue = analogRead(sensorPin);
Serial1.println(sensorValue);
delay(300); 
  
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    currentTime = millis();
    previousTime = currentTime;
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected() && currentTime - previousTime <= timeoutTime) {  // loop while the client's connected
      currentTime = millis();
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"<meta charset=\"UTF-8\"> ");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the table 
            client.println("<style>body { text-align: center; color: black; font-family: \"Trebuchet MS\", Arial; }");
            client.println("table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }");
            client.println("tr { border: 1px solid #ddd; padding: 12px; }");
            client.println("tr:hover { background-color: #bcbcbc; }");
            client.println("td { border: none; padding: 12px; }");
            client.println(".sensor { color:black; font-weight: bold; padding: 1px; }");
            
            // Web Page Heading
            client.println("</style></head><body><h1>ESP32 Mittaukset</h1>");
            client.println("<table><tr><th></th><th></th></tr>");
            client.println("<tr><td>Lämpötila</td><td><span class=\"sensor\">");
            client.println(bme.readTemperature());
            client.println(" °C</span></td></tr>"); 
            client.println("<tr><td>Kosteus</td><td><span class=\"sensor\">");
            client.println(bme.readHumidity());  
            client.println(" %</span></td></tr>"); 
            client.println("<tr><td>Paine</td><td><span class=\"sensor\">");
            client.println(bme.readPressure() / 100.0F);
            client.println(" mbar</span></td></tr>");
            client.println(" </span></td></tr>"); 
            client.println("<tr><td>Valoisuus</td><td><span class=\"sensor\">");
            client.println(sensorValue);
            client.println(" lux</span></td></tr>"); 
            client.println("<tr><td>Paikalla?</td><td><span class=\"sensor\">");
            client.println(state);
            client.println(" </span></td></tr>");
            client.println("</table>");
            
            //client.println();
            client.println("<p><input type=\"button\" value=\"Update\" onClick=\"document.location.reload(true)\"</p> ");


            
            client.println("</body></html>");
            
            
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

What is Serial2? I’m not familiar with it.

PaulMurrayCbr: What is Serial2? I'm not familiar with it.

One of the 4 hardware serial ports available on an Arduino Mega? It appears to be the Mega Wifi version...

If you are not too uncomfortable with HTML and JavaScript I suggest you save the HTML page to the ESP's SPIFFS (there are tutorials for that) and serve the whole page to the client. Then in the HTML of the page use an XMLHttpRqeust (AJAX) to load the data from the ESP. This way your code will not be crowded with HTML code. You'll have a nice and clean separate HTML file. It's easy to debug. It's easy to make changes. It's probably faster and more efficient too.

I am new to working with ESP32, I was not aware that I could upload a separate HTML page on it. Thanks for the advice.

Rather than use SPIFFS (which is deprecated) you should start with the LittleFS (?). There was just a discussion about that on another thread. With that, you can create a subfolder 'data' in your project and then you can upload its contents to the ESP using the IDE, just like the sketch

Sorry, I can't seem to find that other thread at the moment but if you search here, you should be able to locate it

Edit: Here it is https://forum.arduino.cc/?topic=705974#msg4744358