-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotorControlSketch.ino
93 lines (81 loc) · 2.59 KB
/
motorControlSketch.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// WiFi credentials
const char* ssid = "wifi_name";
const char* password = "wifi_password";
// Motor control pins
const int IN3 = D1;
const int IN4 = D2;
const int ENB = D3;
// Create an instance of the ESP8266WebServer class on port 80
ESP8266WebServer server(80);
// Handle the root URL ("/") to display a simple web page with JavaScript for control
void handleRoot() {
// HTML page with JavaScript for controlling the ESP8266
String html = "<html><body>";
html += "<h1>ESP8266 Hiz Kontrol</h1>";
html += "<p>ileri = 'w'</p>";
html += "<script>"
"var keyDown = false;"
"document.addEventListener('keydown', function(event) {"
" if (event.key === 'w' && !keyDown) {"
" fetch('/receive?state=move');"
" keyDown = true;"
" } else if (event.key === 's' && !keyDown) {"
" fetch('/receive?state=reset');"
" keyDown = false;"
" }"
"});"
"document.addEventListener('keyup', function(event) {"
" if (event.key === 'w') {"
" fetch('/receive?state=stop');"
" keyDown = false;"
" }"
"});"
"</script>";
html += "</body></html>";
server.send(200, "text/html", html);
}
// Handle the "/receive" URL with the state parameter to control the motor
void handleReceive() {
// Get the value of the "state" parameter from the URL
String state = server.arg("state");
// Check the value of the "state" parameter and control the motor accordingly
if (state == "move") {
Serial.println("Forward");
analogWrite(ENB, 150);
} else if (state == "stop" || state == "reset") {
Serial.println("Stop");
analogWrite(ENB, 0);
}
// Redirect back to the root URL ("/")
server.sendHeader("Location", "/");
server.send(303);
}
void setup() {
// Set motor control pins as outputs
Serial.begin(9600);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENB, OUTPUT);
// Set initial motor direction and speed
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 0);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println(WiFi.localIP());
Serial.println("Connected to WiFi");
// Set up HTTP handlers for the server and start the server
server.on("/", HTTP_GET, handleRoot);
server.on("/receive", HTTP_GET, handleReceive);
server.begin();
}
void loop() {
// Handle incoming client requests
server.handleClient();
}