In this tutorial, we will be looking at NodeMCU ESP32 basics where we will learn how to connect to a WiFi, getting MAC address, RSSI and also setting a static IP for ESP32 module.
Below is the first code that we will be using :
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "WIFI NAME HERE";
char key[] = "WIFI PASSWORD HERE";
int status = WL_IDLE_STATUS;
void setup() {
Serial.begin(115200);
while (!Serial) {
}
WiFi.mode(WIFI_STA);
/*
Status Meaning
WL_IDLE_STATUS temporary status assigned when WiFi.begin() is called
WL_NO_SSID_AVAIL when no SSID are available
WL_SCAN_COMPLETED scan networks is completed
WL_CONNECTED when connected to a WiFi network
WL_CONNECT_FAILED when the connection fails for all the attempts
WL_CONNECTION_LOST when the connection is lost
WL_DISCONNECTED when disconnected from a network
*/
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to Wifi");
status = WiFi.begin(ssid, key);
delay(10000);
}
// once you are connected :
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
}
void loop() {
delay(10000);
printCurrentNet();
}
void printWifiData() {
// print your WiFi 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: ");
Serial.print(mac[5], HEX);
Serial.print(":");
Serial.print(mac[4], HEX);
Serial.print(":");
Serial.print(mac[3], HEX);
Serial.print(":");
Serial.print(mac[2], HEX);
Serial.print(":");
Serial.print(mac[1], HEX);
Serial.print(":");
Serial.println(mac[0], HEX);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
//print Signal Strength
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
}
The successful execution of this program will display the WiFI SSID, IP and MAC Address along with RSSI.
For the 2nd part of this tutorial, we will be using the below code :
#include <WiFi.h>
char ssid[] = "WIFI NAME HERE";
char key[] = "WIFI PASSWORD HERE";
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, key);
Serial.println("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.print("Original IP: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
initWiFi();
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
// Set your Static IP address
IPAddress local_IP(192, 168, 1, 111);
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8); // optional
IPAddress secondaryDNS(8, 8, 4, 4); // optional
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
Serial.print("Customized IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
// put your main code here, to run repeatedly:
}
When setting a customized IP, it is recommended to use your local IPAddress gateway and subnet.
Successful run of the code above will give the original IP and the successfully changed custom IP.
For more detailed explanation on this tutorial, feel free to check out my YouTube video below :
Hope you enjoyed this tutorial. If you have any questions or comments, feel free to leave it in the comment below.
Don't forget to Like and Subscribe :)
Comments