0% found this document useful (0 votes)
3 views

iot 4

The document outlines a practical experiment using NodeMCU (ESP8266) to create a web server for controlling LED brightness via a slider. It details the necessary hardware connections, software setup, and provides code for implementing the project. The conclusion highlights the successful demonstration of web server hosting and PWM control for LED brightness adjustment.

Uploaded by

biz4dwij
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

iot 4

The document outlines a practical experiment using NodeMCU (ESP8266) to create a web server for controlling LED brightness via a slider. It details the necessary hardware connections, software setup, and provides code for implementing the project. The conclusion highlights the successful demonstration of web server hosting and PWM control for LED brightness adjustment.

Uploaded by

biz4dwij
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

IOT ARCHITECTURE & PROTOCOLS (202046714)

G..H PATEL COLLEGE OF ENGINEERING AND TECHNOLOGY

Practical - 4
Aim: Building a Web Server to Control LED Brightness with a Slider using
NodeMCU (ESP8266)

Apparatus: NodeMCU (ESP8266), 1 x LED, 1 x 220Ω resistor, Breadboard, Jumper


wires, USB cable (for programming NodeMCU via Arduino IDE).

Theory:

1. Controlling an LED Using NodeMCU as a Web Server

The NodeMCU (ESP8266) can act as a web server, allowing users to


control hardware via a browser-based interface. By hosting a simple
webpage, users can adjust an LED’s brightness using a slider, which
modifies PWM (Pulse Width Modulation) values.

2. Using PWM for LED Brightness Adjustment

 Connect the LED to a PWM-enabled GPIO (e.g., D5/GPIO14 on


NodeMCU).

 Pulse Width Modulation (PWM) is used to control brightness by


adjusting the duty cycle of the signal.

 The analogWrite() function (range: 0-1023 for ESP8266) is used to


set the LED intensity dynamically.

3. Web-Based Control Interface

 A simple HTML web page will be created with a slider to control


brightness.

 The slider’s value will be sent to NodeMCU, which will adjust the
PWM output accordingly.

 This allows real-time brightness adjustment without needing to


reprogram the microcontroller.

 Procedure for Controlling LED Brightness Using NodeMCU as a Web


Server

Name: Prem Kumar Das En.No.:12202110501075


IOT ARCHITECTURE & PROTOCOLS (202046714)
G..H PATEL COLLEGE OF ENGINEERING AND TECHNOLOGY

 1. Hardware Connections

 Connect the LED:

 Anode (+) → Connect to D5 (GPIO14) via a 220Ω resistor

 Cathode (-) → Connect to GND

 2. Software Setup

 Install the ESP8266 Board Package in Arduino IDE.

 Connect NodeMCU to the PC via USB cable.

 Ensure the correct Board (NodeMCU 1.0 ESP-12E) and Port are selected
in Arduino IDE.

 3. Upload the Code

 Open Arduino IDE and enter the provided code.

 Click Upload to flash the code onto NodeMCU.

 Once uploaded, open the Serial Monitor to check the IP address assigned
to NodeMCU.

 Use this IP address in a browser to access the LED control web interface.

 .

Code :

#include <ESP8266WiFi.h>

#include <ESP8266WebServer.h>

// WiFi credentials

const char *ssid = "NodeMCU_AP";

const char *password = "12345678";

// Define LED pin (PWM-capable)

Name: Prem Kumar Das En.No.:12202110501075


IOT ARCHITECTURE & PROTOCOLS (202046714)
G..H PATEL COLLEGE OF ENGINEERING AND TECHNOLOGY

const int LED_PIN = D5; // GPIO14

// Create a web server on port 80

ESP8266WebServer server(80);

// Function to serve the web page

void handleRoot() {

String html = "<html>\

<head>\

<title>LED Brightness Control</title>\

<script>\

function updateBrightness(value) {\

var xhttp = new XMLHttpRequest();\

xhttp.open('GET', '/setBrightness?value=' + value, true);\

xhttp.send();\

}\

</script>\

</head>\

<body>\

<h1>Control LED Brightness</h1>\

<input type='range' min='0' max='1023' value='512'


oninput='updateBrightness(this.value)'>\

</body>\

</html>";

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

Name: Prem Kumar Das En.No.:12202110501075


IOT ARCHITECTURE & PROTOCOLS (202046714)
G..H PATEL COLLEGE OF ENGINEERING AND TECHNOLOGY

// Function to set brightness

void handleBrightness() {

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

int brightness = server.arg("value").toInt();

analogWrite(LED_PIN, brightness);

server.send(200, "text/plain", "Brightness set to " + String(brightness));

} else {

server.send(400, "text/plain", "Bad Request");

void setup() {

Serial.begin(115200);

// Set LED pin as output

pinMode(LED_PIN, OUTPUT);

// Start Access Point

WiFi.softAP(ssid, password);

Serial.print("AP IP Address: ");

Serial.println(WiFi.softAPIP());

// Define URL handlers

server.on("/", handleRoot);

server.on("/setBrightness", handleBrightness);

// Start the web server


Name: Prem Kumar Das En.No.:12202110501075
IOT ARCHITECTURE & PROTOCOLS (202046714)
G..H PATEL COLLEGE OF ENGINEERING AND TECHNOLOGY

server.begin();

void loop() {

server.handleClient();

Conclusion:

1. This experiment successfully demonstrated how to use NodeMCU as a web


server to control LED brightness using a slider-based interface.

2. The project showcased the integration of Soft AP mode, web server hosting, and
PWM control.

Name: Prem Kumar Das En.No.:12202110501075

You might also like