ESP8266 needed to restart every 10 minutes...

Hi,
I'm doing a project with a NodeMCU ESP8266 module where i just use it to turn an LED on or off from anywhere. I've simply used code from online and after some port forwarding it works! However, when i plug in the module, it works but after 10ish minutes, the device cannot connect to the server and i can't turn the LED on or off. I have to keep pressing the reset button on the ESP8266 to make it work.

#include <ESP8266WiFi.h>

const char* ssid = "*Not putting it in because of safety...*";
const char* password = "Not putting it in because of safety...";
const char* host = "192.168.0.25"; // (not actual one...)
                                        //just write it here afterwards and upload
int ledPin = D3;


WiFiServer server(301); //just pick any port number you like

void setup() {
  Serial.begin(115200);
  delay(10);
Serial.println(WiFi.localIP());
  // prepare GPIO2
  pinMode(ledPin, OUTPUT);
  digitalWrite(D3, LOW);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  client.flush();

  // Match the request
  if (req.indexOf("") != -10) {  //checks if you're on the main page

    if (req.indexOf("/OFF") != -1) { //checks if you clicked OFF
      digitalWrite(ledPin, LOW);
      Serial.println("You clicked OFF");
    }
    if (req.indexOf("/ON") != -1) { //checks if you clicked ON
      digitalWrite(ledPin, HIGH);
      Serial.println("You clicked ON");
    }
  }

  else {
    Serial.println("invalid request");
   client.stop();
    return;
  }

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  s += "
<input type=\"button\" name=\"bl\" value=\"Turn LED ON \" onclick=\"location.href='/ON'\">";
  s += "


";
  s += "
<input type=\"button\" name=\"bl\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">";
  s += "</html>\n";

  client.flush();


  // Send the response to the client
  client.print(s);
  delay(1);


  
}

Any help would be appreciated!
Thanks!

Well your webpage is not complete as for html format, but somehow i doubt that is the actual issue, have you tried accessing from more than 1 device ? it is possible that your browser crashes because of it.
Also you should put in a 'backdoor' to control the led, using the hwSerial port via USB and the Serial Monitor, to determine if it is just the wifi which can not connect anymore (i suspect) or whether the nodeMCU has frozen up all together.

Any clue visible on the serial console?
Try adding a yield() statement at the top of the loop().

Deva_Rishi:
Well your webpage is not complete as for html format, but somehow i doubt that is the actual issue, have you tried accessing from more than 1 device ? it is possible that your browser crashes because of it.
Also you should put in a 'backdoor' to control the led, using the hwSerial port via USB and the Serial Monitor, to determine if it is just the wifi which can not connect anymore (i suspect) or whether the nodeMCU has frozen up all together.

I'll be honest, I'm a beginner when it comes to this... What would the code look like...?

What does normal use of Serial for debugging purposes look like ? And in this case Serial reception to control program flow ? Have a look at the examples under 'communication' for instance SerialEvent to see how you can receive data (you actually would not need to receive more than 1 character or not even that), you could upon reception of any data, toggle the led state and then clear the buffer. There are many Serial communication examples, check them out.