Communication bugs between R4 and website

In a project, we've written this code (we've removed some parts to keep it simple). The issue we're facing is that the code works fine most of the time, except occasionally the website controlled and receiving data from UNO R4 WiFi gets stuck in some kind of loop, causing the site to freeze and no values to be sent. We've tried uploading the code to UNO R4 WiFi multiple times, but we encounter similar problems with the website getting stuck and continuously reloading, even though R4 is sending data. We've pressed the 'Reset' button, tried sending simple codes, but the same issue persists.

Interestingly, after a short while, UNO R4 WiFi works flawlessly for up to 20 minutes before the same problem occurs again. Is there anyone who might know what could be causing this and how we could potentially reduce this bottleneck if it's related to data exchange between the website and Arduino UNO R4 WiFi?

We're incredibly grateful for any help we can get!

The code:

#include "WiFiS3.h"
#include "arduino_secrets.h"
 
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
 
 
int status = WL_IDLE_STATUS;
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  pinMode(4, OUTPUT);      // set the LED pin mode
  pinMode(ventil, OUTPUT);
  digitalWrite(ventil, LOW);
  digitalWrite(4, LOW);
 
 
  if (WiFi.status() == WL_NO_MODULE) {
    // Serial.println("Communication with WiFi module failed!");
    while (true);
  }
 
  String fv = WiFi.firmwareVersion();
  /*if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    // Serial.println("Please upgrade the firmware");
  }*/
 
  WiFi.config(IPAddress(192, 48, 56, 2));
  // Serial.print("Creating access point named: ");
  // Serial.println(ssid);
 
  status = WiFi.beginAP(ssid, pass);
  /*if (status != WL_AP_LISTENING) {
    // Serial.println("Creating access point failed");
    while (true);
  }*/
 
 
 
  server.begin();
  printWiFiStatus();
}
 
// Handle AJAX request for valve status
 
 
 
 
void loop() {
  
 
  
  // Jämför WiFi-status
  if (status != WiFi.status()) {
    status = WiFi.status();
    if (status == WL_AP_CONNECTED) {
      // Serial.println("Device connected to AP");
    } else {
      // Serial.println("Device disconnected from AP");
    }
  }
 
 
 
  // Lyssna efter klienter
  WiFiClient client = server.available();
  if (client) {
    // Serial.println("new client");
    String currentLine = "";
    while (client.connected()) {
      delayMicroseconds(10);
      if (client.available()) {
        char c = client.read();
        // Serial.write(c);
        if (c == '\n') {
          if (currentLine.length() == 0) {
            // client.println("HTTP/1.1 200 OK/nContent-type:text/html/n/n")
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"); 
 
            client.println();
 
           
 
            break;
          } else {
            currentLine = "";
          }
        } else if (c != '\r') {
          currentLine += c;
        }
        }
      }
    }
    // Stäng anslutningen
    client.stop();
    // Serial.println("client disconnected");
  }
}
 
void printWiFiStatus() {
  // Serial.print("SSID: ");
  // Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  // Serial.print("IP Address: ");
  // Serial.println(ip);
  // Serial.print("To see this page in action, open a browser to http://");
  // Serial.println(ip);
}
 
[4:18 PM] Lydia Trygg
#include "WiFiS3.h"
#include "arduino_secrets.h"
 
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
 
 
int status = WL_IDLE_STATUS;
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  
  if (WiFi.status() == WL_NO_MODULE) {
    // Serial.println("Communication with WiFi module failed!");
    while (true);
  }
 
  String fv = WiFi.firmwareVersion();
  /*if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    // Serial.println("Please upgrade the firmware");
  }*/
 
  WiFi.config(IPAddress(192, 48, 56, 2));
  // Serial.print("Creating access point named: ");
  // Serial.println(ssid);
 
  status = WiFi.beginAP(ssid, pass);
  /*if (status != WL_AP_LISTENING) {
    // Serial.println("Creating access point failed");
    while (true);
  }*/
 
 
  server.begin();
  printWiFiStatus();
}
 
 
void loop() {
    
  // Jämför WiFi-status
  if (status != WiFi.status()) {
    status = WiFi.status();
    if (status == WL_AP_CONNECTED) {
      // Serial.println("Device connected to AP");
    } else {
      // Serial.println("Device disconnected from AP");
    }
  }
 
 // Lyssna efter klienter
  WiFiClient client = server.available();
  if (client) {
    // Serial.println("new client");
    String currentLine = "";
    while (client.connected()) {
      delayMicroseconds(10);
      if (client.available()) {
        char c = client.read();
        // Serial.write(c);
        if (c == '\n') {
          if (currentLine.length() == 0) {
            // client.println("HTTP/1.1 200 OK/nContent-type:text/html/n/n")
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"); 
 
            client.println();
            break;
          } else {
            currentLine = "";
          }
        } else if (c != '\r') {
          currentLine += c;
        }
        }
      }
    }
    // Stäng anslutningen
    client.stop();
    // Serial.println("client disconnected");
  }
}
 
void printWiFiStatus() {
  // Serial.print("SSID: ");
  // Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  // Serial.print("IP Address: ");
  // Serial.println(ip);
  // Serial.print("To see this page in action, open a browser to http://");
  // Serial.println(ip);
}

I had the same issue and a contributor suggested that I update the firmware. It solved the problem for me.

Update the connectivity module firmware on UNO R4 WiFi

2 Likes