Index>Robot Kit>RaspArm-S Kit for RPi>Lesson 8 Simple TCP Communication

Lesson 8 Simple TCP Communication

2321

In this lesson, we use remote control of the servo as a TCP communication example. This example helps beginners understand how the desktop GUI program communicates with the Raspberry Pi.

You can use the program with a graphical interface written by yourself to communicate with the Raspberry Pi on other devices to achieve the purpose of controlling the Raspberry Pi.

8.1 Components used in this course


image.png


8.2 About Tkinter and Socket

The GUI programming method introduced in this chapter is completely done by Python language, specifically, the Tkinter library is used.Tkinter is Python's standard GUI library. Python uses Tkinter to quickly create GUI applications. Because Tkinter is built into the Python installation package, as long as Python is installed, you can import the Tkinter library, and IDLE is also written in Tkinter. For simple graphical interface Tkinter can still cope with it.

We use the Socket library to communicate between devices. Socket is also called "socket". Applications usually send requests to the network or answer network requests through the "socket", so that the process between the host or a computer can communicate

If you do not use Robot HAT driver board to connect with Raspberry Pi driver board, then you need to connect Vin of OLED screen to 5V or 3.3V of Raspberry Pi, and connect GND of OLED screen to GND of Raspberry Pi. Connect SCL of Robot HAT to SCL of OLED, and SCA of Robot HAT to SCA of Raspberry Pi. Please refer to the pin definition diagram of Raspberry Pi for specific pins.



8.3 Wiring diagram (Circuit diagram)


image.png


8.3 How to remotely control Servo

We use the Raspberry Pi as the server and the PC as the client.

8.3.1Run the tcpPI.py program in the Raspberry Pi first

1. Open the terminal software MobaXterm:


image.png


2. Log in to your Raspberry Pi (the way to log in to Raspberry Pi has been introduced in Lesson 1):

image.png


3. The relevant code programs of the RaspArm-S robot are stored in the adeept_rasparms folder, which has been explained in "2.1 Downloading the code program for controlling the robot" in Lesson 2. First, you need to enter a command with the command window of the Raspberry Pi to enter the folder where the course code is stored: CourseCode, this folder stores the sample code program for each course, enter the following command:

cd  adeept_rasparms/CourseCode

image.png


4. Enter the command to display the contents of the current directory:

ls

image.png


5.  The 04TCP_servo folder contains the sample code of this lesson. Enter the command to enter this folder:

cd  04TCP_servo

image.png


6. Enter the command to display the contents of the current directory:

ls

image.png


7.tcpPC.py needs to be run on the PC, you need to modify the IP insideimage.png to the IP of your Raspberry Pi, so that the connection can be established; tcpPI.py runs on the Raspberry Pi.When using the 180°Servo module, we need to install the Python dependency library which is needed to control the 180°Servo: Adafruit_PCA9685, and enter the following command in the console of the command window:

sudo pip3 install adafruit-pca9685

image.png

 

8. Then run the tcpPI.py program on the Raspberry Pi, and directly enter the following commands to run this program on the Raspberry Pi:

sudo  python3  tcpPI.py

image.png

 

9. After successfully running the program, you also need to run the tcpPC.py program on the PC. You need to change the IP inside to the IP of your Raspberry Pi, so that the connection can be established.


8.3.2Run the tcpPC.py program on the PC

1. In the directory:

adeept_asparms/CourseCode/04TCP_servo

Download the file tcpPC.py to the PC and remember the path of this file.

image.png 


2. Use MobaXterm or other IDE to open the tcpPC.py file on your computer. You need to modify the IP address in the SERVER_IP line of code and change it to the IP of your Raspberry Pi (please check the IP address of the Raspberry Pi in lesson one), so that the PC can establish a connection with the Raspberry Pi.

image.png 


3. After the modification is completed, you can directly double-click to open the tcpPC.py file. After opening, as shown below, there are two buttons "ON" and "OFF". When you click the "ON" button, the servo will rotate; when you click the "OFF" button, the servo will stop rotating.

image.png 


8.4 Studying the program code of tcpPI.py and tcpPC.py

8.4.1 Learning tcpPI.py program code

Import the socket library which is used for TCP communication.

image.png 


Define a flag fun_flag to control the servo; then use multi-thread to control the servo.

image.png

 

Next is the configuration related to TCP communication. PORT is the defined port number. You can freely choose numbers from 0-65535. It is recommended to choose the numbers after 1023, which needs to be consistent with the port number defined by the client in the PC.

image.png

 

Start monitoring the client connection, and start receiving the information sent from the client after the client connection is successful.

image.png 


Receive the information from the client, if the information content is on, the servo rotates, if the information content is off, the servo stops rotating.

image.png 


Finally, print out the received data, and continue to monitor the next message sent by the client.

image.png 


8.4.2 Learning tcpPC.py program code

Import the socket library used for TCP communication.

image.png 


Python uses Tkinter to quickly create GUI applications and instantiate them while importing them.

image.png 


Call this method to send the command'on' to control the rotation of the servo.

image.png 


Call this method to send the command'off' to stop the servo.

image.png 


Enter the IP address of the Raspberry Pi here (you need to change it to your Raspberry Pi IP address when you use it).

image.png 


Next is the configuration related to TCP communication. PORT is the defined port number. You can freely choose numbers from 0-65535. It is recommended to choose a number after 1023, which needs to be consistent with the port number defined by the server in the Raspberry Pi.

image.png 


Define a GUI window.

image.png 


Set the title of the window.

image.png 


The size of the window, the middle x is the English letter x.

image.png 


Define the background color of the window.

image.png 


Use Tkinter's Button method to define a button, the button is on the root window, the name on the button is 'ON', the text color of the button is #E1F5FE, and the background color of the button is #0277BD. When the button is pressed, it will call lights_on( )function.

image.png 


Choose a location to place this button.

image.png 


Define another button in the same way. The difference is that the text on the button is changed to'OFF'. When the button is pressed, the lights_off() function is called.

image.png 


Finally, start the message loop.

image.png