Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- const char* ssid = "WifiRcCar";
- const char* password = "12345678";
- // Pins for L298N motor driver
- const int IN5 = 2;
- const int IN1 = 14;
- const int IN2 = 12;
- const int IN3 = 13;
- const int IN4 = 15;
- // Motor speed
- int speed = 100;
- ESP8266WebServer server(80);
- void handleRoot() {
- // Create a web page with controls for forward, backward, left, right, and speed
- String html = "<html><head><title>ESP8266 RC Car</title></head><body>";
- html += "<h1>ESP8266 RC Car</h1>";
- html += "<form action=\"/forward\" method=\"POST\">";
- html += "<input type=\"submit\" value=\"Forward\">";
- html += "</form>";
- html += "<form action=\"/backward\" method=\"POST\">";
- html += "<input type=\"submit\" value=\"Backward\">";
- html += "</form>";
- html += "<form action=\"/left\" method=\"POST\">";
- html += "<input type=\"submit\" value=\"Left\">";
- html += "</form>";
- html += "<form action=\"/right\" method=\"POST\">";
- html += "<input type=\"submit\" value=\"Right\">";
- html += "</form>";
- html += "<form action=\"/netral\" method=\"POST\">";
- html += "<input type=\"submit\" value=\"Netral\">";
- html += "</form>";
- html += "<form action=\"/speed\" method=\"POST\">";
- html += "<input type=\"number\" name=\"speed\" value=\"" + String(speed) + "\">";
- html += "<input type=\"submit\" value=\"Set Speed\">";
- html += "</form>";
- html += "</body></html>";
- // Send the web page to the client
- server.send(200, "text/html", html);
- }
- void handleForward() {
- // Set motor direction and speed
- digitalWrite(IN1, HIGH);
- digitalWrite(IN2, LOW);
- analogWrite(IN5, speed);
- }
- void handleBackward() {
- // Set motor direction and speed
- digitalWrite(IN1, LOW);
- digitalWrite(IN2, HIGH);
- analogWrite(IN5, speed);
- }
- void handleLeft() {
- // Set motor direction and speed
- digitalWrite(IN3, HIGH);
- digitalWrite(IN4, LOW);
- digitalWrite(IN1, HIGH);
- digitalWrite(IN2, LOW);
- analogWrite(IN5, speed);
- }
- void handleRight() {
- // Set motor direction and speed
- digitalWrite(IN3, LOW);
- digitalWrite(IN4, HIGH);
- digitalWrite(IN1, HIGH);
- digitalWrite(IN2, LOW);
- analogWrite(IN5, speed);
- }
- void handleNetral() {
- // Set motor direction and speed
- digitalWrite(IN3, LOW);
- digitalWrite(IN4, LOW);
- digitalWrite(IN1, LOW);
- digitalWrite(IN2, LOW);
- analogWrite(IN5, speed);
- }
- void handleSpeed() {
- // Get the speed from the form
- int newSpeed = server.arg("speed").toInt();
- // Set the speed
- speed = newSpeed;
- // Update the web page
- server.send(200, "text/html", "<html><body>Speed set to " + String(speed) + "</body></html>");
- }
- void setup() {
- // Serial monitor for debugging
- Serial.begin(115200);
- delay(100);
- // Initialize the L298N motor driver
- pinMode(IN5, OUTPUT);
- pinMode(IN1, OUTPUT);
- pinMode(IN2, OUTPUT);
- pinMode(IN3, OUTPUT);
- pinMode(IN4, OUTPUT);
- // Create Access Point
- WiFi.mode(WIFI_AP);
- WiFi.softAP(ssid, password);
- IPAddress myIP = WiFi.softAPIP();
- Serial.print("Access Point IP address: ");
- Serial.println(myIP);
- // Configure server routes
- server.on("/", handleRoot);
- server.on("/forward", handleForward);
- server.on("/backward", handleBackward);
- server.on("/left", handleLeft);
- server.on("/right", handleRight);
- server.on("/netral", handleNetral);
- server.on("/speed", handleSpeed);
- server.begin();
- }
- void loop() {
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement