This tutorial instructs you how to use the ESP8266 to wirelessly control a robot car from a Web browser on your smartphone or PC using WiFi. The control is facilitated through a graphical web user interface using something called WebSocket, allowing for smooth and dynamic control of the car.
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. Additionally, some of these links are for products from our own brand, DIYables .
Overview of 2WD RC Car and WebSocket
Now, why go for WebSocket? Here's the scoop:
Without WebSocket, changing the car's direction would require reloading the page every time. Not ideal!
However, with WebSocket, we establish a special connection between the webpage and the ESP8266. This enables sending commands to the ESP8266 in the background, without needing to reload the page. The result? The robot car moves seamlessly and in real-time. Pretty cool, right?
In a nutshell, the WebSocket connection enables the smooth, real-time control of the robot.
We have specific tutorials about 2WD RC Car and WebSocket. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP8266, ESP8266 code... Learn more about them at the following links:
The ESP8266 code creates both a web server and a WebSocket Server. Here's how it works:
When you enter the ESP8266's IP address in a web browser, it requests the webpage (User Interface) from the ESP8266.
The ESP8266's web server responds by sending the webpage's content (HTML, CSS, JavaScript).
Your web browser then displays the webpage.
The JavaScript code within the webpage establishes a WebSocket connection to the WebSocket server on the ESP8266.
Once this WebSocket connection is established, if you press/release the buttons on the webpage, the JavaScript code quietly sends the commands to the ESP8266 through this WebSocket connection in the background.
The WebSocket server on the ESP8266, upon receiving the commands, controls the robot car accordingly.
The below table show commands list that the webpage sends to ESP8266 based on the user's actions:
User's Action
Button
Command
Car Action
PRESS
UP
1
MOVE FORWARD
PRESS
DOWN
2
MOVE BACKWARD
PRESS
LEFT
4
TURN LEFT
PRESS
RIGHT
8
TURN RIGHT
PRESS
STOP
0
STOP
RELEASE
UP
0
STOP
RELEASE
DOWN
0
STOP
RELEASE
LEFT
0
STOP
RELEASE
RIGHT
0
STOP
RELEASE
STOP
0
STOP
Wiring Diagram between 2WD RC Car and ESP8266
This image is created using Fritzing. Click to enlarge image
Another for the ESP8266 board, L298N module (Motor driver).
But, you can simplify it using just one power source for everything – four 1.5V batteries (totaling 6V). Here's how:
Connect the batteries to the L298N module as shown.
Place two jumpers from ENA and ENB pins to 5 volts on the L298N module.
Remove a jumper labeled 5VEN (yellow circle on the diagram).
Do the remaining wiring as above diagram.
Since the 2WD RC car has an on/off switch, you can optionally connect the battery via the switch to enable turning on/off power for the car. If you want to make it simple, just ignore the switch.
ESP8266 Code
The webpage's content (HTML, CSS, JavaScript) are stored separately on an index.h file. So, we will have two code files on Arduino IDE:
An .ino file that is ESP8266 code, which creates a web sever and WebSocket Server, and controls car
An .h file, which contains the webpage's content.
Detailed Instructions
To get started with ESP8266 on Arduino IDE, follow these steps:
Now you have the code in two files: newbiely.com.ino and index.h
Click Upload button on Arduino IDE to upload code to ESP8266
Open the Serial Monitor
Check out the result on Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
Connecting to WiFi...
Connected to WiFi
ESP8266 Web Server's IP address IP address: 192.168.0.5
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2
Take note of the IP address displayed, and enter this address into the address bar of a web browser on your smartphone or PC.
You will see the webpage it as below:
The JavaScript code of the webpage automatically creates the WebSocket connection to ESP8266.
Now you can control the car to turn left/right, move forward/backward via the web interface.
To save the memory of ESP8266, the images of the control buttons are NOT stored on ESP8266. Instead, they are stored on the internet, so, your phone or PC need to have internet connection to load images for the web control page.
※ NOTE THAT:
If you modify the HTML content in the index.h and does not touch anything in newbiely.com.ino file, when you compile and upload code to ESP8266, Arduino IDE will not update the HTML content.
To make Arduino IDE update the HTML content in this case, make a change in the newbiely.com.ino file (e.g. adding empty line, add a comment....)
Line-by-line Code Explanation
The above ESP8266 code contains line-by-line explanation. Please read the comments in the code!
Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!