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

esp8266-servo

This document is an Arduino sketch for controlling a servo motor using an ESP8266 microcontroller. It sets up a web server that allows users to input direction and rotations to move the servo, and it includes a simple HTML interface for user interaction. The code also handles WiFi connection and mDNS setup for easier access to the server.

Uploaded by

Wizard Last
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

esp8266-servo

This document is an Arduino sketch for controlling a servo motor using an ESP8266 microcontroller. It sets up a web server that allows users to input direction and rotations to move the servo, and it includes a simple HTML interface for user interaction. The code also handles WiFi connection and mDNS setup for easier access to the server.

Uploaded by

Wizard Last
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <ESP8266WiFi.

h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <StreamString.h>
#include <Servo.h>

const char *ssid = "Thanh Nguyen";


const char *password = "08022004";

Servo myServo;
int angle = 0;

ESP8266WebServer server(80);

void handleRoot() {
String content = "<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>ESP8266 Demo</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-
Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>ESP8266 Servo Control</h1>\
<form>\
<label for=\"direction\">Direction:</label>\
<input type=\"text\" id=\"direction\" name=\"direction\"><br><br>\
<label for=\"rotations\">Rotations:</label>\
<input type=\"text\" id=\"rotations\" name=\"rotations\"><br><br>\
<button onclick=\"sendCommand()\">Move Servo</button>\
</form>\
<h1>Hello from ESP8266!</h1>\
<p>Uptime: %02d:%02d:%02d</p>\
<img src=\"/test.svg\" />\
<script>\
function sendCommand() {\
var direction = document.getElementById('direction').value;\
var rotations = document.getElementById('rotations').value;\
var url = '/sendCommand?direction=' + direction + '&rotations=' +
rotations;\
var xhr = new XMLHttpRequest();\
xhr.open('GET', url);\
xhr.send();\
}\
</script>\
</body>\
</html>";

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


}

void handleSendCommand() {
int direction = server.arg("direction").toInt();
int rotations = server.arg("rotations").toInt();
angle = direction * rotations * 360;
myServo.write(angle);
delay(500);
server.send(200, "text/plain", "OK");
}

void drawGraph() {
// Add code for drawing the graph if needed
}

void handleNotFound() {
server.send(404, "text/plain", "Not Found");
}

void setup(void) {
myServo.attach(2);
myServo.write(0); // set the initial angle for the servo

Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");

// Wait for connection


while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}

server.on("/", HTTP_GET, handleRoot);


server.on("/sendCommand", HTTP_GET, handleSendCommand);
server.on("/test.svg", HTTP_GET, drawGraph);
server.on("/inline", HTTP_GET, []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}

void loop(void) {
server.handleClient();
MDNS.update();
}

You might also like