Roboter Fahrzeug mit Blynk vom Smartphone steuern

Welcome to a new blog from our series on Robot Cars. This time we want to control our small car with our smartphone. That's what we use the Blynk app for.

 

Hardware needed

Vehicle variant 1: kit

Advantage: Pricing, all necessary parts available
Advantage: Bad steering, fixing of MCU and sensors is only difficult with the existing fixing holes.

Number

Component

Note

1

Set with 2wheels

 

 

Vehicle variant 2: From the 3D printer

Advantage: Thanks to the roller ball as a follow-up wheel, the steerability is better. There are mounting holes for the MCU and sensors. It is also intended to install a servo for camera or ultrasound sensor.
Advantage: A 3D printer is required.

Number

Component

Note

Number

Component

Note

1

2 Motors with wheels

 

1

Rolling ball as follow-up

 

6

Screws M3 x 30 with nut

for motors and roller balls

2

Screw M3 x 12 with nut

for Powerpack holder

1

Chassis from the 3D printer

 

1

Powerpack holder from 3D printer

 

 

As a microcontroller, we need an ESP8266 or rather an ESP32 because of the WLAN connection. The ESP-32 has the advantage that significantly more I/O pins are available, which will be important for future projects using sensors.

Number

Component

Note

Number

Component

Note

1

D1 Board NodeMCU ESP8266MOD-12F

 

or 1

ESP32 D1 R32 Development Board

 

1

4-Channel L293D Shield Driver

 

 

For the motor driver Shield to be used with the ESP controllers, small conversion work is necessary as the ESP controllers work with 3.3 V supply voltage.


A 10kOhm resistance to GND should be connected from connection D7. This is necessary because at the shield the pin D7 is connected to 5V via a pullup resistance of 10KOhm.
The A0 to A5 connections, as well as the associated supply connections, should be equipped with pin bars. In order for these connections to be used for sensors, the supply voltage must also be connected to 3.3V instead of 5V. This is done simply by separating the connection line to the pins at the bottom. Then you can connect the pins to the 3.3V pin.
The supply voltage connections should also be fitted.

The exits M2 and M4 are used to connect the two engines, as M1 and M3 cannot be controlled with the ESP8266 board. The motor connections will be connected to the corresponding screw terminals on the motor shield. If the vehicle is supplied via the USB socket at the microcontroller, the Jumper (red circle) must be removed and the entry M+ connected to the 5V connection of the microcontroller (green).

The motor shield is simply mounted on the microcontroller board. Further wiring measures are not required.
For the mechanical attachment of the boards, the chassis from the 3D printer has holes where the microcontroller board can be screwed with remote parts. The kit may require drilling holes.

Software

For remote control of the vehicle the freely available software Blynk is to be used. Blynk is a kick-start project that allows you to control microcontrollers with a smartphone as easily as possible. The main element is the app that can be used to create an application via a type of kit. All information about this application is stored in the Blynk server. Each application shall be assigned a unique identification number. On the microcontroller side, a library ensures that the microcontroller communicates with the Blynk server and that the application on the smartphone can directly read or control the pins of the microcontroller. No further programming on the microcontroller is required. The only condition is that the microcontroller must have access to the Blynk server. However, since no pins of the microcontroller can be directly controlled for the remote control of the vehicle, a little programming will be necessary.

But first you have to install the Blynk app on your smartphone. After the app has been installed and started, the login screen appears. To use Blynk, you need an account for the Blynk server. This requires only an email address and any password.

After you have created an account, a message will appear on the topic of energy. To create a block project you need a certain amount of energy for each screen element. With a new account you automatically get 2000 energy points with which you can work. If you need more energy points for a project, you can buy them.


After this message window the start screen appears. Here you can now load the Robocar project using the following QR code.

 

You will now see the project window. When you click on the Mother symbol, the project settings will open. In the project settings you will find the link "Email all". If you click on it, you will receive an email with the key that your microcontroller sketch needs to communicate with the Blynk server. Use the triangle icon on the project window on the top right to start the application.

 

More information about Blynk can be found on the Internet or in SmarthomeYeah. The book also describes how to install your own private Blynk server on a Raspberry Pi.

The sketch

In addition to the package for the ESP32 or ESP8266, you need the Blynk library, which can be installed via the library administration.

 

#ifdef ESP32
  //Constant and include files for ESP32
  #include <WiFi.h>
  #include <BlueSimpleEsp32.h>
  //Pins for the rotary direction slider register 
  #define SHIFT IN 12       //Data entry Arduino D8
  #define SHIFT CLK 17      //Arduino D4 slider
  #define LATCH CLK 19     //Storage rate Arduino D12
  #define Out of ENABLE 14    //Activate Arduino D7 with LOW output
  
  //PWM for engines speed between 0 and 1023
  #define M1PWM 23   //Pin for Motor1 Arduino D11
  #define MORIGHT 25    //Pin for Motor2 Arduino D3
  #define M3PWM  27   //Pin for Motor3 Arduino D6
  #define MILF 16   //Pin for Motor4 Arduino D5
  
  //Servo connections
  #define SERVICE 5  //Pin for Servo1 D10
  #define SERVICES2 13   //Pin for Servo2 D9
#endif

#ifdef ESP8266
  //Constant and include files for ESP8266
  #include <ESP8266WiFi.h>
  #include <BlueSimpleEsp8266.h>

  //Pins for the rotary direction slider register 
  #define SHIFT IN 0       //Data entry Arduino D8
  #define SHIFT CLK 4      //Arduino D4 slider
  #define LATCH CLK 12     //Storage rate Arduino D12
  #define Out of ENABLE 13    //Activate Arduino D7 with LOW output
  
  //PWM for engines speed between 0 and 1023
  #define M1PWM 13   //Pin for Motor1 Arduino D11 double occupancy!
  #define MORIGHT 5   //Pin for Motor2 Arduino D3
  #define M3PWM  12  //Pin for Motor3 Arduino D6 double occupancy!
  #define MILF 14   //Pin for Motor4 Arduino D5
  
  //Servo connections
  #define SERVICE 15  //Pin for Servo1 D10
  #define SERVICES2 2   //Pin for Servo2 D9
#endif

//For Blynk Debug Messages
#define BLYNK PRINT Serial

// The following constants must be replaced by your personal data
// BLYNK authentication key from the email
#define KEY. "+++++++++++++++++++++++++++++++++++++"
// SSID of the local WLAN
#define SSID "************************************"
// Passkey for the WLAN
#define PSK "**************************************"
// Address of a local Blynk server or "" for the public 
#define SERVICES  ""

//Bits in the engine register
#define M1A 2   //Motor1 A
#define M1B 3   //Motor1 B
#define M2A 1   //Motor2 A
#define M2B 4   //Motor2 B
#define M3A 5   //Motor3 A
#define M3B 7   //Motor3 B
#define M4A 0   //Motor4 A
#define M4B 6   //Motor4 B


//Engine assignment
#define COURT 2
#define DEVELOPMENT  4

//Constants for rotation
#define STOP 0
#define FORWARD 1
#define BACKWARD 2

//Current contents of the directional slider register
uint32 road map = 0;

//Maximum speed
int with;

//Fills the engine control slider register
//with the content of directions
void sendDirections() {
  u t i;
  digitalWrite(LATCH CLK, LOW);  //Lock memory
  digitalWrite(SHIFT IN, LOW);   //Entry to 0
  for (i=0; i<8; i++) {
    digitalWrite(SHIFT CLK, LOW);//Read bit for bit
    if (road map & bit(7-i)) {
      digitalWrite(SHIFT IN, HIGH);
    } ed {
      digitalWrite(SHIFT IN, LOW);
    }
    digitalWrite(SHIFT CLK, HIGH);
  }
  digitalWrite(LATCH CLK, HIGH); //Save with the positive flank
}

//Set the direction of rotation for an engine
void setDirection(u t engine, u t direction) {
  u t a=0, b=0;
  //Determine bitnumbers for the selected engine
  switch (engine) {
    case 1: a=M1A; b=M1B; break;
    case 2: a=M2A; b=M2B; break;
    case 3: a=M3A; b=M3B; break;
    case 4: a=M4A; b=M4B; break;
  }
  //first set both bits for the engine to 0 means STOP
  road map &= ~ bit(a) & ~ bit(b);

  switch (direction) {
    case FORWARD: road map |= bit(a); break; //For foreword Bit A to 1
    case BACKWARD: road map |= bit(b); break;//For BACK VALUES Bit B on 1
  }
  //Serial.printf("Directions =%x\n",directions); 
  sendDirections(); //Send new setting to slider register
}


// neutral zone for x and y
// The joystick must be outside this range to activate an engine
int min = 312;
int maxRange = 712;

//Turn left and right engine.
//The parameters left and right contain the speed
//Negative values mean backwards
void Engines(int16u t left, int16u t Right.) {
  //Serial.printf("Links%i, right%i\n",left,right);
  //direction
  if (left<0) {
    setDirection(DEVELOPMENT,BACKWARD);
  } ed if (left == 0){
    setDirection(DEVELOPMENT,STOP);
  } ed {
    setDirection(DEVELOPMENT,FORWARD);
  }
  if (Right.<0) {
    setDirection(COURT,BACKWARD);
  } ed if (Right. == 0){
    setDirection(COURT,STOP);
  } ed {
    setDirection(COURT,FORWARD);
  }
  //PWM output is different for ESP32 and ESP8266
  #ifdef ESP32
    //Speed
    ledcWrite(DEVELOPMENT,Abs(left));
    ledcWrite(COURT,Abs(Right.));
  #endif
  #ifdef ESP8266
    //Speed
    analog(MILF,Abs(left));
    analog(MORIGHT,Abs(Right.));
  #endif
  
}

// Evaluation of Joystick data
void moveControl(int x, int y) {

Move forward

  if(y >= maxRange && x >= min && x <= maxRange) { //forward
    Engines(with,with);
  } ed if(x <= min && y >= maxRange ) { //move forward right
    Engines(0.7*with,0.3*with);
  } ed if(x >= maxRange && y >= min && y<=maxRange) { //move right
    Engines(with,-with);
  } ed if(x <= min && y >= maxRange ) { Move forward LEFT
    Engines(0.3*with,0.7*with);
  } ed if(x <= min && y >= min && y <= maxRange) { //move left
    Engines(-with,with);
 } ed if(y < maxRange && y > min && x < maxRange && x > min) {// neutral zone
    Engines(0,0);
 } ed if(y <= min && x >= min && x <= maxRange) { Move back
    Engines(-with,-with);
 } ed if(y <= min && x <= min) { Move back and right
    Engines(-0.7*with,-0.3*with);
 } ed if(y <= min && x >= maxRange) { Move back and left
    Engines(-0.3*with,-0.7*with);
 } 
}

//Setup
void setup() {
  Serial.start(115200);
  Serial.Print();
  Serial.Print("Autumn");
  //Set all used pins as OUTPUT
  pinMode(SHIFT IN,OUTPUT);
  pinMode(SHIFT CLK,OUTPUT);
  pinMode(LATCH CLK,OUTPUT);
  pinMode(Out of ENABLE,OUTPUT);
  //PWM for ESP32 
  #ifdef ESP32
    //PWM for ESP32 
    //ledCSetup(channel, frequency, resolution bit)
    ledcSetup(DEVELOPMENT, 100, 10);
    ledcSetup(COURT,100, 10);
    //Engine pins assign to channels
    ledcAttachPin(MILF,DEVELOPMENT);
    ledcAttachPin(MORIGHT,COURT);  
  #endif
  #ifdef ESP8266
    //ESP8266 Motor Pins easy to exit
    pinMode(MILF,OUTPUT);
    pinMode(MORIGHT,OUTPUT);
  #endif
  //All engines stop
  road map = 0;
  sendDirections();  //Send to Slider Register
  digitalWrite(Out of ENABLE,0); //Release Slider Log Outputs
  

  Serial.Print("Start BLYNK");
  //Depending on public or private server
  //different start for Blynk
  if (SERVICES=="") {
    Blue.start(KEY., SSID, PSK);
  } ed {
    Blue.start(KEY., SSID, PSK, SERVICES, 8080);
  }
}

void run() {  
  //here the communication with the Blynk server takes place
  Blue.run(); 
}

//Virtual Pin V1 has received data
BLYNK WRITE(V1)
  //Read and evaluate Joystick values
  { int x = stop[0].Asinth();
  int y = stop[1].Asinth();
  moveControl(x,y); }

  //Virtual Pin V2 has received data
BLYNK WRITE(V2) 
  { 
    //Read value and set maximum speed
    with = stop[0].Asinth(); 
  }

 Sketch to download


Chassis from the 3D printer


File for the chassis

File for the battery pack holder

 

 

Contribution as PDF

Have fun with the robot car

 

AmateurfunkEsp-32Esp-8266Projects for beginners

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN