Advertisement
iqhtyar2k

WifiRcCar

Jan 27th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 3.38 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4.  
  5. const char* ssid = "WifiRcCar";
  6. const char* password = "12345678";
  7.  
  8. // Pins for L298N motor driver
  9. const int IN5 = 2;
  10. const int IN1 = 14;
  11. const int IN2 = 12;
  12. const int IN3 = 13;
  13. const int IN4 = 15;
  14.  
  15. // Motor speed
  16. int speed = 100;
  17.  
  18. ESP8266WebServer server(80);
  19.  
  20. void handleRoot() {
  21.   // Create a web page with controls for forward, backward, left, right, and speed
  22.   String html = "<html><head><title>ESP8266 RC Car</title></head><body>";
  23.   html += "<h1>ESP8266 RC Car</h1>";
  24.   html += "<form action=\"/forward\" method=\"POST\">";
  25.   html += "<input type=\"submit\" value=\"Forward\">";
  26.   html += "</form>";
  27.   html += "<form action=\"/backward\" method=\"POST\">";
  28.   html += "<input type=\"submit\" value=\"Backward\">";
  29.   html += "</form>";
  30.   html += "<form action=\"/left\" method=\"POST\">";
  31.   html += "<input type=\"submit\" value=\"Left\">";
  32.   html += "</form>";
  33.   html += "<form action=\"/right\" method=\"POST\">";
  34.   html += "<input type=\"submit\" value=\"Right\">";
  35.    html += "</form>";
  36.   html += "<form action=\"/netral\" method=\"POST\">";
  37.   html += "<input type=\"submit\" value=\"Netral\">";  
  38.   html += "</form>";
  39.   html += "<form action=\"/speed\" method=\"POST\">";
  40.   html += "<input type=\"number\" name=\"speed\" value=\"" + String(speed) + "\">";
  41.   html += "<input type=\"submit\" value=\"Set Speed\">";
  42.   html += "</form>";
  43.   html += "</body></html>";
  44.  
  45.   // Send the web page to the client
  46.   server.send(200, "text/html", html);
  47. }
  48.  
  49. void handleForward() {
  50.   // Set motor direction and speed
  51.   digitalWrite(IN1, HIGH);
  52.   digitalWrite(IN2, LOW);
  53.   analogWrite(IN5, speed);
  54. }
  55.  
  56. void handleBackward() {
  57.   // Set motor direction and speed
  58.   digitalWrite(IN1, LOW);
  59.   digitalWrite(IN2, HIGH);
  60.   analogWrite(IN5, speed);
  61. }
  62.  
  63. void handleLeft() {
  64.   // Set motor direction and speed
  65.   digitalWrite(IN3, HIGH);
  66.   digitalWrite(IN4, LOW);
  67.   digitalWrite(IN1, HIGH);
  68.   digitalWrite(IN2, LOW);
  69.   analogWrite(IN5, speed);
  70. }
  71.  
  72. void handleRight() {
  73.   // Set motor direction and speed
  74.   digitalWrite(IN3, LOW);
  75.   digitalWrite(IN4, HIGH);
  76.   digitalWrite(IN1, HIGH);
  77.   digitalWrite(IN2, LOW);
  78.   analogWrite(IN5, speed);
  79. }
  80.  
  81. void handleNetral() {
  82.   // Set motor direction and speed
  83.   digitalWrite(IN3, LOW);
  84.   digitalWrite(IN4, LOW);
  85.   digitalWrite(IN1, LOW);
  86.   digitalWrite(IN2, LOW);
  87.   analogWrite(IN5, speed);
  88. }
  89.  
  90. void handleSpeed() {
  91.   // Get the speed from the form
  92.   int newSpeed = server.arg("speed").toInt();
  93.  
  94.   // Set the speed
  95.   speed = newSpeed;
  96.  
  97.   // Update the web page
  98.   server.send(200, "text/html", "<html><body>Speed set to " + String(speed) + "</body></html>");
  99. }
  100.  
  101. void setup() {
  102.   // Serial monitor for debugging
  103.   Serial.begin(115200);
  104.   delay(100);
  105.  
  106.   // Initialize the L298N motor driver
  107.   pinMode(IN5, OUTPUT);
  108.   pinMode(IN1, OUTPUT);
  109.   pinMode(IN2, OUTPUT);
  110.   pinMode(IN3, OUTPUT);
  111.   pinMode(IN4, OUTPUT);
  112.  
  113.   // Create Access Point
  114.   WiFi.mode(WIFI_AP);
  115.   WiFi.softAP(ssid, password);
  116.   IPAddress myIP = WiFi.softAPIP();
  117.   Serial.print("Access Point IP address: ");
  118.   Serial.println(myIP);
  119.  
  120.   // Configure server routes
  121.   server.on("/", handleRoot);
  122.   server.on("/forward", handleForward);
  123.   server.on("/backward", handleBackward);
  124.   server.on("/left", handleLeft);
  125.   server.on("/right", handleRight);
  126.   server.on("/netral", handleNetral);
  127.   server.on("/speed", handleSpeed);
  128.  
  129.   server.begin();
  130. }
  131.  
  132. void loop() {
  133.   server.handleClient();                    
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement