How to disconnect and reconnect WiFi using WiFiS3 on Arduino Uno R4 WiFi

Hi everyone,

I'm using the Arduino Uno R4 WiFi together with the WiFiS3 library.
The connection works fine in general, but I don't always need WiFi running.
To reduce stress on the board and save resources, I would like to disconnect from WiFi temporarily and reconnect later when needed.

Unfortunately, the WiFiS3 library lacks proper documentation, and I couldn't find any examples on how to cleanly disconnect or reconnect the WiFi.

Has anyone here worked with this library and knows how to handle this properly?
Any help or code snippets would be really appreciated!

Thanks in advance!

Probably most forum members are not familiar with this library. So it would be a good idea for you to post a link to whatever documentation pages you have.

WiFiS3 is the default for UNO R4. <WiFiS3.h> just has #include <WiFi.h> for the file in the same directory. Also, WiFi.begin ends with its own "wait to connect" loop, so you don't need your own in most cases. Therefore, toggling the WiFi looks like

#include <WiFi.h>
#include "arduino_secrets.h"

void setup() {
  Serial.begin(115200);
}

void loop() {
  switch (WiFi.status()) {
    case WL_IDLE_STATUS:
      Serial.println("idle; trying to connect");
      if (WiFi.begin(SECRET_SSID, SECRET_PASS)) {
        Serial.println(WiFi.localIP());
      } else {
        Serial.println("failed to connect");
      }
      break;
    case WL_CONNECTED:
      WiFi.end();
      Serial.println("ended");
      break;
    default:
      Serial.print("unexpected status: ");
      Serial.println(WiFi.status());
  }
  delay(7500);
}

Note that there is a WL_DISCONNECTED status, but when not connected, it is WL_IDLE_STATUS instead. After compiling, you can right-click on either WL_ constant and choose Go to Definition to see the wl_status_t enum.

you can see other WiFi libraries documentation for basic methods

https://docs.arduino.cc/libraries/wifi/
https://docs.arduino.cc/libraries/wifinina/

and a look into the header file helps too

https://github.com/arduino/ArduinoCore-renesas/blob/main/libraries/WiFiS3/src/WiFi.h#L246

I only have this link as documentation:
UNO R4 WiFi Network Examples | Arduino Documentation

Thank you for the tip with looking in the header files!
I found the methods disconnect() and end() there. I will try it out next week :slight_smile:

The most correct documentation will always be the source code. NOTE beware of comments that are not maintained with the code, I always delete or cover them up since 99% of programmers never maintain the comments.