Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: "Wi-Fi Car"
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-01-03 12:52:05
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* wifi controlled rc car that can be controlled by */
- /* webserver */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <WiFi.h> // Include WiFi library for ESP32
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void forward();
- void backward();
- void turnLeft();
- void turnRight();
- void stopMotors();
- // Wi-Fi credentials
- const char *ssid = "Dibya";
- const char *password = "Dibya1852";
- // Pin definitions for L298N motor driver
- #define ENA 14 // ENA pin
- #define IN1 27 // IN1 pin
- #define IN2 26 // IN2 pin
- #define IN3 33 // IN3 pin
- #define IN4 25 // IN4 pin
- #define ENB 34 // ENB pin
- WiFiServer server(80);
- void setup(void)
- {
- // Motor pins setup
- pinMode(ENA, OUTPUT);
- pinMode(IN1, OUTPUT);
- pinMode(IN2, OUTPUT);
- pinMode(IN3, OUTPUT);
- pinMode(IN4, OUTPUT);
- pinMode(ENB, OUTPUT);
- // Start serial communication
- Serial.begin(115200);
- // Connect to Wi-Fi
- Serial.println("Connecting to Wi-Fi...");
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("\nWi-Fi connected!");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- // Start the server
- server.begin();
- }
- void loop(void)
- {
- WiFiClient client = server.available();
- if (client) {
- Serial.println("New client connected!");
- String request = client.readStringUntil('\r');
- Serial.println(request);
- client.flush();
- // Motor control logic based on received request
- if (request.indexOf("/FORWARD") != -1) {
- forward();
- } else if (request.indexOf("/BACKWARD") != -1) {
- backward();
- } else if (request.indexOf("/LEFT") != -1) {
- turnLeft();
- } else if (request.indexOf("/RIGHT") != -1) {
- turnRight();
- } else if (request.indexOf("/STOP") != -1) {
- stopMotors();
- }
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println();
- client.println("<!DOCTYPE HTML>");
- client.println("<html><h1>ESP32 Car Control</h1></html>");
- delay(1);
- Serial.println("Client disconnected.");
- }
- }
- void forward() {
- digitalWrite(IN1, HIGH);
- digitalWrite(IN2, LOW);
- digitalWrite(IN3, HIGH);
- digitalWrite(IN4, LOW);
- ledcWrite(ENA, 255); // Use ledcWrite for PWM on ESP32
- ledcWrite(ENB, 255); // Use ledcWrite for PWM on ESP32
- Serial.println("Moving forward");
- }
- void backward() {
- digitalWrite(IN1, LOW);
- digitalWrite(IN2, HIGH);
- digitalWrite(IN3, LOW);
- digitalWrite(IN4, HIGH);
- ledcWrite(ENA, 255); // Use ledcWrite for PWM on ESP32
- ledcWrite(ENB, 255); // Use ledcWrite for PWM on ESP32
- Serial.println("Moving backward");
- }
- void turnLeft() {
- digitalWrite(IN1, LOW);
- digitalWrite(IN2, HIGH);
- digitalWrite(IN3, HIGH);
- digitalWrite(IN4, LOW);
- ledcWrite(ENA, 150); // Use ledcWrite for PWM on ESP32
- ledcWrite(ENB, 150); // Use ledcWrite for PWM on ESP32
- Serial.println("Turning left");
- }
- void turnRight() {
- digitalWrite(IN1, HIGH);
- digitalWrite(IN2, LOW);
- digitalWrite(IN3, LOW);
- digitalWrite(IN4, HIGH);
- ledcWrite(ENA, 150); // Use ledcWrite for PWM on ESP32
- ledcWrite(ENB, 150); // Use ledcWrite for PWM on ESP32
- Serial.println("Turning right");
- }
- void stopMotors() {
- digitalWrite(IN1, LOW);
- digitalWrite(IN2, LOW);
- digitalWrite(IN3, LOW);
- digitalWrite(IN4, LOW);
- ledcWrite(ENA, 0); // Use ledcWrite for PWM on ESP32
- ledcWrite(ENB, 0); // Use ledcWrite for PWM on ESP32
- Serial.println("Stopping motors");
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement