#Coding4Fun – How to control your #drone with 20 lines of code! (2/N)

Buy Me A Coffee


I my previous posts I shared some links about the DJI Tello drone. One of them is the SDK In this document we can find the main commands and descriptions of the specifics commands to use to communicate with the drone.

dji tello drone sdk architecture

The document also links a Python sample file with the following code:

# Tello Python3 Control Demo
# http://www.ryzerobotics.com/
# 1/1/2018
# official source: https://terra-1-g.djicdn.com/2d4dce68897a46b19fc717f3576b7c6a/Tello%20编程相关/Both/Tello3(1).py
import threading
import socket
import sys
import time
import platform
host = ''
port = 9000
locaddr = (host,port)
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tello_address = ('', 8889)
def recv():
count = 0
while True:
data, server = sock.recvfrom(1518)
except Exception:
print ('\nExit . . .\n')
print ('\r\n\r\nTello Python3 Demo.\r\n')
print ('Tello: command takeoff land flip forward back left right \r\n up down cw ccw speed speed?\r\n')
print ('end — quit demo.\r\n')
#recvThread create
recvThread = threading.Thread(target=recv)
while True:
python_version = str(platform.python_version())
version_init_num = int(python_version.partition('.')[0])
# print (version_init_num)
if version_init_num == 3:
msg = input("");
elif version_init_num == 2:
msg = raw_input("");
if not msg:
if 'end' in msg:
print ('…')
# Send data
msg = msg.encode(encoding="utf-8")
sent = sock.sendto(msg, tello_address)
except KeyboardInterrupt:
print ('\n . . .\n')

The code is very easy to read:

  • Lines 10-14. Defines the main libraries to be used. I never used sockets and threads in Python, so this is an excellent chance for me to learn about this
  • Lines 16-26. Implements the basic communication via UDP described in the architecture from the SDK. Accessing the drone via IP and port 8889, and bind to localhost with post 9000
  • Lines 28-48. Function to receive data from the drone. It’s executed in an different thread, so here are my 1st multi-threading python app.
  • Lines 50-74. Main App Loop, where it waits for user type command and send the command to the drone. It also checks for Python version, and close the socket before end.

An amazing way to start playing with the drone. Just 5 minutes to connect and have something up and running.

Happy coding!


El Bruno


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.