Professional Documents
Culture Documents
Simple OTA LED Control
Simple OTA LED Control
Simple OTA LED Control
Materials:
Circuit Connection:
1. Connect the positive leg of the LED (longer leg) to digital pin D1 on the ESP8266.
2. Connect the negative leg of the LED (shorter leg) to one end of the resistor.
3. Connect the other end of the resistor to the GND (ground) pin on the ESP8266.
Code:
cpp
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
ArduinoOTA.setHostname("LED_Control");
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
ArduinoOTA.handle();
// Add your additional code here
}
void handleLedControl() {
String ledState = server.arg("state");
if (ledState == "on") {
digitalWrite(LED_BUILTIN, HIGH);
} else if (ledState == "off") {
digitalWrite(LED_BUILTIN, LOW);
}
server.send(200, "text/plain", "LED state: " + ledState);
}
Make sure you have installed the necessary libraries by going to "Sketch" -> "Include
Library" -> "Manage Libraries" and searching for the following libraries:
ESP8266WiFi
ESP8266mDNS
WiFiUdp
ArduinoOTA
Instructions:
1. Connect the ESP8266 to your computer via USB and upload the code using the
Arduino IDE.
2. After successful uploading, open the Serial Monitor to view the ESP8266's IP address.
3. Install the "ESP8266 Sketch Data Upload" plugin in the Arduino IDE by going to
"Sketch" -> "Include Library" -> "Manage Libraries" and searching for "ESP8266
Sketch Data Upload". Install the plugin and restart the Arduino IDE.
4. Create a new folder called "data" in the same directory as your sketch and create a
blank file called "index.html" inside the "data" folder.
5. In the "index.html" file, add the following HTML code:
html
<!DOCTYPE html>
<html>
<head>
<title>LED Control</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h1>LED Control</h1>
<button onclick="toggleLED('on')">Turn On</button>
<button onclick="toggleLED('off')">Turn Off</button>
<script>
function toggleLED(state) {
$.ajax({
url: "/led",
method: "POST",
data: { state: state },
success: function(result) {
console.log(result);
}
});
}
</script>
</body>
</html>
1. Click on "Tools" -> "ESP8266 Sketch Data Upload" to upload the HTML file to the
ESP8266.
2. Once the upload is complete, disconnect the ESP8266 from the computer and connect
it to a power source.
3. Open a web browser on a device connected to the same network as the ESP8266 and
enter the IP address of the ESP8266 in the address bar.
4. You should see a web page with "LED Control" title and two buttons to turn the LED
on and off. Clicking the buttons will control the LED remotely.
That's it! You have successfully created a simple OTA project to control an LED remotely
using the ESP8266 and OTA updates.