ESP32 Wifi Car Featured Image

Posted on

by

in

Create your own ESP32 Wifi Car using the library esp32-wifi-car

Introduction

Would you like to create your own ESP32 Wifi car using minimal code? Please follow along with this post as I am sharing with you the library that I have created to help you create your own ESP32 Wifi car.

I created an Arduino library from my earlier post about ESP32 Robot Car Using Websockets. I am sharing this library for you to try and download. All you need is to wire the schematic and add minimal code to your entry code and you will now have your own ESP32 Wifi car.

If you want to see this project in action and you want a video tutorial then please see this video in my youtube channel.

Code

The ESP32 Wifi Car Library can be found in my GitHub repository which you can access from here.

You can take a look at the examples folder on how to use this library. I will keep improving the project and documentation as soon as I can.

Github Repository project link

Prerequisite

  • ESP32 (I used ESP32s)
  • LM298n motor driver module
  • Generic Robot Car
  • breadboard/wiring
  • battery pack

I used Visual Studio Code with the PlatformIO extension installed in developing this project. If you are in Windows and have not yet installed Visual Studio Code then you can check my post about how to Install Visual Studio Code or VSCode in Windows. If you are not familiar with PlatformIO then please read this PlatformIO Tutorial for Arduino Development where I detailed how to get started with development using the PlatformIO IDE extension

Wiring/Schematic

ESP32 Wifi Car Library-Wiring

For your reference:

ESP32LM298
GPIO21ENA
GPIO22ENB
GPIO16IN1
GPIO17IN2
GPIO32IN3
GPIO33IN4
LM298GEAR MOTOR
MOTOR A OUTPUT 1OUTPUT PIN 1
MOTOR A OUTPUT 2OUTPUT PIN 2
MOTOR B OUTPUT 1OUTPUT PIN 1
MOTOR B OUTPUT 1OUTPUT PIN 2
LM298BATTERY PACKESP32
GNDGNDGND
VCCPOSITIVE (Pin)Vin

Note: Make sure that the battery pack is not connected while we are programming the ESP32 with your workstation and that the USB cable is connected to it. This is to avoid our ESP32 MCU from encountering power issues. Once we are done programming and uploading our program then we can connect your battery pack.

ESP32 Wifi Car Library - Sample

How to use the ESP32 Wifi Car Library?

Using PlatformIO

  1. Create a new project in PlatformIO IDE and select your ESP32 board from the board drop-down. Select Arduino as the Framework. Set the name of your project and the location where you want to save the project. Wait for the initialization to finish.
ESP32 Wifi Car Library - PlatformIO Create project
  1. Open the platform.ini file and write down the following additional entries. This will set the Serial Monitor Baud Rate and the needed library dependencies.
; Serial Monitor options
monitor_speed = 115200

lib_deps=
    https://github.com/donskytech/esp32-wifi-car.git
    FS
    Wifi
    SPIFFS

Your platform.ini should now look like the following. These are the libraries needed to make this project run into

ESP32 Wifi Car Library - PlatformIO platform.ini
  1. Build the project and check if there are no errors by clicking the PlatformIO icon and clicking the Build task. This should download all the dependencies automatically.
ESP32 Wifi Car Library - PlatformIO Build
  1. Go to the explorer and expand the “.pio\lib_deps\ESP32 Wifi Car”. Copy the data folder and paste it into the root of your project.
ESP32 Wifi Car Library - PlatformIO Copy data folder

Your explorer should now be like this.

ESP32 Wifi Car Library - PlatformIO Copy data folder into root
  1. Upload the FileSystem image into your ESP32 Development board. First, connect your ESP32 MCU to your laptop or workstation. Next, Click the Upload FileSystem Image in the PlatformIO task.
ESP32 Wifi Car Library - PlatformIO Upload FileSystem Image

You should see the following console messages displayed.

Note: If you see the console displaying “Connecting……___” message then click the boot button on the ESP32 momentarily.

  1. Open the main.cpp file and copy the example program from the example file here.

For your reference, the code looks like this.

#include <Arduino.h>
#include "esp32_wifi_car.h"

// Change this to your network SSID
const char *ssid = "<CHANGE_THIS_TO_YOUR_SSID>";
const char *password = "<CHANGE_THIS_TO_YOUR_SSID_PASSWORD>";

WifiCar wifiCar;

// Callback function that receives messages from websocket client
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
               void *arg, uint8_t *data, size_t len)
{
  switch (type)
  {
  case WS_EVT_DATA:
  {
    // data packet
    AwsFrameInfo *info = (AwsFrameInfo *)arg;
    if (info->final && info->index == 0 && info->len == len)
    {
      // the whole message is in a single frame and we got all of it's data
      if (info->opcode == WS_TEXT)
      {
        data[len] = 0;
        char *command = (char *)data;
        wifiCar.sendCarCommand(command);
      }
    }
    break;
  }
  default:
    // Serial.println("Received other socket event!");
    break;
  }
}

// Setup function
void setup()
{
  // Initialize the serial monitor baud rate
  Serial.begin(115200);
  Serial.println("Connecting to ");
  Serial.println(ssid);

  // Connect to your wifi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED)
  {
    Serial.printf("WiFi Failed!\n");
    return;
  }

  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // Initialize SPIFFS
  if (!SPIFFS.begin(true))
  {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }

  wifiCar.start(onWsEvent);

}

void loop()
{
  // No code in here.  Server is running in asynchronous mode
}

That is the whole code for this project. Let us run through the code.

#include <Arduino.h>
#include "esp32_wifi_car.h"

Import the Arduino and esp32_wifi_car header.

// Change this to your network SSID
const char *ssid = "<CHANGE_THIS_TO_YOUR_SSID>";
const char *password = "<CHANGE_THIS_TO_YOUR_SSID_PASSWORD>";

Change the Wifi SSID and password according to your network.

WifiCar wifiCar;

// Callback function that receives messages from websocket client
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
               void *arg, uint8_t *data, size_t len)
{
  switch (type)
  {
  case WS_EVT_DATA:
  {
    // data packet
    AwsFrameInfo *info = (AwsFrameInfo *)arg;
    if (info->final && info->index == 0 && info->len == len)
    {
      // the whole message is in a single frame and we got all of it's data
      if (info->opcode == WS_TEXT)
      {
        data[len] = 0;
        char *command = (char *)data;
        wifiCar.sendCarCommand(command);
      }
    }
    break;
  }
  default:
    // Serial.println("Received other socket event!");
    break;
  }
}

Declare our WifiCar and the onWsEvent callback function needed to send commands to our car.

// Setup function
void setup()
{
  // Initialize the serial monitor baud rate
  Serial.begin(115200);
  Serial.println("Connecting to ");
  Serial.println(ssid);

  // Connect to your wifi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED)
  {
    Serial.printf("WiFi Failed!\n");
    return;
  }

  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // Initialize SPIFFS
  if (!SPIFFS.begin(true))
  {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }

We initialize our Serial monitor baud rate, connect to your Wifi, and open the SPIFFS Filesystem.

  wifiCar.start(onWsEvent);

We start our wifi car. That is it for the code.

  1. Upload the project to the ESP32 MCU. Click the Upload and monitor in the tasks section and wait for the Upload to succeed. Take note of the IP Address at the end as we will need this.
ESP32 Wifi Car Library - PlatformIO Upload and Monitor
  1. Disconnect your ESP32 from your Wifi and connect now the battery pack.
  2. Open your mobile browser from your phone. You should now be seeing the following application displayed in your mobile browser. If there are no issues in your wiring then your car should move correctly.

Congratulations you now have created your own ESP32 Wifi Car using the library that I have created!

Issues

Wrap Up

We have gone thru how to use the ESP32 Wifi car library that I have created in this post. This library will help you in creating your own Wifi car with less code. I hope you had fun doing this project!

Happy Exploring!

If you like my post then please consider sharing this. Thanks!

One response to “Create your own ESP32 Wifi Car using the library esp32-wifi-car”

  1. ESP32 Robot Car Using Websockets – donskytech.com

    […] I have created this project into an Arduino library so that it would be easier to use it in your project. If you are interested then please look Create your own ESP32 Wifi Car using the library esp32-wifi-car. […]

Leave a Reply

Your email address will not be published. Required fields are marked *