Advertisement
pleasedontcode

"Wi-Fi Car" rev_01

Jan 3rd, 2025
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Wi-Fi Car"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-01-03 12:52:05
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* wifi controlled rc car that can be controlled by */
  21.     /* webserver */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <WiFi.h> // Include WiFi library for ESP32
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void forward();
  33. void backward();
  34. void turnLeft();
  35. void turnRight();
  36. void stopMotors();
  37.  
  38. // Wi-Fi credentials
  39. const char *ssid = "Dibya";
  40. const char *password = "Dibya1852";
  41.  
  42. // Pin definitions for L298N motor driver
  43. #define ENA 14 // ENA pin
  44. #define IN1 27 // IN1 pin
  45. #define IN2 26 // IN2 pin
  46. #define IN3 33 // IN3 pin
  47. #define IN4 25 // IN4 pin
  48. #define ENB 34 // ENB pin
  49.  
  50. WiFiServer server(80);
  51.  
  52. void setup(void)
  53. {
  54.     // Motor pins setup
  55.     pinMode(ENA, OUTPUT);
  56.     pinMode(IN1, OUTPUT);
  57.     pinMode(IN2, OUTPUT);
  58.     pinMode(IN3, OUTPUT);
  59.     pinMode(IN4, OUTPUT);
  60.     pinMode(ENB, OUTPUT);
  61.  
  62.     // Start serial communication
  63.     Serial.begin(115200);
  64.  
  65.     // Connect to Wi-Fi
  66.     Serial.println("Connecting to Wi-Fi...");
  67.     WiFi.begin(ssid, password);
  68.  
  69.     while (WiFi.status() != WL_CONNECTED) {
  70.         delay(500);
  71.         Serial.print(".");
  72.     }
  73.  
  74.     Serial.println("\nWi-Fi connected!");
  75.     Serial.print("IP address: ");
  76.     Serial.println(WiFi.localIP());
  77.  
  78.     // Start the server
  79.     server.begin();
  80. }
  81.  
  82. void loop(void)
  83. {
  84.     WiFiClient client = server.available();
  85.     if (client) {
  86.         Serial.println("New client connected!");
  87.         String request = client.readStringUntil('\r');
  88.         Serial.println(request);
  89.         client.flush();
  90.  
  91.         // Motor control logic based on received request
  92.         if (request.indexOf("/FORWARD") != -1) {
  93.             forward();
  94.         } else if (request.indexOf("/BACKWARD") != -1) {
  95.             backward();
  96.         } else if (request.indexOf("/LEFT") != -1) {
  97.             turnLeft();
  98.         } else if (request.indexOf("/RIGHT") != -1) {
  99.             turnRight();
  100.         } else if (request.indexOf("/STOP") != -1) {
  101.             stopMotors();
  102.         }
  103.  
  104.         client.println("HTTP/1.1 200 OK");
  105.         client.println("Content-Type: text/html");
  106.         client.println();
  107.         client.println("<!DOCTYPE HTML>");
  108.         client.println("<html><h1>ESP32 Car Control</h1></html>");
  109.         delay(1);
  110.         Serial.println("Client disconnected.");
  111.     }
  112. }
  113.  
  114. void forward() {
  115.     digitalWrite(IN1, HIGH);
  116.     digitalWrite(IN2, LOW);
  117.     digitalWrite(IN3, HIGH);
  118.     digitalWrite(IN4, LOW);
  119.     ledcWrite(ENA, 255); // Use ledcWrite for PWM on ESP32
  120.     ledcWrite(ENB, 255); // Use ledcWrite for PWM on ESP32
  121.     Serial.println("Moving forward");
  122. }
  123.  
  124. void backward() {
  125.     digitalWrite(IN1, LOW);
  126.     digitalWrite(IN2, HIGH);
  127.     digitalWrite(IN3, LOW);
  128.     digitalWrite(IN4, HIGH);
  129.     ledcWrite(ENA, 255); // Use ledcWrite for PWM on ESP32
  130.     ledcWrite(ENB, 255); // Use ledcWrite for PWM on ESP32
  131.     Serial.println("Moving backward");
  132. }
  133.  
  134. void turnLeft() {
  135.     digitalWrite(IN1, LOW);
  136.     digitalWrite(IN2, HIGH);
  137.     digitalWrite(IN3, HIGH);
  138.     digitalWrite(IN4, LOW);
  139.     ledcWrite(ENA, 150); // Use ledcWrite for PWM on ESP32
  140.     ledcWrite(ENB, 150); // Use ledcWrite for PWM on ESP32
  141.     Serial.println("Turning left");
  142. }
  143.  
  144. void turnRight() {
  145.     digitalWrite(IN1, HIGH);
  146.     digitalWrite(IN2, LOW);
  147.     digitalWrite(IN3, LOW);
  148.     digitalWrite(IN4, HIGH);
  149.     ledcWrite(ENA, 150); // Use ledcWrite for PWM on ESP32
  150.     ledcWrite(ENB, 150); // Use ledcWrite for PWM on ESP32
  151.     Serial.println("Turning right");
  152. }
  153.  
  154. void stopMotors() {
  155.     digitalWrite(IN1, LOW);
  156.     digitalWrite(IN2, LOW);
  157.     digitalWrite(IN3, LOW);
  158.     digitalWrite(IN4, LOW);
  159.     ledcWrite(ENA, 0); // Use ledcWrite for PWM on ESP32
  160.     ledcWrite(ENB, 0); // Use ledcWrite for PWM on ESP32
  161.     Serial.println("Stopping motors");
  162. }
  163.  
  164. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement