Bare minimum code for Python

Hi all just been thrown a nice curve ball.

I have been asked to use tello edu to teach python coding.

What is the bare minimum code I need for python to connect to a drone and get it to take off, then land.

I can then add bits and pieces later on into between and then talk about variables and loops.

I had a quick look at the Python code lessons but got a bit confused on what to take out and ended up spending ages trying to debug my Frankenstein code

Did you go through DroneBlocks curriculum yet. I was able to get started with Python coding using thr curriculum.

Yep I went through the programme and got drones working just want to strip out everything and just want drone to connect take off and land.

I edited the code to what I thought was right but got lots of errors, when correct others appeared and got frustrating

Oh ok. Can you put your code here. Let’s have a look at it.

import socket
import threading
import time

IP and port of Tello

tello_address = (‘192.168.10.1’, 8889)

IP and port of local computer

local_address = (’’, 9000)

Create a UDP connection that we’ll send the command to

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

Bind to the local address and port

sock.bind(local_address)

Send the message to Tello and allow for a delay in seconds

def send(message, delay):

Try to send the message otherwise print the exception

try:
sock.sendto(message.encode(), tello_address)
print("Sending message: " + message)
except Exception as e:
print("Error sending: " + str(e))

Delay for a user-defined period of time

time.sleep(delay)

Receive the message from Tello

def receive():

Continuously loop and listen for incoming messages

while True:

Try to receive the message otherwise print the exception

try:
response, ip_address = sock.recvfrom(128)
print("Received message: " + response.decode(encoding=‘utf-8’))
except Exception as e:

If there’s an error close the socket and break out of the loop

sock.close()
print("Error receiving: " + str(e))
break

Create and start a listening thread that runs in the background

This utilizes our receive functions and will continuously monitor for incoming messages

receiveThread = threading.Thread(target=receive)
receiveThread.daemon = True
receiveThread.start()

Put Tello into command mode

send(“command”, 3)

Send the takeoff command

send(“takeoff”, 5)

Land

send(“land”, 5)

Print message

print(“Mission completed successfully!”)

Close the socket

sock.close()

Dont worry I have sorted it ready to rock and roll

Would you mind sharing your final solution so that others can benefit? Interested in seeing what you came up with!

It was the same code as above I was messing around with spaces and tabs when not needed.

Can see it running here

https://youtu.be/urVWB3TOQs8

1 Like

Bravo! I just learned about QPython from your video. We have been using Pythonista for iOS. Thanks for sharing.

The power of sharing it is a great little app and for me it is a game changer and for the kids I teach. I wonder if we can get it to swarm which is my final aim

I went for the OL version as was aimed more at learning and the 3L was more advanced. Always use Python 3 not python 2. I was sitting on the bus coding on phone love it

We’ll call you the “Mobile Coder”

Just FYI we have some swarm sample code here:

There will be a few challenges to get this set up but happy to assist. If you’d like to chat about it please crate a new post so we keep things clean. Thanks.

Lol thanks I will wear that badge with pride. My aim is to have mobile as my default option to teach as all can be easily transferred to PCs and Laptops.

Will tinker with my nemesis AKA swarming to get this working on mobile.

May the force be with you, always