WiFi
secrets.h
// Header file with secret values maintained separate from code.
#define WIFI_SSID "your-wifi-ssid"
#define WIFI_PASSWORD "your-wifi-password"
wifi.ino
/*
Project: Smart Home
Component: Arduino Ek R4 Built-in WiFi
Sketch Description: This sketch has functions to connect to an available unencrypted WiFi network with an id and password and print the MAC address of the WiFi module, the IP address obtained, and other network details.
Author: STEMVentor Educonsulting
This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs. This code is for educational purposes only and is not for production use.
*/
// LIBRARIES
// The WiFi library is loaded along with the UNO R4 board package.
// This is a different library from the one used by the standalone ESP32 board.
#include <WiFiS3.h>
// You should save your WiFi access id and password in a separate file
// so that it is not shared with the rest of the code.
// Use "" when including library files in same directory as against <> for library files in global directory.
#include "secrets.h"
char ssid[] = WIFI_SSID; // your network SSID (name)
char pass[] = WIFI_PASSWORD; // your network password (use for WPA, or use as key for WEP)
// PIN DEFINTIONS
// GLOBAL CONSTANTS
// GLOBAL VARIABLES
int wifi_radio_status = WL_IDLE_STATUS; // The on-board WiFi radio status.
// INITIALIZE OBJECTS
/* Libraries usually follow an object-oriented approach that requires
* an instance of the class to call its methods.
*/
// LOCAL FUNCTIONS
void WiFiSetup() {
// Check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// Don't continue
while (true);
}
// This is an advanced use case, can be skipped.
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// Attempt to connect to WiFi network
while (wifi_radio_status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
wifi_radio_status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// Connected
is_wifi_connected = true;
// Display WiFi on LED Matrix
displayWiFiOnLEDMatrix();
Serial.print("You're connected to the network");
// printCurrentNet();
// printWifiData();
}
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void printMacAddress(byte mac[]) {
if(OPERATING_MODE == 'D'){
for (int i = 0; i < 6; i++) {
if (i > 0) {
Serial.print(":");
}
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
}
Serial.println();
}
}
/*
* Function to decode the WiFi encryption type code
* Not really necessary since the access point to connect to is hardcoded.
* Needed if offering a choice of networks to connect to.
*/
// String translateEncryptionType(wifi_auth_mode_t encryptionType) {
// switch (encryptionType) {
// case (WIFI_AUTH_OPEN):
// return "Open";
// case (WIFI_AUTH_WEP):
// return "WEP";
// case (WIFI_AUTH_WPA_PSK):
// return "WPA_PSK";
// case (WIFI_AUTH_WPA2_PSK):
// return "WPA2_PSK";
// case (WIFI_AUTH_WPA_WPA2_PSK):
// return "WPA_WPA2_PSK";
// case (WIFI_AUTH_WPA2_ENTERPRISE):
// return "WPA2_ENTERPRISE";
// }
// }
/*
* Function to scan for available WiFi networks and print their name, signal strength and MAC address.
* Not really necessary since the access point to connect to is hardcoded.
* Needed if offering a choice of networks to connect to.
*/
// void scanNetworks() {
// int numberOfNetworks = WiFi.scanNetworks();
// Serial.print("Number of networks found: ");
// Serial.println(numberOfNetworks);
// for (int i = 0; i < numberOfNetworks; i++) {
// Serial.print("Network name: ");
// Serial.println(WiFi.SSID(i));
// Serial.print("Signal strength: ");
// Serial.println(WiFi.RSSI(i));
// Serial.print("MAC address: ");
// Serial.println(WiFi.BSSIDstr(i));
// Serial.print("Encryption type: ");
// String encryptionTypeDescription = translateEncryptionType(WiFi.encryptionType(i));
// Serial.println(encryptionTypeDescription);
// Serial.println("-----------------------");
// }
// }