esp8266-servo
esp8266-servo
h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <StreamString.h>
#include <Servo.h>
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>";
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("");
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");
}
void loop(void) {
server.handleClient();
MDNS.update();
}