Practical Example. Toy hacking day - coding

Coding

/*********

 Toy hacking Sample.

WiFi remote control of of-road car with 2 DC-motors

Using free Blynk IoT platform

A.Toshkov

  Permission is hereby granted, free of charge, to any person obtaining a copy

  of this software and associated documentation files.


*********/



#define BLYNK_PRINT Serial

#include <WiFi.h>

#include <WiFiClient.h>

#include <BlynkSimpleEsp32.h>


// Motor A connections

int dcF = 32;

int dcB = 33;


// Motor B connections

int dcL = 26;

int dcR = 27;


// int dcS Stop both motors

int dcS = 2;


char auth[] = "********* Your Blynk security code ************";  // You should get Auth Token 

                                         //in the Blynk App. Auth. code is send to your e-mail

char ssid[] = "***Your network SSID name*********";    // Your Wi-Fi Credentials

char pass[] = "****Your network PASSWORD ********";



void setup() {  

  

    pinMode(dcF, OUTPUT);  

    pinMode(dcB, OUTPUT);  

    pinMode(dcL, OUTPUT);  

    pinMode(dcR, OUTPUT);   

    pinMode(dcS, OUTPUT);

    pinMode(dcF, LOW);  

    pinMode(dcB, LOW);  

    pinMode(dcL, LOW);  

    pinMode(dcR, LOW);

    pinMode(dcS, HIGH);

    

  Serial.begin(115200);

  delay(10);

  Serial.print("Connecting to ");

  Serial.println(ssid);

  WiFi.begin(ssid, pass);

  //int wifi_ctr = 0;

  while (WiFi.status() != WL_CONNECTED) {

  delay(500);

  Serial.print(".");

  }

  Serial.println("WiFi connected");  

  Blynk.begin(auth, ssid, pass);

}


void loop(){

  if (digitalRead(dcS) == HIGH)

  {

    pinMode(dcF, LOW);  

    pinMode(dcB, LOW);  

    pinMode(dcL, LOW);  

    pinMode(dcR, LOW);

    delay(10);

  }

        Blynk.run();

}


If you use Blynk IoT platform, you will be needing an account and registration.

In this case you should create the remote-control interface, using Blynk online instruments.

This is easy for use and understand. 

There you can see the control panel, for creating your web based remote control.


Working on the Blynk platform is intuitive for beginner applications and this is probably the easiest way to good decision. However, the free account is very limited in terms of the number of components used. You will need to write a management program using your own server if you want to write more complex application.

Fortunately, this is fully available for ESP32 at Arduino IDE.


The basic code for such Toy hacking Sample for Wi-Fi remote control of off-road car with 2 DC-motors using own WebSerwers is given below. 


/*********

Toy hacking Sample.

WiFi remote control of of-road car with 2 DC-motors

Using own WebSerwer

A.Toshkov

  Permission is hereby granted, free of charge, to any person obtaining a copy

  of this software and associated documentation files.



*********/


#include <WiFi.h>

#include <ESPAsyncWebServer.h>

//#include <AsyncTCP.h>


// REPLACE WITH YOUR NETWORK CREDENTIALS

const char* ssid = "***Your network SSID name*********";

const char* password = "****Your network PASSWORD ********";


const int output26 = 26;

const int output27 = 27;

const int output32 = 32;

const int output33 = 33;


// HTML web page

const char index_html[] PROGMEM = R"rawliteral(

<!DOCTYPE HTML><html>

  <head>

    <title>ESP Pushbutton Web Server</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <style>

      body { font-family: Arial; text-align: center; margin:0px auto; padding-top: 30px;}

      .button {

        padding: 20px 40px;

        font-size: 24px;

        text-align: center;

        outline: none;

        color: #fff;

        background-color: #2f4468;

        border: none;

        border-radius: 5px;

        box-shadow: 0 6px #999;

        cursor: pointer;

        -webkit-touch-callout: none;

        -webkit-user-select: none;

        -khtml-user-select: none;

        -moz-user-select: none;

        -ms-user-select: none;

        user-select: none;

        -webkit-tap-highlight-color: rgba(0,0,0,0);

      }  

      .button:hover {background-color: #1f2e45}

      .button:active {

        background-color: #1f2e45;

        box-shadow: 0 4px #666;

        transform: translateY(2px);

      }

    </style>

   </head>

  <body>

    <h1>ESP Pushbutton Web Server</h1>

    <button class="button" onmousedown="toggleCheckbox('onF');" ontouchstart="toggleCheckbox('onF');" onmouseup="toggleCheckbox('offF');" ontouchend="toggleCheckbox('offF');" onmouseout="toggleCheckbox('offF');" onmouseover="toggleCheckbox('onF');" onclick="toggleCheckbox('offF');">Forward</button>

    <button class="button" onmousedown="toggleCheckbox('onB');" ontouchstart="toggleCheckbox('onB');" onmouseup="toggleCheckbox('offB');" ontouchend="toggleCheckbox('offB');" onmouseout="toggleCheckbox('offB');" onmouseover="toggleCheckbox('onB');" onclick="toggleCheckbox('offB');">Back</button>

    <button class="button" onmousedown="toggleCheckbox('onL');" ontouchstart="toggleCheckbox('onL');" onmouseup="toggleCheckbox('offL');" ontouchend="toggleCheckbox('offL');" onmouseout="toggleCheckbox('offL');" onmouseover="toggleCheckbox('onL');" onclick="toggleCheckbox('offL');">Left</button>

    <button class="button" onmousedown="toggleCheckbox('onR');" ontouchstart="toggleCheckbox('onR');" onmouseup="toggleCheckbox('offR');" ontouchend="toggleCheckbox('offR');" onmouseout="toggleCheckbox('offR');" onmouseover="toggleCheckbox('onR');" onclick="toggleCheckbox('offR');">Right</button>


   <script>

   function toggleCheckbox(x) {

     var xhr = new XMLHttpRequest();

     xhr.open("GET", "/" + x, true);

     xhr.send();

   }

  </script>

  </body>

</html>)rawliteral";


void notFound(AsyncWebServerRequest *request) {

  request->send(404, "text/plain", "Not found");

}


AsyncWebServer server(80);


void setup() {

  Serial.begin(115200);

  WiFi.mode(WIFI_STA);

  WiFi.begin(ssid, password);

  if (WiFi.waitForConnectResult() != WL_CONNECTED) {

    Serial.println("WiFi Failed!");

    return;

  }

  Serial.println();

  Serial.print("ESP IP Address: http://");

  Serial.println(WiFi.localIP());

  

  pinMode(output26, OUTPUT);

  digitalWrite(output26, LOW);

  pinMode(output27, OUTPUT);

  digitalWrite(output27, LOW);

  pinMode(output32, OUTPUT);

  digitalWrite(output32, LOW);

  pinMode(output33, OUTPUT);

  digitalWrite(output33, LOW);

 

// Send web page to client

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){

    request->send_P(200, "text/html", index_html);

  });


  // Receive an HTTP GET request for Left/Rignt

  server.on("/onL", HTTP_GET, [] (AsyncWebServerRequest *request) {

    digitalWrite(output26, HIGH);

    request->send(200, "text/plain", "ok");

  });


  // Receive an HTTP GET request

  server.on("/offL", HTTP_GET, [] (AsyncWebServerRequest *request) {

    digitalWrite(output26, LOW);

    request->send(200, "text/plain", "ok");

  });

  

//За бутона към Пин 27.   

  // Receive an HTTP GET request

  server.on("/onR", HTTP_GET, [] (AsyncWebServerRequest *request) {

    digitalWrite(output27, HIGH);

    request->send(200, "text/plain", "ok");

  });


  // Receive an HTTP GET request

  server.on("/offR", HTTP_GET, [] (AsyncWebServerRequest *request) {

    digitalWrite(output27, LOW);

    request->send(200, "text/plain", "ok");

  });

  



  // Receive an HTTP GET request for Forward/Back

  server.on("/onF", HTTP_GET, [] (AsyncWebServerRequest *request) {

    digitalWrite(output32, HIGH);

    request->send(200, "text/plain", "ok");

  });


  // Receive an HTTP GET request

  server.on("/offF", HTTP_GET, [] (AsyncWebServerRequest *request) {

    digitalWrite(output32, LOW);

    request->send(200, "text/plain", "ok");

  });

  

//За бутона към Пин 33.   

  // Receive an HTTP GET request

  server.on("/onB", HTTP_GET, [] (AsyncWebServerRequest *request) {

    digitalWrite(output33, HIGH);

    request->send(200, "text/plain", "ok");

  });


  // Receive an HTTP GET request

  server.on("/offB", HTTP_GET, [] (AsyncWebServerRequest *request) {

    digitalWrite(output33, LOW);

    request->send(200, "text/plain", "ok");

  });

 

  server.onNotFound(notFound);

  server.begin();

}


void loop() {

 

}


Before putting your car into operation, I recommend that you first test the code with LEDs only. When you set everything to work with them, then move on to the implementation of the management of the hardware part of the car.


After the implementation of this part of the project, in the next steps of the project you can add new hardware and code, such as a camera, distance sensors, a LED colored music and more.



Last modified: Thursday, 15 July 2021, 9:59 AM