Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Connect to Wi-Fi

This tutorial will guide you through the essential steps to connect your Arduino board to a Wi-Fi network. You’ll learn how to initialize the Wi-Fi module, verify its firmware, and securely join a network using its SSID and password. Once connected, you’ll discover how to monitor important network details like your device’s IP and MAC addresses, as well as the network’s signal strength, directly from the serial console. This tutorial serves as both a practical guide to Wi-Fi connectivity and an introduction to network monitoring with Arduino, helping you establish and maintain a reliable Wi-Fi connection.

1. Upload the code

Open the 01-wifi_connect.ino file under the path of elite-explorer-kit-main\r4_new_feature\01-wifi_connect, or copy this code into Arduino IDE.

Note

Wi-Fi® support is enabled via the built-in WiFiS3 library that is shipped with the Arduino UNO R4 Core. Installing the core automatically installs the WiFiS3 library.

You still need to create or modify arduino_secrets.h, replace SECRET_SSID and SECRET_PASS with the name and password of the wifi you want to connect to. The file should contain:

arduino_secrets.h
//arduino_secrets.h header file
#define SECRET_SSID "your_ssid"
#define SECRET_PASS "your_password"
01-wifi_connect.ino
  1/*
  2  This code is designed to establish a WiFi connection using a WiFi module 
  3  on an Arduino Uno R4 board. It initializes serial communication, verifies 
  4  the presence and firmware version of the WiFi module, attempts to connect 
  5  to a specified WiFi network, and once connected, prints network information 
  6  to the serial monitor. It continuously checks the network connection every 
  7  10 seconds in the loop.
  8
  9  Board: Arduino Uno R4 
 10*/
 11
 12
 13#include <WiFiS3.h>
 14
 15#include "arduino_secrets.h" 
 16///////please enter your sensitive data in the Secret tab/arduino_secrets.h
 17char ssid[] = SECRET_SSID;        // your network SSID (name)
 18char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
 19int status = WL_IDLE_STATUS;     // the WiFi radio's status
 20
 21void setup() {
 22  //Initialize serial and wait for port to open:
 23  Serial.begin(9600);
 24  
 25  while (!Serial) {
 26    ; // wait for serial port to connect. Needed for native USB port only
 27  }
 28
 29  // check for the WiFi module:
 30  if (WiFi.status() == WL_NO_MODULE) {
 31    Serial.println("Communication with WiFi module failed!");
 32    // don't continue
 33    while (true);
 34  }
 35
 36  String fv = WiFi.firmwareVersion();
 37  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
 38    Serial.println("Please upgrade the firmware");
 39  }
 40
 41  // attempt to connect to WiFi network:
 42  while (status != WL_CONNECTED) {
 43    Serial.print("Attempting to connect to WPA SSID: ");
 44    Serial.println(ssid);
 45    // Connect to WPA/WPA2 network:
 46    status = WiFi.begin(ssid, pass);
 47
 48    // wait 10 seconds for connection:
 49    delay(10000);
 50  }
 51
 52  // you're connected now, so print out the data:
 53  Serial.print("You're connected to the network");
 54  printCurrentNet();
 55  printWifiData();
 56
 57}
 58
 59void loop() {
 60  // check the network connection once every 10 seconds:
 61  delay(10000);
 62  printCurrentNet();
 63}
 64
 65void printWifiData() {
 66  // print your board's IP address:
 67  IPAddress ip = WiFi.localIP();
 68  Serial.print("IP Address: ");
 69  
 70  Serial.println(ip);
 71
 72  // print your MAC address:
 73  byte mac[6];
 74  WiFi.macAddress(mac);
 75  Serial.print("MAC address: ");
 76  printMacAddress(mac);
 77}
 78
 79void printCurrentNet() {
 80  // print the SSID of the network you're attached to:
 81  Serial.print("SSID: ");
 82  Serial.println(WiFi.SSID());
 83
 84  // print the MAC address of the router you're attached to:
 85  byte bssid[6];
 86  WiFi.BSSID(bssid);
 87  Serial.print("BSSID: ");
 88  printMacAddress(bssid);
 89
 90  // print the received signal strength:
 91  long rssi = WiFi.RSSI();
 92  Serial.print("signal strength (RSSI):");
 93  Serial.println(rssi);
 94
 95  // print the encryption type:
 96  byte encryption = WiFi.encryptionType();
 97  Serial.print("Encryption Type:");
 98  Serial.println(encryption, HEX);
 99  Serial.println();
100}
101
102void printMacAddress(byte mac[]) {
103  for (int i = 5; i >= 0; i--) {
104    if (mac[i] < 16) {
105      Serial.print("0");
106    }
107    Serial.print(mac[i], HEX);
108    if (i > 0) {
109      Serial.print(":");
110    }
111  }
112  Serial.println();
113}

Open the serial monitor, and you will see similar content as follows. Arduino will output your device’s IP and MAC addresses, as well as the network’s signal strength.

../_images/01_1_wifi.png

2. Code explanation

  1. Including Libraries and Secret Data

    #include <WiFiS3.h>
    #include "arduino_secrets.h"
    
    • WiFiS3 is a library that provides functions for Wi-Fi connectivity. Installing the R4 core automatically installs the WiFiS3 library.

    • arduino_secrets.h is a separate file where you keep your SSID and password so they’re not exposed in your main code. Storing network and password separately reduces accidental sharing of Wi-Fi credentials.


  2. Declaring Global Variables

    char ssid[] = SECRET_SSID;
    char pass[] = SECRET_PASS;
    int status = WL_IDLE_STATUS;
    
    • ssid and pass contain your network name and password.

    • status will store the current status of your Wi-Fi connection.


  3. setup() Function

    The Serial interface is initialized with a baud rate of 9600. The while (!Serial); line makes sure that the program waits until the Serial connection is established.

    void setup() {
        //Initialize serial and wait for port to open:
        Serial.begin(9600);
        while (!Serial) {
          ; // wait for serial port to connect. Needed for native USB port only
        }
        ...
    }
    

    And then, the code checks whether the Wi-Fi module is available or not. If not, the program will halt, effectively stopping any further execution.

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

    In this part of the code, we check if the firmware version of uno R4 wifi is up to date. If it is not the latest version, a prompt for upgrade will be displayed. You can refer to Update the radio module firmware on your UNO R4 WiFi board for firmware upgrade.

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

    void loop() {
      // check the network connection once every 10 seconds:
      delay(10000);
      printCurrentNet();
    }
    
    • Every 10 seconds, the function printCurrentNet() is called to print the current network details.

Reference