logo elektroda
logo elektroda
X
logo elektroda

[Solved] Car control with ESP8266 and L298N does not work - what to do?

kuba1234zxcvbnmo9 573 4
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 20977356
    kuba1234zxcvbnmo9
    Level 3  
    Hello, I am having a problem with my car project which contains an ESP8266 module and an L298N motor controller. This car is to be controlled by a second ESP8266 module which contains 4 buttons. I have written a program where the modules connect, but when I click the buttons, nothing happens. Please help as I only have a week left to complete this project.
  • ADVERTISEMENT
  • #2 20977380
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #3 20977493
    tos18
    Level 42  
    kuba1234zxcvbnmo9 wrote:
    Please help, as I only have one week left to complete this project.

    Based on the schematics and program codes posted by my colleague, as well as photographic documentation supplemented by a comprehensive description of the prototype's behaviour, I can firmly state that it is necessary to locate the cause of the non-functioning and then correct the problem by making changes to the prototype.
  • ADVERTISEMENT
  • #4 20977803
    kuba1234zxcvbnmo9
    Level 3  
    "I will come back in the evening and give all the details. I wanted to know first if anyone would be interested in helping to improve a program written in C++."


    Added after 10 [hours] 1 [minutes]:
    CODE FOR THE WIFI
    #include
    #include
    #include

    // WiFi network settings
    const char *ssid = "NameYourNetwork";
    const char *password = "PasswordYourNetwork";

    // Pin settings for the L298N motor controller
    const int motor1A = D1;
    const int motor1B = D2;
    const int motor2A = D3;
    const int motor2B = D4;

    WiFiServer server(80);
    WiFiClient client;

    void setup() {
    // Initialisation of motor pins
    pinMode(motor1A, OUTPUT);
    pinMode(motor1B, OUTPUT);
    pinMode(motor2A, OUTPUT);
    pinMode(motor2B, OUTPUT);


    // Connection to WiFi network
    Serial.begin(115200);
    Serial.println();
    Serial.print("Connecting to network ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");

    // Initialise server
    server.begin();
    }

    void loop() {
    // Listen for client connection
    client = server.available();
    if (client) {
    while (client.connected()) {
    // Read the first byte of data (command) sent by the remote
    if (client.available()) {
    char command = client.read();
    executeCommand(command);
    }
    }
    client.stop();
    }
    }

    // Function to execute the corresponding command
    void executeCommand(char command) {
    switch (command) {
    case 'F':
    forward();
    break;
    case 'B':
    backward();
    break;
    case 'L':
    left();
    break;
    case 'R':
    right();
    break;
    case 'S':
    stop();
    break;
    default:
    break;
    }
    }

    // Functions controlling the movement of the car
    void forward() {
    digitalWrite(motor1A, HIGH);
    digitalWrite(motor1B, LOW);
    digitalWrite(motor2A, HIGH);
    digitalWrite(motor2B, LOW);
    }

    void backward() {
    digitalWrite(motor1A, LOW);
    digitalWrite(motor1B, HIGH);
    digitalWrite(motor2A, LOW);
    digitalWrite(motor2B, HIGH);
    }

    void left() {
    digitalWrite(motor1A, LOW);
    digitalWrite(motor1B, HIGH);
    digitalWrite(motor2A, HIGH);
    digitalWrite(motor2B, LOW);
    }

    void right() {
    digitalWrite(motor1A, HIGH);
    digitalWrite(motor1B, LOW);
    digitalWrite(motor2A, LOW);
    digitalWrite(motor2B, HIGH);
    }

    void stop() {
    digitalWrite(motor1A, LOW);
    digitalWrite(motor1B, LOW);
    digitalWrite(motor2A, LOW);
    digitalWrite(motor2B, LOW);
    }
    CODE FOR PILOT
    #include
    #include

    // WiFi network settings
    const char *ssid = "NameYourNetwork";
    const char *password = "PasswordYourNetwork";

    // pin settings for buttons
    const int button1 = D1;
    const int button2 = D2;
    const int button3 = D3;
    const int button4 = D4;

    WiFiClient client;

    void setup() {
    // Initialisation of button pins
    pinMode(button1, INPUT_PULLUP);
    pinMode(button2, INPUT_PULLUP);
    pinMode(button3, INPUT_PULLUP);
    pinMode(button4, INPUT_PULLUP);


    // Connection to WiFi network
    Serial.begin(115200);
    Serial.println();
    Serial.print("Connecting to network ");
    Serial.println(ssid);
    WiFi.mode(WIFI_AP);
    WiFi.softAP(ssid, password);
    Serial.println("Hotspot started");
    }

    void loop() {
    if (client.connect("address_ip_selfie", 80)) {
    if (digitalRead(button1) == LOW) {
    client.write('F'); // Button 1 - drive forward
    } else if (digitalRead(button2) == LOW) {
    client.write('B'); // Button 2 - going backwards
    } else if (digitalRead(button3) == LOW) {
    client.write('L'); // Button 3 - turning left
    } else if (digitalRead(button4) == LOW) {
    client.write('R'); // Button 4 - turn right
    } else {
    client.write('S'); // No buttons pressed - stop
    }
    delay(100); // Delay to prevent network congestion
    client.stop();
    }
    }
    The boards establish a connection with each other, but when the buttons are clicked, the motors do not respond.
  • #5 20978756
    kuba1234zxcvbnmo9
    Level 3  
    I solved the problem myself
ADVERTISEMENT