Arduinio and esp 01 wifi car

Hi, I need help with my project, I'm trying to build a RC car with Wi-Fi using ESP 01 and Arduino Mega 2560. I'm programming the ESP 01 with another Arduino. So far I've managed to create an access point for the ESP 01, which looks like this:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

const char* ssid = "RC_Auticko";
const char* password = "12345678";

ESP8266WebServer server(80);

void handleRoot() {
  String page = "<html><body><h1>RC Auticko</h1>";
  page += "<a href=\"/forward\"><button>Vpred</button></a><br>";
  page += "<a href=\"/backward\"><button>Zpet</button></a><br>";
  page += "<a href=\"/left\"><button>Leva</button></a><br>";
  page += "<a href=\"/right\"><button>Prava</button></a><br>";
  page += "<a href=\"/stop\"><button>Stop</button></a>";
  page += "</body></html>";
  server.send(200, "text/html", page);
}

void setup() {
  Serial.begin(9600); // Spojení s Arduinem
  WiFi.softAP(ssid, password);
  server.on("/", handleRoot);
  server.on("/forward", []() {
    server.send(200, "text/html", "<html><body><h1>Vpred</h1><a href=\"/\">Zpet</a></body></html>");
    Serial.println("FORWARD");
  });
  server.on("/backward", []() {
    server.send(200, "text/html", "<html><body><h1>Zpet</h1><a href=\"/\">Zpet</a></body></html>");
    Serial.println("BACKWARD");
  });
  server.on("/left", []() {
    server.send(200, "text/html", "<html><body><h1>Leva</h1><a href=\"/\">Zpet</a></body></html>");
    Serial.println("LEFT");
  });
  server.on("/right", []() {
    server.send(200, "text/html", "<html><body><h1>Prava</h1><a href=\"/\">Zpet</a></body></html>");
    Serial.println("RIGHT");
  });
  server.on("/stop", []() {
    server.send(200, "text/html", "<html><body><h1>Stop</h1><a href=\"/\">Zpet</a></body></html>");
    Serial.println("STOP");
  });

  server.begin();
}

void loop() {
  server.handleClient();
}

And code for arduino wich looks like this:

#include <SD.h>
#include <TMRpcm.h>
#include <SPI.h>

// SD karta
#define SD_ChipSelectPin 53
TMRpcm tmrpcm;

// Motor A
const int motorPin1 = 5;  // L293D - pin 14
const int motorPin2 = 6;  // L293D - pin 10
// Motor B
const int motorPin3 = 10; // L293D - pin 7
const int motorPin4 = 9;  // L293D - pin 2

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);

  Serial.begin(9600);   // Debug přes USB
  Serial1.begin(9600);  // Komunikace s ESP-01

  if (!SD.begin(SD_ChipSelectPin)) {
    Serial.println("SD karta nenalezena nebo chyba inicializace.");
    return;
  }

  tmrpcm.speakerPin = 11;
  tmrpcm.setVolume(5);
  tmrpcm.play("5.wav");
}

void loop() {
  if (!tmrpcm.isPlaying()) {
    tmrpcm.play("5.wav");
  }

  if (Serial1.available()) {
    String command = Serial1.readStringUntil('\n');
    command.trim();
    Serial.println("Přijatý příkaz: " + command);

    if (command == "FORWARD") {
      forward();
    } else if (command == "BACKWARD") {
      backward();
    } else if (command == "LEFT") {
      left();
    } else if (command == "RIGHT") {
      right();
    } else if (command == "STOP") {
      stopMotors();
    }
  }
}

void forward() {
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
}

void backward() {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
}

void left() {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
}

void right() {
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
}

void stopMotors() {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
}

Which works pretty well, but when I press some of these buttons, it's a little slow and the motors just shake a little. The reproductor part works, but the button does not work properly. Thanks for any advicies.

It dosent? I mean i tested it and then make some small changes but still it should make ap

Sorry, I deleted my post when I realised my mistake.

Ahhh allright sorry

I guess my feedback would be to avoid using, and having to program, 2x MCUs in the same project. The added complexity, of having the two codes communicate successfully and smoothly, makes the project less likely to succeed.

Instead, use something like an ESP32, and if you need more I/o pins, use I/o extender chips.

1 Like

That would be great solutions if i have esp 32 but i need to do this projects with esp 01 and arduino

Who is forcing this difficult restriction on you?

My wallet and me😅 i want to use things wha i already own

ESP32 are cheap. You are inflicting difficulties on yourself and then asking for help from others. This may make many forum members less willing to help you.

It's like posting on a cake baking forum asking for help making a birthday cake using only bananas and sausages because that is what you have. Use the bananas and sausages in other recipes, buy flour, sugar etc for your cake.

Yeah but still i need to buy, i dont undestrand why j cant use this metod its only something with the buttons

And maybe someone know how to fix it.

You're using the ESP01 instead of a HC05/06. I think that's OK.
Perhaps if your transmit strings were shorter, single letter (F, L, R, B, S).

1 Like

Yeah thanks for repond i allready solve it somehow the problem was that if i click forward for example it just send one signal

So the messages get through from the ESP-01 to the Mega OK ?

void forward() {
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
}

If this function is executed, does the car actually move forward or not.

Can you post a schematic. How have you powered the whole thing.

1 Like

Hi thnaks for your time and repsond but i solved it already

How?

Well in my previous code was error because if i click on the button only one signal was sent so i asked chat gpt and he correct it and now if i hold that button it send signals all the time

Nice.
Would you show the code that make it work?

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

const char* ssid = "Honza Jochman";
const char* password = "FernandoAlonso2006";

ESP8266WebServer server(80);

// HTML stránka s ovládacími tlačítky
const char* htmlPage = R"rawliteral(
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>RC Autíčko</title>
  <style>
    body {
      text-align: center;
      font-family: sans-serif;
      user-select: none;
      -webkit-user-select: none;
      margin-top: 50px;
    }
    .button {
      width: 100px;
      height: 100px;
      font-size: 36px;
      margin: 10px;
      touch-action: none;
      user-select: none;
    }
  </style>
</head>
<body>
  <h2>RC Autíčko</h2>
  <div>
    <button class="button" ontouchstart="send('FORWARD')" ontouchend="stop()">⬆️</button>
  </div>
  <div>
    <button class="button" ontouchstart="send('LEFT')" ontouchend="stop()">⬅️</button>
    <button class="button" ontouchstart="send('TOGGLE_MUSIC')" ontouchend="stopSending()">🎵</button>
    <button class="button" ontouchstart="send('RIGHT')" ontouchend="stop()">➡️</button>
  </div>
  <div>
    <button class="button" ontouchstart="send('BACKWARD')" ontouchend="stop()">⬇️</button>
  </div>

  <script>
    let interval;

    function send(cmd) {
      clearInterval(interval);
      fetch("/cmd?dir=" + cmd);
      interval = setInterval(() => {
        fetch("/cmd?dir=" + cmd);
      }, 200);
    }

    // Původní stop pro motory
    function stop() {
      clearInterval(interval);
      fetch("/cmd?dir=STOP");
    }

    // Nová funkce pro zastavení odesílání hudebních příkazů bez STOP
    function stopSending() {
      clearInterval(interval);
    }
  </script>
</body>
</html>
)rawliteral";

void setup() {
  Serial.begin(9600); // Spojení s Arduinem
  WiFi.softAP(ssid, password);
  Serial.println("WiFi AP na: " + WiFi.softAPIP().toString());

  server.on("/", []() {
    server.send(200, "text/html; charset=utf-8", htmlPage);
  });

  server.on("/cmd", []() {
    if (server.hasArg("dir")) {
      String dir = server.arg("dir");
      dir.trim();
      Serial.println(dir);  // posílá příkaz na Arduino přes sériovou linku
      server.send(200, "text/plain", "OK");
    } else {
      server.send(400, "text/plain", "Missing parameter");
    }
  });

  server.begin();
  Serial.println("Webový server spuštěn");
}

void loop() {
  server.handleClient();
}

Nice.

1 Like