Control led light remotely using Nodemcu esp8266

 In this project, we are going to turn a led on or off and talk about the protocol involved. First let's set up our project.

COMPONENTS REQUIRED:

NodeMCU esp8266, 9 V battery, IC7805 Voltage Regulator, 330 ohm resistor, led, connecting wires.

PROJECT SETUP:

Step 1: Setup the arduino IDE to upload the code to NodeMCU esp8266 (I've given another post on how to setup your ide to upload your code in detail)

Step 2: Copy this code and paste it in your IDE and upload it to your NodeMCU esp8266

  #include <ESP8266WiFi.h>

  #include <ESP8266WebServer.h> // Replace with your network credentials

  const char* ssid = "xxxxx";

  const char* password = "xxxxxx";

  ESP8266WebServer server(80);

  const int ledPin = D0; // Define the pin connected to the LED

  void handleRoot() {

  String status;

  if (server.hasArg("on")) {

    digitalWrite(ledPin, HIGH);

    status = "LED is ON";

  } else if (server.hasArg("off")) {

    digitalWrite(ledPin, LOW);

    status = "LED is OFF";

  }

   String html = "<html><body>";

  html += "<h1>ESP8266 LED Control</h1>";

  html += "<p>Current LED Status: " + status + "</p>";

  html += "<p><a href='?on'>Turn On</a></p>";

  html += "<p><a href='?off'>Turn Off</a></p>";

  html += "</body></html>";

 server.send(200, "text/html", html);

}

void setup() {

  Serial.begin(115200);

  pinMode(ledPin, OUTPUT);

  digitalWrite(ledPin, LOW); // Initialize LED as off

  // Connect to Wi-Fi

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);

    Serial.println("Connecting to WiFi...");

  }

  Serial.println("Connected to WiFi");

 Serial.print("IP address: ");

  Serial.println(WiFi.localIP());

  // Define web server routes

  server.on("/", handleRoot);

  // Start the server

  server.begin();

  Serial.println("HTTP server started");

}

void loop() {

  server.handleClient();

}

The code will be uploaded just fine in this setting:


Note: Choose your COM port after verifying it in the drive manager.


Note: Just connect the nodemcu directly without led


After uploading the screen will look like this:


Step 3: Open your serial monitor and then press the reset button in your nodeMCU. After pressing reset you can see this in your serial monitor:

Press Reset:


Step 4: Copy the IP Address and paste it in the google or any web browser. You'll get this webpage.


Step 5: Remove NodeMCU esp8266 from the USB-B cable and give connections as per this diagram:


Note: The output voltage from NodeMCU esp8266 will be 5V which will damage the led overtime. So, to protect the led we are using a 330 ohm resistor. Since the voltage required to turn on a led differs according to its color you might want to change the resistor value if needed. Similarly, IC7805 is used to supply a constant voltage of 5 V to NodeMCU esp8266.


Our project setup is now ready!

TEST:

Now when you click 'Turn On' on the browser, your led should turn on and viceversa.

ENHANCEMENT:

If you want to control the led using your mobile phone, try MIT Inventor app at https://appinventor.mit.edu/ 

Step 1: Click on the button- Create Apps!

Step 2: After you login using your credentials and agree to the terms and conditions, you will get this screen:

Step 3: Click on continue and close any screen that pops up, click on New Project and give a project name and then you will get a screen like this:

Step 4: Drag and drop webviewer from the user interfaces as shown below:
Step 5: And then go back to the webpage with the IP and copy it:


Step 6: Paste it in the HomeUrl field in the properties tab:


Step 7: Now, go to the 'AI Companion' option in the 'Connect' tab:


Step 8: A screen like this will appear. 
Step 9: Install the MIT Inventor app in your android or IOS and open it.

Step 10: You will get a screen like this:

Step 11: You can either connect with the given code or you can scan QR code. 

After this, you will be able to view this tab on your phone and be able to control the led from it.











The principle behind its working is based on the HTTP protocol.

Working Demo:































Comments

Popular posts from this blog

Hello! It's me!

INSTALLATION OF MICROPYTHON ON NODEMCU esp8266