Robot Tank Car Kit Lesson 7:Control Robot Tank Car through WIFI and Bluetooth

Authorized Amazon Seller:

Robot Tank Car Chassis

Buy from US Buy from CA Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES

Robot Tank Car Electronic Parts Kit

Buy from US Buy from CA Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES

I. Objective:

In this lesson, we learn how to control the Robot car mobile APP through WiFi and Bluetooth. , we just use the ESP8266 WIFI board as expansion board and control the tank car via IR receiver in previous lessons. In this lesson, we will learn how to use the phone to connect the hot spot from esp8266 board and control the car movement via phone app. At the same time, you also can put the bluetooth into the esp8266 board and control the car movement via phone bluetooth.

II. How it Work

There is a hot spot called DoitWIFI_Config from ESP8266 board in working time and you could connect the hot spot via phone wifi without password. When turn the switch of ESP8266 board to “ON”, the message from the Arduino and esp8266 can be exchanged by serial ports.The control command can be send to ESP8266 via phone app and then send to Arduino via serial ports between Arduino and ESP8266 board.

When turn the switch of ESP8266 board to “1” and “2” position to disconnect the serial ports between Arduino and ESP8266 board. If you put the bluetooth module on esp8266 board and connect it via phone bluetooth, the message from the Arduino and esp8266 can be exchanged by serial ports via bluetooth module.

ESP8266-pin

Find more about the ESP8266 WIFI shiled board, please visit here: http://kookye.com/?p=5902

III. Software Installation:

Step 1: Install latest Arduino IDE (If you have Arduino IDE version after 1.1.16, please skip this step)
Download Arduino IDE from https://www.arduino.cc/en/Main/Software?setlang=en, then install the software.

Step 2:Download Lesson 7 sample code from http://www.kookye.com/download/Arduino_Tank_Car_Kit/tank_robot_lesson7.zip, unzip the download zip file tank_robot_lesson7.zip, you will see a folder called tank_robot_lesson7.

Step 3: Connect UNO R3 board to PC with USB cable, Open Arduino IDE -> click file -> click Open -> choose code “tank_robot_lesson7.ino” in tank_robot_lesson7 folder, load the code into arduino.

open

Step 4: Choose corresponding board and port for your project,upload the sketch to the board.

Note: Bluetooth module and Arduino board communicate with each other through UART serial port. Please remove bluetooth module before uploading sketch in Arduino board, or the sketch will be uploaded unsuccessfully.

port

Step 5: Download the APP and install it on you phone:

Android Phone APP:  http://kookye.com/download/Arduino_Tank_Car_Kit/car.apk

iOS Phone APP:  search osoyoo Arduino robot car in apple app store

Arduino Robot tank APP
Note: This android app can just support Android phone and we offer the app source code(http://kookye.com/download/Arduino_Tank_Car_Kit/BTcar-master.zip). You could revise and personalize the code as per your needs.

IV. Understand the Code:

This program is used to receive the control command from phone app.

//WiFi / Bluetooth through the serial control
void do_Uart_Tick()
{

  char Uart_Date=0;
  if(Serial.available()) 
  {
    size_t len = Serial.available();
    uint8_t sbuf[len + 1];
    sbuf[len] = 0x00;
    Serial.readBytes(sbuf, len);
    //parseUartPackage((char*)sbuf);
    memcpy(buffUART + buffUARTIndex, sbuf, len);//ensure that the serial port can read the entire frame of data
    buffUARTIndex += len;
    preUARTTick = millis();
    if(buffUARTIndex >= MAX_PACKETSIZE - 1) 
    {
      buffUARTIndex = MAX_PACKETSIZE - 2;
      preUARTTick = preUARTTick - 200;
    }
  }
  if(buffUARTIndex > 0 && (millis() - preUARTTick >= 100))//APP send flag to modify the obstacle avoidance parameters
  { //data ready
    buffUART[buffUARTIndex] = 0x00;
    if(buffUART[0]=='C') 
    {
      Serial.println(buffUART);
      Serial.println("You have modified the parameters!");//indicates that the obstacle avoidance distance parameter has been modified
      sscanf(buffUART,"CMD%d,%d,%d",&distancelimit,&sidedistancelimit,&turntime);
    }
    else  Uart_Date=buffUART[0];
    buffUARTIndex = 0;
  }
  switch (Uart_Date)    //serial control instructions
  {
    case '2':
            Drive_Status=MANUAL_DRIVE; Drive_Num=GO_ADVANCE;Serial.println("forward"); break;
    case '4':
            Drive_Status=MANUAL_DRIVE; Drive_Num=GO_LEFT; Serial.println("turn left");break;
    case '6':
            Drive_Status=MANUAL_DRIVE; Drive_Num=GO_RIGHT; Serial.println("turn right");break;
    case '8':
            Drive_Status=MANUAL_DRIVE; Drive_Num=GO_BACK; Serial.println("go back");break;
    case '5':
            Drive_Status=MANUAL_DRIVE; Drive_Num=STOP_STOP;buzz_off();Serial.println("stop");break;
    case '3':
            Drive_Status=AUTO_DRIVE_UO; Serial.println("avoid obstacles...");break;
    case '1':
          Drive_Status=AUTO_DRIVE_LF; Serial.println("line follow...");break;
    default:break;
  }
}

V. Hardware Installation:

Step 1: Install ESP8266 Expansion Board on UNO R3 board.
lesson2-HW-1

Step 2: Bluetooth Connection

 1. Put the bluetooth module into ESP8266 wifi Board as the following picture.

bluetooth-esp8266bluetooth-esp8266_p

2. If you use bluetooth control car, put switch in “1,2” position.

ESP8266_ON ESP8266_OFF

3. Put two 12865 batteries into battery box and turn the swith of battery box to “ON”.

4.If you control the robot car through Bluetooth, please turn on bluetooth of your Android or iOS phone which you have installed APP and scan bluetooth(different bluetooth module will scan different bluetooth name), Click connect and enter password “1234” or “0000” if no change (You can change wifi or bluetooth settings in Android phone to switch to control car through wifi or bluetooth):

Lesson7-V-step6-bluetooth

5. Open app>> select bluetooth mode>> then you can controller the robot car through bluetooth.

Lesson7-V-step7-bluetooth-setting

Step 3:Wifi Connection

 1. Pull the bluetooh out of esp8266 board and turn the switch of esp8266 to “on” position.

 2. If you want to control the robot car through WiFi, please scan WiFi hotspot with your Android or apple phone which you have installed APP and search for wifi named” DoitWIFI_Config”, Click connect(no password needed).

Lesson7-V-step4-wifi

3. Open app>> select WiFi mode>> then you can controller the robot car through wifi

Android_APP-576x1024

4.Open app>> select wifi mode>> then you can controller the robot car through phone wifi hotspot.

Lesson7-V-step5-wifi-setting

Note:

There are three working mode: manual control, obstacle avoidance and tracking. Users can switch freely among three working mode.
1) In manual control mode, you can click buttons (^) (V) (<) (>) to control the Robot car to move forward and backward, turn right and left. Meanwhile, the APP can observe the car real time movement.

2) Click “tracking” button of App to switch the current mode to tracking mode. The Robot car will move forward along the black line in white background. Meanwhile, the APP can observe the car real time movement. Press “||” button to stop moving and click other button to change Robot car working mode. To learn more about this mode, please review our lesson 4.

3) Click “Obstacle” button to switch the current mode to obstacle avoidance mode. The Robot car can be driven forward continuously and stopped and steered away once any obstacle in its way. Press “||” button to stop moving and then click other button to change Robot car working mode. To learn more about this mode, please review our lesson 5.