I am setting the esp as a access point to which my device connects then I can go to a web server hosted on the esp which has the necessary controls, I am using 4 hobby gearmotors to make the car move. Right now my plan is to take the input from the web server, process it using the arduino and execute it using the L293D Motor Driver Shield. I am struggling with the code and would like some guidance.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// Define motor pins
const int leftMotorPin1 = D1; // Left motor pin 1
const int leftMotorPin2 = D2; // Left motor pin 2
const int rightMotorPin1 = D3; // Right motor pin 1
const int rightMotorPin2 = D4; // Right motor pin 2
ESP8266WebServer server(80);
void setup() {
// Initialize motor pins as outputs
pinMode(leftMotorPin1, OUTPUT);
pinMode(leftMotorPin2, OUTPUT);
pinMode(rightMotorPin1, OUTPUT);
pinMode(rightMotorPin2, OUTPUT);
connectToWiFi();
server.on("/", HTTP_GET, handleRoot);
server.on("/control", HTTP_GET, handleControl);
server.begin();
}
void loop() {
server.handleClient();
}
void connectToWiFi() {
WiFi.mode(WIFI_AP);
WiFi.softAP("RC_Car_AP", "password"); // Set SSID and password for the AP
Serial.println("Access Point Started");
Serial.println(WiFi.softAPIP()); // Print the IP address
}
void handleRoot() {
// Display the web page with control buttons
String page = "<!DOCTYPE html><html><head><title>RC Car Control</title></head><body>";
page += "<h1 style='text-align: center;'>RC Car Control</h1>";
page += "<div style='text-align: center;'>";
page += "<button style='font-size: 15px;' onmousedown=\"startMoving('forward');\" onmouseup=\"stopMoving();\" onmouseleave=\"stopMoving();\">↑ Forward</button><br>"; // Forward
page += "<button style='font-size: 15px;' onmousedown=\"startMoving('left');\" onmouseup=\"stopMoving();\" onmouseleave=\"stopMoving();\">← Left</button>";
page += "<button style='font-size: 15px;' onmousedown=\"startMoving('right');\" onmouseup=\"stopMoving();\" onmouseleave=\"stopMoving();\">→ Right</button><br>"; // Left and Right
page += "<button style='font-size: 15px;' onmousedown=\"startMoving('backward');\" onmouseup=\"stopMoving();\" onmouseleave=\"stopMoving();\">↓ Backward</button>"; // Backward
page += "</div>";
page += "<script>var intervalId;function startMoving(command) {intervalId = setInterval(function() {sendCommand(command);}, 100);}function stopMoving() {clearInterval(intervalId);sendCommand('stop');}function sendCommand(command) {var xhttp = new XMLHttpRequest();xhttp.open('GET', '/control?cmd=' + command, true);xhttp.send();}</script>";
page += "</body></html>";
server.send(200, "text/html", page);
}
void handleControl() {
// Process control commands
String command = server.arg("cmd");
if (command == "forward") {
moveForward();
} else if (command == "backward") {
moveBackward();
} else if (command == "left") {
turnLeft();
} else if (command == "right") {
turnRight();
} else if (command == "stop") {
stopMoving();
}
server.send(200, "text/plain", "OK");
}
void moveForward() {
digitalWrite(leftMotorPin1, HIGH);
digitalWrite(leftMotorPin2, LOW);
digitalWrite(rightMotorPin1, HIGH);
digitalWrite(rightMotorPin2, LOW);
}
void moveBackward() {
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, HIGH);
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, HIGH);
}
void turnLeft() {
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, HIGH);
digitalWrite(rightMotorPin1, HIGH);
digitalWrite(rightMotorPin2, LOW);
}
void turnRight() {
digitalWrite(leftMotorPin1, HIGH);
digitalWrite(leftMotorPin2, LOW);
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, HIGH);
}
void stopMoving() {
digitalWrite(leftMotorPin1, LOW);
digitalWrite(leftMotorPin2, LOW);
digitalWrite(rightMotorPin1, LOW);
digitalWrite(rightMotorPin2, LOW);
}
The web server part and turning the motors in 2 directions using the driver, I don't know if what I am doing is wrong or right, I don't know if setting the ESP as a access point will make sure I can access it. and I am not sure how to make it look normal and if I can put some JavaScript or CSS in it. (also, is this the best category for this project?)
I can show what the wiring diagram is going to look like (tomorrow as it is very late) but I can't tell you about my current sketch as I all the parts haven't arrived yet. I will look into the Google Maps API and get back to you with the sketch's functionality by 1 to 2 May.
I was just saying i don't have all the parts so I can't tell you about the functionality till they arrive. Also I am sorry but I don't understand what you mean by ultimatum
Hey there, I am making a simple WiFi car project using Arduino Uno, L293D Motor Driver Shield, 4 DC Motors and an ESP32-CAM. I have set up a web server on the esp32cam with 5 buttons, Forward, Backward, Left, Right and Stop(For redundancy). When Forward is pressed it prints 'F' to the serial monitor, 'B' for back, 'R' for right, 'L' for left, and 'S' for stop. The Arduino Uno interprets these commands and executes specific functions. I have connected RX of ESP32-CAM to TX of Arduino Uno and TX of ESP32-CAM to RX of Arduino Uno. I have provided power to both the shield and the ESP32-CAM using the ftdi programmer's vcc. I have read many threads and they were saying I need level shifters to make communication between arduino and esp possible. Also, when I send the commands MANUALLY through serial it works, but when esp sends them, it does not respond, I think it has to do something with the commands being echoed as without echoing they are not visibile.
You can feed it with either but using Vcc = 5 volt it is converted to 3.3 volt for the internal logic. Don't mix logic levels with the power fed to the circuit.
Will it damage in anyway? I am not sending any serial data to CAM from Arduino Uno, I am just receiving
Edit: The serial print functions were only used during debugging, when CAM was NOT connected. Normally they are removed. As show here - https://www.instructables.com/A-Quick-Guide-on-Logic-Level-Shifting/ I am only send from 3.3v logic to 5v logic not receiving. So, do I really need a logic level shifter?
Remove the UNO Tx -》to CAM Rx and You're safe.
Instructables has the nickname "destructables" for a good reason.
Always check with the datasheet, section "Absolute maximum ratings". Never rely on "found on Internet".
Whats the minimum output voltage that the ESP32 puts out for a logic 1 and compare it with the minimum voltage that the ATmega328P (UNO) needs to recognise a logic 1.
The connection might appear to work without a logic level shifter, but that does not mean it will always work and be reliable.