ESP32 Expeditions:Chapter- 2 ESP32 Web Server (STA_Mode)
Once upon a time in the realm of technology, there was a bright and inquisitive programmer named Shemanto Sharkar ๐งโ๐ป. Shemantoโs heart was set on creating a device that could connect to the enchanting world of the internet. To bring this dream to life, Shemanto wielded a magical tool known as the ESP32board ๐งฐ.
Shemantoโs journey began in a cozy workshop filled with an array of electronic wonders and gadgets. With a sense of purpose and determination in their eyes, they set out to craft a mystical code. This code would serve as the key to open the gateway to the internet.
In their quest, Shemanto summoned the assistance of the WiFi library ๐, a source of powerful incantations for connecting to the digital realm. The library enabled them to communicate with the WiFi network nearby, akin to deciphering spells from an ancient tome.
With their spirits high, Shemanto embarked on the coding adventure ๐. They initiated their communication with the computer, akin to engaging in a dialogue with a wise oracle.
Their first task was to establish a connection with a WiFi network by providing the networkโs name (SSID) and secret code (password). This process felt like sending a message to a distant WiFi tower, hoping for a magical response ๐ก.
Yet, as is often the case in grand adventures, challenges arose. Sometimes, the WiFi network was reticent, and the connection proved elusive. Undeterred, Shemanto incorporated a special loop to persistently attempt a connection, patiently waiting for the moment of success ๐.
With each attempt, they would announce to the computer, โHey, Iโm trying to make a new friend!โ ๐ฃ, followed by a brief pause of two seconds before making another endeavor โณ.
Finally, the moment of triumph arrived. Shemantoโs device successfully befriended the WiFi network, and they couldnโt contain their excitement. They exclaimed, โConnected! Good to Go!โ ๐, celebrating their achievement.
With a sense of pride, they shared their unique internet address, as if revealing a hidden treasure map ๐ก. It was a symbol of their conquest in the digital realm.
As Shemanto gazed upon their code, they knew that their adventure had only just begun. The โloopโ section of the code was akin to an empty canvas, waiting to be adorned with magical tasks. Shemanto eagerly anticipated the limitless possibilities that awaited them on their journey through the digital landscape. With unwavering determination, Shemanto Sharkar embarked on their coding adventure, ready to leave their mark in the boundless world of technology. ๐๐๐ก๐ก๐
#include <WiFi.h>
#include <WiFiServer.h>
// ๐ฐ We create a magical server that listens on port 80
WiFiServer server(80);
// ๐งโโ๏ธ We have a special friend (client) to help us with incoming messages
WiFiClient client;
void setup() {
// ๐ We prepare to talk and understand our friend's messages
Serial.begin(115200);
// ๐ We try to connect to the Wi-Fi network with a secret name and password
WiFi.begin("Name", "Password");
// ๐ While we're not connected, we keep trying every 2 seconds
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Trying to Connect!");
delay(2000);
}
Serial.println();
Serial.println("Connected! Good to Go!"); // ๐ Hooray, we're connected!
Serial.println(WiFi.localIP()); // ๐ก We proudly tell everyone our address
// โจ We start our magical server
server.begin();
}
void loop() {
// ๐ช We check if there's a new friend knocking at our door (client connected)
client = server.available();
if (client) {
// ๐ We create a special box for our friend's message
String request = "";
// ๐ต We listen to our friend as long as they're connected
while (client.connected()) {
if (client.available()) {
char c = client.read(); // ๐ We read one character from our friend
// ๐ We check if the character is a special "newline"
if (c == '\n') {
// ๐ฃ When we see the "newline," we share our friend's message with the world
Serial.println(request);
// ๐ช You can do some special magic here to do things with the message
request = ""; // ๐งน We clear our special box for the next message
} else {
request += c; // ๐งฉ If it's not a "newline," we add the character to our message box
}
}
}
// ๐ช After we've shared the message, we say goodbye to our friend and close the door
client.stop();
}
}
Subscribe to my newsletter
Read articles from Shemanto's Coding Tales directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by