Introduction
This experiment demonstrates how to set up a simple web server using the ESP8266 Wi-Fi module. The web server can control outputs such as LEDs or other devices over a web page.
Components Needed
- ESP8266 Wi-Fi Module
- Arduino (e.g., Uno, Nano)
- LED
- 220-ohm Resistor
- Jumper wires
Circuit Setup
- Connect the VCC pin of the ESP8266 to the 3.3V pin of the Arduino.
- Connect the GND pin of the ESP8266 to the GND pin of the Arduino.
- Connect the LED to pin 2 of the Arduino with the 220-ohm resistor in series.
Make sure the connections are correct before proceeding.
Code for ESP8266 Web Server
Upload the following code to your Arduino to create a web server that controls the LED:
#include
const char* ssid = "yourSSID";
const char* password = "yourPassword";
WiFiServer server(80);
const int ledPin = 2;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
server.begin();
pinMode(ledPin, OUTPUT);
}
void loop() {
WiFiClient client = server.available();
if (client) {
String request = client.readStringUntil('\r');
client.flush();
if (request.indexOf("/led/on") != -1) {
digitalWrite(ledPin, HIGH);
}
if (request.indexOf("/led/off") != -1) {
digitalWrite(ledPin, LOW);
}
client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
client.print("ESP8266 Web Server
");
client.stop();
}
}
Explanation
This code sets up a web server on the ESP8266. When you visit the IP address of the ESP8266, you can control the state of the LED by clicking on links.
Troubleshooting
- If the web server isn't reachable, ensure the ESP8266 is correctly connected to the Wi-Fi network and check the serial monitor for any error messages.
- If the LED doesn't respond, verify the circuit wiring and ensure the code is correctly uploaded to the Arduino.