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){
  else if(state == 0){

I want to print that value in this HTML table:

client.println("<tr><td>Paikalla?</td><td><span class=\"sensor\">");
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() {
  bool status;

// uusi serial yhteys TEMT6000 sensoria varten, säädetään nopeus ja viive 

//PIR sensori
  byte state = digitalRead(sensorPin2);

  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 ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  // Print local IP address and start web server
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");

void loop(){

byte state = digitalRead(sensorPin2);
  if(state == 1){
  else if(state == 0){

sensorValue = analogRead(sensorPin);
  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 =;             // 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("Connection: close");
            // 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("<tr><td>Lämpötila</td><td><span class=\"sensor\">");
            client.println(" °C</span></td></tr>"); 
            client.println("<tr><td>Kosteus</td><td><span class=\"sensor\">");
            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(" lux</span></td></tr>"); 
            client.println("<tr><td>Paikalla?</td><td><span class=\"sensor\">");
            client.println(" </span></td></tr>");
            client.println("<p><input type=\"button\" value=\"Update\" onClick=\"document.location.reload(true)\"</p> ");

            // The HTTP response ends with another blank line
            // Break out of the while loop
          } 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
    Serial.println("Client disconnected.");

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