This example program demonstrates the complete process of connecting to WiFi using the ESP32 development board in Station (STA) mode. It displays the connection status via the TFT LCD screen and shows network information upon successful connection, including SSID, IP address, and MAC address.
This example code is based on the ESP32-WROOM-32E. The operation logic is applicable to all ESP32 series development boards.
The following sections cover the program initialization and core configuration parts:
#include <TFT_eSPI.h>
#include <WiFi.h>
//Manually modifying parameters
const char *ssid = "ssid";
const char *password = "password";
char t_buf[100] = {0};
int i = 0;
TFT_eSPI my_lcd = TFT_eSPI();
TFT_eSPI.h (screen driver library) and WiFi.h (WiFi functionality library) for LCD display and network connection features, respectively.ssid and password variables to store the WiFi network name and password to connect to. Users need to manually modify these according to their environment.t_buf for formatted string storage and an integer i for connection timeout counting.TFT_eSPI object my_lcd as the entry point for screen operations.The setup() function completes the screen initialization, WiFi configuration, and connection attempt. It runs once when the ESP32 is powered on or reset.
void setup()
{
Serial.begin(115200);
my_lcd.begin();
my_lcd.setRotation(0);
my_lcd.fillScreen(TFT_WHITE);
my_lcd.setTextColor(TFT_RED);
WiFi.mode(WIFI_STA);
WiFi.setSleep(false);
my_lcd.drawString("Start connecting to WiFi...", 22,85,2);
WiFi.begin(ssid, password);
while(WiFi.status()!= WL_CONNECTED)
{
i++;
delay(500);
Serial.print(".");
if(i>360)
{
break;
}
}
if(i>360)
{
while(1)
{
my_lcd.fillRect(22, 85, my_lcd.width()-1, 25,TFT_WHITE);
delay(500);
my_lcd.drawString("connection failed!!!", 22,85,2);
delay(500);
}
}
else
{
my_lcd.fillRect(22, 85, my_lcd.width()-1, 25,TFT_WHITE);
my_lcd.setTextColor(TFT_GREEN);
my_lcd.drawString("Connection successful!!!", 22,85,2);
my_lcd.setTextColor(TFT_BLUE);
sprintf(t_buf, "SSID : %s", ssid);
my_lcd.drawString(t_buf, 22,105,2);
sprintf(t_buf, " IP : %s", WiFi.localIP().toString().c_str());
my_lcd.drawString(t_buf, 22,125,2);
sprintf(t_buf, " MAC : %s", WiFi.macAddress().c_str());
my_lcd.drawString(t_buf, 22,145,2);
}
}
Serial.begin(115200);
my_lcd.begin();
my_lcd.setRotation(0);
my_lcd.fillScreen(TFT_WHITE);
Serial.begin(115200): Sets the serial baud rate to 115200 for debug message output.my_lcd.begin(): Initializes the TFT screen.my_lcd.setRotation(0): Sets the screen orientation to the default (portrait/landscape mode 0).my_lcd.fillScreen(TFT_WHITE): Fills the entire screen with white as the background.WiFi.mode(WIFI_STA);
WiFi.setSleep(false);
my_lcd.drawString("Start connecting to WiFi...", 22,85,2);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA): Sets the ESP32 to Station (client) mode to connect to a router.WiFi.setSleep(false): Disables WiFi sleep mode to ensure connection stability (optional optimization).WiFi.begin(ssid, password): Starts attempting to connect to the specified WiFi network.while(WiFi.status()!= WL_CONNECTED)
{
i++;
delay(500);
Serial.print(".");
if(i>360)
{
break;
}
}
This is a loop-wait process used to check the WiFi connection status:
WiFi.status() equals WL_CONNECTED.i by 1.i > 360 (approximately 180 seconds), it forcibly exits the loop to prevent the program from waiting indefinitely.if(i>360)
{
while(1)
{
my_lcd.fillRect(22, 85, my_lcd.width()-1, 25,TFT_WHITE);
delay(500);
my_lcd.drawString("connection failed!!!", 22,85,2);
delay(500);
}
}
If the connection times out, the program enters an infinite loop:
else
{
my_lcd.fillRect(22, 85, my_lcd.width()-1, 25,TFT_WHITE);
my_lcd.setTextColor(TFT_GREEN);
my_lcd.drawString("Connection successful!!!", 22,85,2);
my_lcd.setTextColor(TFT_BLUE);
sprintf(t_buf, "SSID : %s", ssid);
my_lcd.drawString(t_buf, 22,105,2);
sprintf(t_buf, " IP : %s", WiFi.localIP().toString().c_str());
my_lcd.drawString(t_buf, 22,125,2);
sprintf(t_buf, " MAC : %s", WiFi.macAddress().c_str());
my_lcd.drawString(t_buf, 22,145,2);
}
If the connection is successful, the program displays the network information on the screen:
The loop() function is the main loop of the Arduino program, running indefinitely after setup() is executed.
void loop()
{
delay(1000);
}
Since this example only demonstrates the connection process, the loop() is empty and only includes a 1-second delay. In actual applications, you can add network-based functional code here (such as an HTTP server, data upload, etc.).
This example program demonstrates the ESP32's connection process as a WiFi client through the following steps:
Key functions used in the program include:
WiFi.mode(): Sets the WiFi operating mode.WiFi.begin(): Starts the WiFi connection.WiFi.status(): Gets the current connection status.WiFi.localIP(): Gets the device's IP address.WiFi.macAddress(): Gets the device's MAC address.my_lcd.drawString(): Draws text on the screen.sprintf(): Formats strings.If you need to modify the code to suit your own environment, you can refer to the following aspects:
Modify WiFi Credentials:
ssid and password variables at the top of the code to your own WiFi name and password.Adjust Timeout Period:
if(i>360). The current value is 360, representing 180 seconds (360 * 500ms).Modify Screen Display Content:
my_lcd.drawString() to display custom information.Change Display Colors:
my_lcd.setTextColor() (e.g., TFT_RED, TFT_GREEN, etc.).Add New Features:
loop() function.