Tello Drone Meets Google Home…(Part-2)

Kamalakar Rao
3 min readMar 7, 2020

--

In the previous post, you have successfully updating the values in Firebase database using Google Assistant.
Now it’s time to control the drone based on changes in database.

Part-2 — Building a Python Program which controls the drone based on changes made in Firebase Database.

The Goal is to control the drone based on changes in database values.

We will be using following firebase configuration in code

import pyrebase
import time
from easytello import tello
import json


config = {
"apiKey": "YOUR_API_KEY",
"authDomain": "YOUR_FIREBASE_PROJECT_ID",
"databaseURL": "YOUR_FIRBASE_DATABASE_URL",
"serviceAccount": "PATH_TO_SERVICE_ACCOUNT_JSON",
"storageBucket":"YOUR_STORAGE_URL"
}

You can find Api Key & Project_id/authDomain in General tab under settings as shown

Api key & Project_id

Generate Service Account Json

Database Url can be found as follows

Save the database url

Saving the storage bucket

Let’s Dive into Code

Install following libraries

pip install easytello

pip install pyrebase

Here’s the Following code to control the drone

import pyrebase
from easytello import tello


config = {
"apiKey": "YOUR_API_KEY",
"authDomain": "YOUR_FIREBASE_PROJECT_ID",
"databaseURL": "YOUR_FIRBASE_DATABASE_URL",
"serviceAccount": "PATH_TO_SERVICE_ACCOUNT_JSON",
"storageBucket":"YOUR_STORAGE_URL"
}

firebase = pyrebase.initialize_app(config)
db = firebase.database()

my_drone = tello.Tello()


def stream_handler1(message):
print(message["event"]) # put
print(message["path"]) # /-K7yGTTEp7O549EzTYtI
print(message["data"]) # {'title': 'Pyrebase', "body": "etc..."}

distance = 20

if message['path'] == "/":
# msg_json = json.loads(json.dumps((message["data"])))
if "-" in message["data"]:
dir_val = message["data"]
print(dir_val)
d = dir_val.split("-")

if d[1] == "":
print("Nothing..Default to 3")
distance = 20
else:
distance = int(d[1])
print("-----")
print(d[0])
if d[0] == "upwards":
print("Going upwards")
my_drone.up(distance)

if d[0] == "downwards":
print("Going Backward")
my_drone.down(distance)

if d[0] == "forward":
print("Going Forward")
my_drone.forward(distance)

if d[0] == "backward":
print("Going Backward")
my_drone.back(distance)

if d[0] == "left":
print("Going left")
my_drone.left(distance)

if d[0] == "right":
print("Going right")
my_drone.right(distance)

if d[0] == "takeoff":
print("Taking Off")
my_drone.takeoff()

if d[0] == "land":
print("Landing")
my_drone.land()


my_status1 = db.child("tello_drone").child("direction").stream(stream_handler1, stream_id="switch1_status")

db.child(“tello_drone”).child(“direction”).stream(stream_handler1, stream_id=”switch1_status”)

This Line is used to continuously listen to changes in the database and stream handler1 function is called when there is any change in database value.

Time to take off the drone

Now Turn on the drone and you can see Tello-id on your wifi networks.
Notice that you will lose internet connection as you connect to drone.

You need to do a usb tethering from your mobile and make sure your PC has internet even when it is connected to drone wifi.

Now Run the above python code to start controlling from voice.

Initiate your Voice app by saying “Hey Google , Talk to Mr.Tello
Now you can say “takeoff” to update the value in database which will takeoff the drone.

Say “go forward for 80 cms” to make the drone move forward

Note: If you have difficulty in initiating your voice app as Mr.Tello then you change it to any name under Invocations -> Display name in actions on google console.

Hope you enjoyed the post and able to control the drone from Google Assistant.
Happy to code and share…
Will be sharing on object detection on Drone in upcoming posts.

Here’s My LinkedIn Profile to Help , Share and Learn.

--

--

Kamalakar Rao

Python Developer | Project Lead | ML- Always curious to create, build something through technology which provides value.