ESP32 Expeditions:Chapter-3 ESP32 LED ON/OFF in STA Mode

Once upon a time, in a magical land ๐Ÿฐ, there was a wizard ๐Ÿง™โ€โ™‚๏ธ who wanted to control a light using his special powers. To do this, he created a magical server that could listen to messages on a special number, which was 80.

First, the wizard needed a helper, so he summoned a special friend ๐Ÿง™โ€โ™‚๏ธ named โ€œclientโ€ to assist him with incoming messages. Together, they embarked on a quest to control a light using these messages.

In their preparation phase ๐Ÿš€, the wizard connected to a Wi-Fi network with a secret name (โ€œShemanto Wifiโ€) and a secret password (โ€œssssssssโ€). They had to try to connect every 2 seconds, but they never gave up until they were successfully connected ๐ŸŒ. Once they were connected, they celebrated ๐ŸŽ‰ and proudly told everyone their address ๐Ÿก.

Now, onto the magical part โœจ! They started their magical server on port 80, which meant they were ready to receive messages from anyone who wanted to communicate with them.

In their daily routine, they constantly checked if there was a new friend knocking at their door ๐Ÿšช. If a friend arrived (a client connected), they created a special box ๐Ÿ’Œ to collect their friendโ€™s message.

They were great listeners ๐ŸŽต! They listened to their friend as long as the friend stayed connected. Whenever a character arrived, they read it like it was a page from a book ๐Ÿ“œ.

They had a special way of recognizing when a new line character (a โ€œnewlineโ€) appeared ๐ŸŒŸ. Whenever they saw it, they would share their friendโ€™s message with the world ๐Ÿ“ฃ. But before doing anything magical ๐Ÿช„ with the message, they checked if it was about turning the light on or off.

If the message said โ€œGET /ledON HTTP/1.1,โ€ they used their magical powers to turn the light on by sending a signal to pin number 2 (the LED pin). And if the message said โ€œGET /ledOFF HTTP/1.1,โ€ they turned the light off. They also made sure to clear their special message box ๐Ÿงน for the next message.

After sharing the message and performing their magic, they politely said goodbye to their friend and closed the door ๐Ÿšช.

And so, the wizard and his special friend were able to control the light using the magical server and live happily ever after in their world of code! ๐ŸŒŸ๐Ÿง™โ€โ™‚๏ธ๐Ÿ”ฎ๐Ÿฐ๐ŸŽ‰

#include <WiFi.h>
#include <WiFiServer.h>

#define LED 2

// ๐Ÿฐ 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() {
  pinMode(LED, OUTPUT);
  // ๐Ÿš€ 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("Shemanto Wifi", "ssssssss");

  // ๐Ÿ”„ 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

          if (request.indexOf("GET /ledON HTTP/1.1") != -1)
          {
            digitalWrite(LED, HIGH);
          }

          if (request.indexOf("GET /ledOFF HTTP/1.1") != -1)
          {
            digitalWrite(LED, LOW);
          }

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