From 07b36e9d083fa9a7000add9f6ed3dbcd9e64c47f Mon Sep 17 00:00:00 2001
From: Kyrre Sjobak <kyrre.ness.sjoebaek@cern.ch>
Date: Mon, 1 Mar 2021 10:39:33 +0100
Subject: [PATCH] WEB server timeout + fix SPC timeout

---
 PositionGaugeServer.ino | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/PositionGaugeServer.ino b/PositionGaugeServer.ino
index 01c2a50..56c03e9 100644
--- a/PositionGaugeServer.ino
+++ b/PositionGaugeServer.ino
@@ -54,6 +54,7 @@ IPAddress subnet(255, 255, 0, 0);
 // ***** OTHER CONFIG ***************************
 const unsigned long update_interval = 1000; //[ms]
 const unsigned long SPC_timeout     = 5;    //[ms] 260 us per 4-bit word, 13 words, plus some buffer
+const unsigned long WEB_timeout     = 600;  //[ms] Max time allowed waiting for web client
 
 // ***** GLOBAL VARIABLES ***********************
 
@@ -261,7 +262,7 @@ void loop() {
 
 
 void readSPCinstrument(uint8_t ch) {
-    unsigned long SPC_timeout_time = millis();
+    unsigned long SPC_timeout_time = millis()+SPC_timeout;
     
     digitalWrite(pinREQ[ch],HIGH);
     
@@ -378,10 +379,13 @@ void readSPCinstrument(uint8_t ch) {
 
 
 void HTTPserver () {
+  unsigned long WEB_timeout_time = millis()+WEB_timeout;
+  
   EthernetClient client = srvWEB.available();
   if(client) {
     Serial.println();
-    Serial.println("New web client");
+    Serial.print("New web client ");
+    Serial.println(client.remoteIP());
   }
   else {
     return;
@@ -393,7 +397,7 @@ void HTTPserver () {
   // We should also echo what the client sends
   
   boolean currentLineIsBlank = true;
-  while(client.connected()) {
+  while(client.connected() and millis() < WEB_timeout_time ) {
     if (client.available()) {
       //WARNING: If client does not "do it's thing",
       // the Arduino may become unresponsive.
@@ -473,6 +477,12 @@ void HTTPserver () {
       
     }
   }
+  
+  if ( millis() >= WEB_timeout_time) {
+    Serial.println();
+    Serial.println("WEB timed out.");
+  }
+  
   // give the web browser time to receive the data
   delay(10);//[ms]
   // close the connection:
-- 
GitLab