In this lesson, we will teach you how to use mobile APP to control Robot car through UDP protocol. The Raspberry Pi will run a C++  program to get UDP packet from APP.

You will learn how to use C++ to create socket and  get remote data (UDP packet) from mobile APP.

If you are interested in Python programming, this project has a Python version in following link:
https://osoyoo.com/2020/08/01/osoyoo-raspberry-pi-v2-0-car-lesson-4-use-mobile-app-to-control-the-car-with-udp-protocol/

No. Picture Device Qty. Accessories Link
5 7pin 25cm Female to Female Cable 1 Click here to buy
6 20Pin jumper wire female to female 20cm some
7 Philips screwdriver 1 Click here to buy

Step 1: Install the smart car basic frame work as per lesson 1

Osoyoo WIFI Robot APP search “Osoyoo WIFI UDP Robot APP” in
Google Play or Apple Store

Step 1) Download OSOYOO WIFI UDP Robot Car control APP
In Google Play or Apple Store, please search key words “OSOYOO WIFI UDP Robot Car”, you will find an orange icon APP as following:

Step 2) If you haven’t install PCA9685 library in lesson 1, please type following command to download the PCA9685 library.

wget http://osoyoo.com/driver/p3-car/pca9685.tar.gz
tar -zxvf pca9685.tar.gz

Step 3)Type following command to download the sample code:

wget http://osoyoo.com/driver/p3-car/lesson4.c

Step 4) Compile our sample   C  code by typing the command:

gcc -pthread -o lesson4 lesson4.c pca9685/pca9685.c -lwiringPi 

Step 5) Now we can run the program by typing following command:

./lesson4

After above command is executed, your car is waiting for command from  your cell phone.

Step 5) Connect your phone with the same router wifi SSID your raspberry pi use. Open the APP, click Settings, enter your Raspberry Pi IP address and Port to 8888 in settings:

Now your  can click the ◄ ► ▲ ▼ direction keys to make the car move. Use || pause key to stop the car movement.

Note: F1~F6 are further development functions in the future.

FAQ about the WIFI UDP APP and sketch Code:

Q1)How to tune the robot car speed?
A: If you want change the speed performance of the robot car, please change values following parameters in line 36-38 in lesson4.c file :

#define high_speed 3000  // Max pulse length out of 4096
#define mid_speed  2000  // Max pulse length out of 4096
#define low_speed  1350  // Max pulse length out of 4096

Q 2)What happened when you press  buttons in OSOYOO WiFi UDP Robot Car APP ?
A: When you press a button of the APP, APP will send a single-letter message through UDP protocol to target device (in this example, our Arduino  WIFI Shield)

Button UDP message
F1 F
F2 G
F3 H
F4 I
F5 J
F6 K
A
B
R
L
square E
obstacle O
tracking T

Q3)How does Arduino handle the UDP command?
Line 170 to 173   receives UDP data from APP and give it to variable buff (char[] array), switch statement in line 177 – 198 handle the APP command saved in buff[0] variable :

    switch(buf[0])
    {
            case 'A':
            go_Advance( fd,mid_speed,mid_speed);
            break;

            case 'B':
            go_Back( fd,mid_speed,mid_speed);
            break;

            case 'L':
            go_Left( fd,0,low_speed);
            break;

            case 'R':
            go_Right( fd,low_speed,0);
            break;

            case 'E':
            stop_car( fd);
            break;
      }

For example , when APP ▲ key is pressed , buff[0] value is A , then ticker() function call go_Advance( fd,mid_speed,mid_speed) function to make car moving forward.