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