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