Arduino UNO R4 Webserver Issue

Hello Group,
I have integrated a sample webserver and sample ADC code together and am having mixed results.
The program seems to work, in that it will generate the data on the webpage and also give me the "heartbeat" data on the serial monitor, but regularly it gets hung up in trying to reload the webpage. When the webpage tries to update, either automatically or manually, the ADC routine stalls and the last output on the serial monitor says "new client." The ADC data streams stops and the webpage won't load. After 30 seconds to a minute or so, it will restart.
Any thoughts what the issue might be?
Please see the code below.
Thanks.

/*

  This sketch will print the IP address of your WiFi module (once connected)
  to the Serial Monitor. From there, you can open that address in a web browser
  and see the values of ADC 0. The webpage should refresh every 5 seconds.

  This example is written for a network using WPA encryption. For
  WEP or WPA, change the WiFi.begin() call accordingly.

  Circuit:
  * Board Arduino UNO WiFi Rev.4)

 */

#include "WiFiS3.h"

#include "arduino_secrets.h"

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_WIFI_SSID;    // your network SSID (name)
char pass[] = SECRET_WIFI_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                  // your network key index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  // Map a float value from one range to another.
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

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

  analogReadResolution(10); //change adc to 10-bit resolution

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

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

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}

void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  // Retrieve analog value from pin A0:
  int adc_value = analogRead(A0);
  // Convert the analog value to a voltage (0-5V range):
  float voltage = floatMap(adc_value, 0, 1023, 0, 5);

  // Add a percentage calculation based on the adc value
  float percentage;
  int total_marks = 1023;

  percentage = (float) adc_value / total_marks * 100.0;

  // Output the analog value, corresponding voltage and percentage to the serial monitor:
  Serial.print("Analog Count: ");
  Serial.print(adc_value);
  Serial.print(", Voltage: ");
  Serial.print(voltage);
  Serial.print("V");
  Serial.print(", Percentage: ");
  Serial.print(percentage);
  Serial.println("%");
  // Wait for 2 seconds before repeating the loop:
  delay(2000);

  if (client) {                             // if you get a client,

    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out to the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("<head>");       
            client.print("<meta http-equiv=refresh content=5>");   
            client.print("</head>");
            client.print("<p style=\"font-size:48px;\">Arduino UNO R4 WiFi<br></p>");
            client.print("<p style=\"font-size:32px;\">WiFi Web Server ADC Program<br></p>");
            client.print("<p style=\"font-size:18px;\">ADC Value: </p>");
            client.print(adc_value);
            client.print("<p style=\"font-size:18px;\">Voltage: </p>");
            client.print(voltage);
            client.print("<p style=\"font-size:18px;\">Percentage: </p>");
            client.print(percentage);
            
            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

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

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

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

Did you update your R4 to the latest firmware? I had to on all mine, meant running a .bat file in Windows (outside of IDE) and everything was working after that.

1 Like

Thanks for the advice, I'll give it a try.

Gimme a sec, just looking for the instructions I used

Found it. The COM port (can only speak for Windows since that's what I have) won't be the same one as the board had in the Arduino IDE. That threw me for a bit of a loop.

1 Like

That did the trick! Thanks for the help!

1 Like

No problem :saluting_face:

1 Like