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();
  }
}
0
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

Shemanto's Coding Tales
Shemanto's Coding Tales