Group Assignment Internet of Things
Controlling LEDs Connected to
NodeMCU from a Web Interface
Ariadi Nugroho, PhD
Objective:
The goal of this project is to control multiple LEDs connected to a NodeMCU using a simple
web interface. The NodeMCU will serve as a web server, allowing users to turn LEDs on and
off by interacting with a webpage.
Prerequisites:
• Basic knowledge of the Arduino IDE and ESP8266/ESP32.
• Familiarity with HTML and basic web programming.
• Understanding of how to connect LEDs to the NodeMCU.
Materials Required:
1. NodeMCU (ESP8266 or ESP32)
2. 2 LEDs (of different colors)
3. 220Ω Resistors (one per LED)
4. Breadboard and Jumper Wires
5. Micro USB cable (for programming NodeMCU)
Step-by-Step Procedure (just as a guide, can be adjusted as necessary):
1. Hardware Setup :
Circuit Connections:
• LED 1: Connect the anode (+) of LED 1 to the D1 (GPIO 5) pin of the NodeMCU. Connect the
cathode (-) of LED 1 to ground through a 220Ω resistor.
• LED 2: Connect the anode (+) of LED 2 to the D2 (GPIO 4) pin of the NodeMCU. Connect the
cathode (-) of LED 2 to ground through another 220Ω resistor.
2. Software Setup:
Step 1: Install ESP8266 Library in Arduino IDE
1. Open Arduino IDE.
2. Go to File > Preferences.
3. In the Additional Boards Manager URLs field, enter this URL:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
4. Go to Tools > Board > Boards Manager, search for ESP8266, and install it.
Step 2: Write the NodeMCU Code (this is a sample code, you are allowed to make adjustments
as necessary)
#include <ESP8266WiFi.h>
// Replace with your network credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
// Define the LED pins
int LED1 = 5; // D1 pin
int LED2 = 4; // D2 pin
WiFiServer server(80); // Start the web server on port 80
void setup() {
// Start serial communication for debugging
Serial.begin(115200);
delay(10);
// Initialize the LED pins as outputs
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
// Connect to Wi-Fi
Serial.println();
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Start the server
server.begin();
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("New Client.");
String currentLine = ""; // String to hold incoming data
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c); // Print received data
// If the user has pressed enter (end of line)
if (c == '\n') {
// Process request to control LEDs
if (currentLine.length() == 0) {
// Send the web page
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE html><html>");
client.println("<head><title>LED Control</title></head>");
client.println("<body><h1>Control LEDs from the Web</h1>");
client.println("<p><a href=\"/LED1/ON\"><button>Turn ON LED
1</button></a></p>");
client.println("<p><a href=\"/LED1/OFF\"><button>Turn OFF LED
1</button></a></p>");
client.println("<p><a href=\"/LED2/ON\"><button>Turn ON LED
2</button></a></p>");
client.println("<p><a href=\"/LED2/OFF\"><button>Turn OFF LED
2</button></a></p>");
client.println("</body></html>");
break;
}
currentLine = "";
} else if (c != '\r') {
currentLine += c;
}
// Control LED based on URL
if (currentLine.indexOf("GET /LED1/ON") >= 0) {
Serial.println("Turning LED 1 ON");
digitalWrite(LED1, HIGH);
} else if (currentLine.indexOf("GET /LED1/OFF") >= 0) {
Serial.println("Turning LED 1 OFF");
digitalWrite(LED1, LOW);
} else if (currentLine.indexOf("GET /LED2/ON") >= 0) {
Serial.println("Turning LED 2 ON");
digitalWrite(LED2, HIGH);
} else if (currentLine.indexOf("GET /LED2/OFF") >= 0) {
Serial.println("Turning LED 2 OFF");
digitalWrite(LED2, LOW);
}
}
}
// Close the connection
client.stop();
Serial.println("Client disconnected.");
}
Step 3: Upload Code to NodeMCU
1. Connect the NodeMCU to your computer via a USB cable.
2. Select the correct Board (NodeMCU 1.0) and Port in the Arduino IDE.
3. Upload the code to the NodeMCU.
3. Accessing the Web Interface:
After successfully uploading the code, the NodeMCU will connect to your Wi-Fi network.
The IP address of the NodeMCU will be printed in the Serial Monitor. Enter this IP address
into any web browser (e.g., http://192.168.1.10) to access the web interface. The interface
will have buttons to:
• Turn ON LED 1
• Turn OFF LED 1
• Turn ON LED 2
• Turn OFF LED 2
Optional Enhancements:
• Add more LEDs: Extend the project by adding more LEDs and controlling them from the
web interface.
• Real-time Status Updates: Show the current status (ON/OFF) of each LED on the webpage.
• CSS Styling: Enhance the user interface with CSS to make the web page more visually
appealing.
Submission :
§ Submit all deliverables via email : [email protected]
§ Submission Deadline : 12 Oct 2024
§ Deliverables :
o Project document (presentation slides) that describes :
§ Problem statement
§ Approach and Solution (IC diagram, screenshots, etc)
§ Challenges Faced
§ Potential Future Improvements / Enhancements.
o Arduino sketch (source code)