Web Server Interruptions - Web Site Hangs

Trying to get the Arduino Uno Wifi Rev 2 to serve a web page that controls some lights (as an example).

My code is a mix of two examples. A great little example from John Woolsey based on the SimpleWebServerWiFi example (Controlling An Arduino Uno WiFi Rev2 Or Arduino Uno With WiFi Shield From A Web Browser - Woolsey Workshop) and an example I found on another Arduino forum (Get Server status and restart · Issue #47 · arduino-libraries/WiFiNINA · GitHub) where I was attempting to integrate WiFi sleep modes and such.

My issue:

  1. John Woolsey code works fine but seems to fall asleep or maybe just gets disconnected from Wifi (not certain which), and requires a hard reset. This is a problem for me as the setup is intended to be at a remote site
  2. The WiFiNINA/issues/47 code also works but cycles through a Wifi reconnect every 30 seconds and so causes the web page to get hung up during.

What I am hoping to learn how to do is properly sleep the device for some short time intervals when no activity has happened for some time, but once awake, stay awake for at least 5 or 10 minutes so the web site pages react nice.

Any suggestions greatly appreciated.

PS: I read the Arduino online documentation about how the classes used in my code and think I get most of it. If there is some more specific parts I may be misunderstanding, please point out.

Please see my code attached.

SimpleWebServerWiFi_LEDs_With_SleepMode_Forum.ino.ino (14.1 KB)

SimpleWebServerWiFi_LEDs_With_SleepMode_Code and Serial Output.txt (965 Bytes)

The WiFiNINA/issues/47 code does some stuff you should never do: handling standard code in interrupt context. Call ControlClientWebPage() from your loop and remove the RTC interrupt code. Call WiFiConnection() from setup() and remove everything else you currently have in loop().
Once that runs, you should check the WiFi connection in every loop() run and re-establish it if it fails.

Thank you for your guidance. Will work on that and report back when I have completed attempted code cleanup suggestions.

Ok…cleaned up the code and broke down some things into a few more functions. I also commended out all the serial monitor elements. It runs well now and has stayed running for about 12 hours without issue. I am not certain if my code will deal with network disconnects so will continue to let it run to see how it survives long term.

So what I really was shooting for was how put the setup into sleep mode when nothing is changing and then how to get it to wake up when user interaction happens via the web interface.

Any help is much appreciated.

#include <SPI.h>
#include <WiFiNINA.h>  // for use with Arduino Uno WiFi Rev2

const int    redLED = 2;
const int yellowLED = 3;
const int  greenLED = 5;

IPAddress ip(x, y, z, 150);
IPAddress dns1(x, y, z, 16);
IPAddress gateway(x, y, z, 1);
IPAddress subnet(255, 255, 255, 0);

char hostname[] = "optiUnoWiFi-01";

WiFiClient client;
char ssid[] = "myssid";
char pass[] = "mypass";
int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  Serial.begin(9600);  // initialize serial communication

  pinMode(redLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);

  digitalWrite(redLED, LOW);
  digitalWrite(yellowLED, LOW);
  digitalWrite(greenLED, LOW);



void loop() {


char WiFiDeviceCheck(){
  if (WiFi.status() == WL_NO_SHIELD || WiFi.status() == WL_NO_MODULE) {
    while (true);
  else {

  String fv = WiFi.firmwareVersion();
  if (fv < "1.2.0") {

/*                   Make the WiFi Connection                    */
char WiFiConnection(){

  status = WL_IDLE_STATUS;

  while (status != WL_CONNECTED) {

    status = WiFi.begin(ssid, pass);
  while (server.status() != 1) {

void ControlClientWebPage() {
  WiFiClient client = server.available();

  if (client) {
    String currentLine = "";
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // Serial.write(c);
        if (c == '\n') {
          if (currentLine.length() == 0) {
          } else {
            currentLine = "";
        } else if (c != '\r') {
          currentLine += c;

void showWebPage(WiFiClient client) {
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");

  client.println("<h1>Arduino Remote Control</h1>");
  client.println("<table border=1 style='text-align:center'>");

  client.print("<tr><td>Red LED</td><td>");
  if (digitalRead(redLED)) {  // show colored status based on state of redLED
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  client.println("</td><td><a href='/redLED/on'>ON</a> / <a href='/redLED/off'>OFF</a></td></tr>");  // display redLED control links

  client.print("<tr><td>Yellow LED</td><td>");
  if (digitalRead(yellowLED)) {  // show colored status based on state of yellowLED
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  client.println("</td><td><a href='/yellowLED/on'>ON</a> / <a href='/yellowLED/off'>OFF</a></td></tr>");  // display yellowLED control links

  client.print("<tr><td>Green LED</td><td>");
  if (digitalRead(greenLED)) {  // show colored status based on state of greenLED
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  client.println("</td><td><a href='/greenLED/on'>ON</a> / <a href='/greenLED/off'>OFF</a></td></tr>");  // display greenLED control links


  // The HTTP response ends with another blank line

void performRequest(String line) {
  if (line.endsWith("GET /redLED/on")) {             // turn on red LED
    digitalWrite(redLED, HIGH);
  } else if (line.endsWith("GET /redLED/off")) {     // turn off red LED
    digitalWrite(redLED, LOW);
  } else if (line.endsWith("GET /yellowLED/on")) {   // turn on yellow LED
    digitalWrite(yellowLED, HIGH);
  } else if (line.endsWith("GET /yellowLED/off")) {  // turn off yellow LED
    digitalWrite(yellowLED, LOW);
  } else if (line.endsWith("GET /greenLED/on")) {    // turn on green LED
    digitalWrite(greenLED, HIGH);
  } else if (line.endsWith("GET /greenLED/off")) {   // turn off green LED
    digitalWrite(greenLED, LOW);

void printWifiStatus() {
  // print the SSID of the network you're attached to
  // Serial.print("SSID: ");
  // Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to
  byte bssid[6];
  // Serial.print("BSSID: ");
  // print the received signal strength
  long rssi = WiFi.RSSI();
  // Serial.print("signal strength (RSSI): ");
  // Serial.print(rssi);
  // Serial.println(" dBm");

// print the encryption type
  byte encryption = WiFi.encryptionType();
  // Serial.print("Encryption Type: ");
  // Serial.println(encryption, HEX);

  // Serial.println();

  // print the hostname of the device
  // char hostname = WiFi.getHostname();
  // Serial.print("Hostname: ");
  // Serial.println(hostname);

  // print the firmware version
  String fv = WiFi.firmwareVersion();
  // Serial.print("Firmware version: ");
  // Serial.println(fv);
  // Serial.println();

  // print the WiFi device's IP address
  // Serial.print("IP Address: ");
  // Serial.println(WiFi.localIP());

  // print the WiFi device's subnet mask
  // Serial.print("Subnet mask: ");
  // Serial.println(WiFi.subnetMask());

  // print the WiFi device's gateway IP address
  // Serial.print("Gateway IP Address: ");
  // Serial.println(WiFi.gatewayIP());

  // print the WiFi device's dns IP address(s)
  // Serial.print("DNS IP Address(s): ");
  // Serial.println(dns1);

  // Serial.println();

  // Print where to go in a browser
  // Serial.println("To see this page in action:");
  // Serial.print("Open a browser to http://");
  // Serial.println(ip);
  // Serial.println("--OR--");
  // Serial.print("Open a browser to http://");
  // Serial.println(hostname);

void printMacAddress(byte mac[]) {
  for (int i = 5; i >= 0; i--) {
    if (mac[i] < 16) {
      // Serial.print("0");
    // Serial.print(mac[i], HEX);
    if (i > 0) {
      // Serial.print(":");
  // Serial.println();

So what I really was shooting for was how put the setup into sleep mode when nothing is changing and then how to get it to wake up when user interaction happens via the web interface.

This is not possible because in sleep mode the network interface is not up.