-
Notifications
You must be signed in to change notification settings - Fork 0
/
Esp01AsyncRc.ino
152 lines (140 loc) · 3.85 KB
/
Esp01AsyncRc.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
const char* ssid = "iqhtyar";
const char* password = "12345678";
IPAddress ip(192, 168, 43, 129);
IPAddress netmask(255, 255. 255, 0);
const int port = 5038; // and this port 9876
const int motorPin1 = 0;
const int motorPin2 = 3;
const int motorPin3 = 2;
const int motorPin4 = 1;
AsyncWebServer server(80);
// Kode HTML untuk halaman web kendali motor
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Esp RcBotCar</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
.button {
display: inline-block;
margin: 13px;
padding: 10px 20px;
font-size: 15px;
cursor: pointer;
border-radius: 5px;
background-color: #3498db;
color: white;
text-decoration: none;
}
.button:hover {
background-color: #2980b9;
}
</style>
</head>
<body>
<h1>Remote Control</h1>
<div>
<button class="button" onclick="controlMotor('forward')">MAJU</button>
</div>
<div>
<button class="button" onclick="controlMotor('left')">KIRI</button>
<button class="button" onclick="controlMotor('stop')">STOP</button>
<button class="button" onclick="controlMotor('right')">KANAN</button>
</div>
<div>
<button class="button" onclick="controlMotor('reverse')">MUNDUR</button>
</div>
<script>
function controlMotor(command) {
fetch('/motor', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `command=${command}`
})
.then(response => {
console.log(response);
})
.catch(error => {
console.error('Error:', error);
});
}
</script>
</body>
</html>
)rawliteral";
void setup() {
Serial.begin(57600);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
// Mencoba terhubung ke WiFi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if(WiFi.status() == WL_CONNECT_FAILED) {
Serial.println("Connection Failed!");
break;
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html);
});
server.on("/motor", HTTP_POST, [](AsyncWebServerRequest *request){
String command = request->arg("command");
if (command.equals("forward")) {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(10);
} else if (command.equals("reverse")) {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(10);
} else if (command.equals("left")) {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(10);
} else if (command.equals("right")) {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(10);
} else if (command.equals("stop")) {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(10);
}
request->send(200, "text/plain", "Motor diatur: " + command);
});
server.begin();
}
void loop() {
}