Program stuck even after reset

Hey Guys,

Im using arduiono Uno Rev 2 Wifi in my escape room, and yesterday I hit a very strange bug. On a restart between two teams I realized that the Wifi connection was failed for some reason, coupe of restart, nothing, so after the team I plug the board via USB to my laptop to check the issue. It was nothing, serial monitor plain white, even after a reset. But when I upload the same sketch again, its start running perfectly.

#include <SPI.h>
#include <WiFiNINA.h>

#define SECRET_SSID "*********"
#define SECRET_PASS "*********"

char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
int status = WL_IDLE_STATUS;
IPAddress ip(192, 168, 10, 190);  

WiFiServer server(80);
WiFiClient client = server.available();

bool puzzle_active = true;
String road = "";
int last_step = -1;
int client_timeout=0;
int door_active=0;

void enable_WiFi() {
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    //while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }
}

void connect_WiFi() {
  int i=0;
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    i++;
    if (i>1) {break;}
    delay(10000);
  }
}

void setup() {
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);

  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);

  digitalWrite(6,HIGH);
  digitalWrite(7,HIGH);

  Serial.begin(9600);
  while (!Serial);

  enable_WiFi();
  WiFi.config(ip);
  connect_WiFi();
  server.begin();
  printData();
}

void iranyitas(int x)
{
  if (x==0) {digitalWrite(6,LOW);}
  if (x==1) {digitalWrite(7,LOW); door_active=1;}
}

void loop() {
  delay(10);
  if (digitalRead(2)==1 && last_step!=2) {road="2"; last_step=2;}
  if (digitalRead(3)==1 && last_step!=3) {road=road+"3"; last_step=3;}
  if (digitalRead(4)==1 && last_step!=4) {road=road+"4"; last_step=4;}
  if (digitalRead(5)==1 && last_step!=5) {road=road+"5"; last_step=5;}

  if (road=="245435")
  {
    iranyitas(0);
  }

  client = server.available();
  if (client)
  {
    String currentLine = "";
    client_timeout=0;
    while (client.connected())
    {
      client_timeout++;
      if (client_timeout>500) {Serial.println(currentLine); Serial.println("safety break"); break;}
      if (client.available())
      {
        char c = client.read();
        //Serial.println(c);
        if (c == '\n')
        {
          if (currentLine.length() == 0)
          {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
            client.print("OK");
            client.print("#");
            client.print(road);
            client.print("#");
            client.print(door_active);
            client.println();
            break;
          }else
          {
            currentLine = "";
          }
        }else if (c != '\r') {currentLine+=c;}

        if (currentLine.endsWith("_control"))
        {
          int pos=currentLine.indexOf("_");
          String subs=currentLine.substring(pos+1);
          pos=subs.indexOf("_");
          String in_pin=subs.substring(0,pos);
          int pin=in_pin.toInt();
          iranyitas(pin);
        }

      }
    }
    client.stop();
  }

}

void printData() {
  Serial.println("Board Information:");
  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  Serial.println();
  Serial.println("Network Information:");
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);

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

This is the third time I detect this kind of behavior, not with the same arduino and program, but similar.

The issue look like and endless loop somewhere, so my questions are:
Is it possible that while(!serial) will be fail and makes and endless loop for me there?
Is there a way that client.connected() or client.available() stuck somehow?
If its and endless loop, why a single reset not solving it?

Thank you in advance

If not mistaken, while(!Serial) has no function on an Uno WiFi Rev2. You should be able to test that with the blink sketch and that statement. If you have the serial monitor clised and upload the modified example, does the L-LED blink?

If while(!Serial) is functional on your board, your sketch will always hang there if you reset the board without an open communication channel(serial monitor).

Hey sterretje,

thanks for the quick answer.

This gonna be a amateur question, what can be an open communication channel in this case? So far these arduinos (I have 9 of them in total) are running without any connected device, the only communication is through Wifi, so when im not testing it with my laptop and USB, they dont have any communication channel (as far as I know).
One of them was works properly like that for the last 2 years, and a couple of weeks ago hit the same problem.

I will test the blink sketch

Tested, just the blink sketch and the serial, works and blink

The behaviour you are seeing is typical memory leak or some other user code error that walks on memory.
Look into MQTT for communication channel. I use a library that send messages to both the Serial if connected and to an MQTT broker running on a Pi4. Very handy.

Hey sonofcy,

thx, I will check that.

How is that the reset not solving it, if its a memory leak?

Not sure what you mean by

Reset starts the code from the beginning, so all memory leaks are gone, but will re-appear if it's the same code.

I mean if the code itself wrong and hit the memory, then it should also do it after I reupload the sketch. Now its working fine after a reupload, but when I just reset , it was no change.

So the issue is something what can be fixed with a reupload, but not with reset.

How many times did you try to reset, did you also restart IDE, did you retry enabling/disabling the Serial monitor?
What I am trying to say is that all of these MPU/MCU have moments when they appear dead but for some unknown reason work if you try enough steps. A VERY common scenario is to have the serial monitor open in a second window or similar. Simple memory leaks will repeat, data driven memory leaks will only repeat if the same inputs in the same sequence happens.
IF rest does not fix but re-upload does, then the problem is in non-volatile memory.

Yesterday I tried to reset it a coupe of times, then this morning also 2-3 times,
then plugged into Laptop via USB, then I see that serial empty, reupload, then works, USB removed, back to battery, works also, couple of reset, still works.

The while(!serial) would make so much sense as a fail point to be honest. Its at the beginning, explain the white serial monitor. Also it should give sort of an explanation while it start working after I reupload, because then I have serial monitor connected to it.
But then the question, why its keep working after I remove it from the USB, and why start failing 2 months later. Any chance that for so long Arduino "remember" that he has a serial monitor?

I am having a bit of a problem understanding you.
I am what is known as a hardened user. After 65 years of experience with all kinds of computers, I seldom use support and if I do I quickly move up to 2nd and 3rd level. These devices are NOT reliable, they act up from time to time for no obvious reason, BUT they work 99.99999% of the time. When they act up with me, I know which buttons on the IDE to click. Just in the past several weeks I have been developing some code and it always appears to freeze, but just by clicking the two icons circled in Red the sketch will often restart. I might need to do that a dozen times before it 'catches' but that is my experience and tenacity.
I also code my Serial.begin different than you do, check this out

while (!Serial && (millis() < 5000)) ;   // Wait up to 5 secs for the Serial device to be ready

Screenshot 2024-08-05 at 06.54.30

This is classic memory leak behaviour

why start failing 2 months later.

Have a look at the following two forum links
https://forum.arduino.cc/t/arduino-reset-via-software/889153
https://forum.arduino.cc/t/how-to-soft-reset-code-snippet/897991

Hey, sorry, I try to be more clear about the explanation

Its not 2 months of constant running. This is part of an escape room, so after every team, I shut it down, then start it when new team playing. During the play time, the arduino is running from a 12V power source, not connected to my laptop, so I cannot use the IDE to reset.

Actually the code snippet you sent will solve it, if its really the while(!serial) is the problem, im just curious that if this is the case then right now, why its working properly? Its not connected to anything except the 12V Power source, so it should fail again (unless I add the minis() to the script as you mentioned).

Thx the links and code snippet, I will check them.

All the boards were Uno Rev2 WiFI ?

Hey jim-p,

yes, all of the are Uno Rev2 Wifi.
I use a couple of Rev3 without Wifi, they never had the same issue

I know that the reset sequence is different for a manual reset and when you do a download but what is inexplicable is why disconnecting power didn't reset everything.

Does the board have any non-volatile memory that maybe only gets reset on an upload?

Serial monitor opened.

So while(!Serial) does not provide any functionality and can't be the cause of the problem.