Default AP SSID: RobotCarAP
Password = 12345678
Browser URL: [ Ссылка ]
Program Code
=============
#include <ESP8266WiFi.h>
const char* ssid = "RobotCarAP"; // Name of the hotspot
const char* password = "12345678"; // Password for the hotspot//
const int motor1A = D1; // Motor 1 forward
const int motor1B = D2; // Motor 1 backward
const int motor2A = D3; // Motor 2 forward
const int motor2B = D4; // Motor 2 backward
WiFiServer server(80);
void setup() {
Serial.begin(9600);
WiFi.softAP(ssid, password);
Serial.println("Access Point Started");
Serial.print("Access Point Name: ");
Serial.println(ssid);
Serial.print("Access Point Password: ");
Serial.println(password);
Serial.print("Access Point IP Address: ");
Serial.println(WiFi.softAPIP()); // Print the IP address of the hotspot
server.begin();
Serial.println("Web Server started");
Serial.print("Use this URL to connect: http://");
Serial.println(WiFi.softAPIP());
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
}
void loop() {
WiFiClient client = server.available();
if (client) {
String request = client.readStringUntil('\r');
client.flush();
if (request.indexOf("/forward") != -1) {
forward();
} else if (request.indexOf("/backward") != -1) {
backward();
} else if (request.indexOf("/left") != -1) {
left();
} else if (request.indexOf("/right") != -1) {
right();
} else if (request.indexOf("/stop") != -1) {
stop();
}
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.println("<!DOCTYPE html>");
client.println("<html lang=\"en\">");
client.println("<head>");
client.println("<meta charset=\"UTF-8\">");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
client.println("<title>Robot Car Control</title>");
client.println("<style>");
client.println("body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; }");
client.println(".joystick { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr 1fr; gap: 10px; width: 250px; height: 300px; background-color: #ffffff; border-radius: 20px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.2); padding: 20px; }");
client.println(".button { background-color: #007BFF; border: none; border-radius: 10px; color: white; font-size: 20px; cursor: pointer; transition: background-color 0.3s; height: 80px; }");
client.println(".button:hover { background-color: #0056b3; }");
client.println(".button:active { background-color: #004085; }");
client.println(".stop { grid-column: 1 / span 2; background-color: #dc3545; }");
client.println(".stop:hover { background-color: #c82333; }");
client.println(".stop:active { background-color: #bd2130; }");
client.println("</style>");
client.println("</head>");
client.println("<body>");
client.println("<div class=\"joystick\">");
client.println("<button class=\"button\" style=\"grid-column: 1 / span 2;\" onclick=\"sendCommand('/forward')\">Forward</button>");
client.println("<button class=\"button\" onclick=\"sendCommand('/left')\">Left</button>");
client.println("<button class=\"button\" onclick=\"sendCommand('/right')\">Right</button>");
client.println("<button class=\"button\" style=\"grid-column: 1 / span 2;\" onclick=\"sendCommand('/backward')\">Backward</button>");
client.println("<button class=\"button stop\" style=\"grid-column: 1 / span 2;\" onclick=\"sendCommand('/stop')\">Stop</button>");
client.println("</div>");
client.println("<script>");
client.println("function sendCommand(command) { fetch(command).then(response => response.text()).then(data => console.log(data)).catch(error => console.error('Error:', error)); }");
client.println("</script>");
client.println("</body>");
client.println("</html>");
client.stop();
}
}
void forward() {
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
Serial.println("Moving Forward");
}
void backward() {
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
Serial.println("Moving Backward");
}
void left() {
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
Serial.println("Turning Left");
}
void right() {
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
Serial.println("Turning Right");
}
void stop() {
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, LOW);
Serial.println("Stopped");
}
=======
#NodeMCU #ES8266 #WiFiRCcar #DIYProjects #Electronics #IoT #Hobbyist #TechTutorials
Ещё видео!