다음을 통해 공유


Azure Cloud Services for Tello Drones: How to send telemetry to Azure IoTHub

Introduction

Tello is a programmable mini drone, which is perfect and popular for beginners. Users can easily learn programming languages such as Scratch, Python, and Swift. Microsoft Azure provides a variety of cloud computing services including artificial intelligence, machine learning, IoT, storage, security, networking, media, integration and so on. For IoT applications, Azure IoT Hub provides a cloud-hosted solution back end to connect any device over the internet. 
In this article, we will walk you through the steps required to send the sensor data of the Tello to Azure IoT Hub on your computer with Python Azure IoT SDK.

Prerequisites

  1. Tello Drone.
  2. Python Azure IoT Device SDK
  3. IDE: PyCharm Community

Network Access for PC

Since Tello is connected to the PC by Wi-Fi, it is straightforward to know that our PC should equipped with two network interface cards, one is for connecting with Tello, and the other one is for connecting with Internet. 

Install Python Azure IoT SDK and Tello Python SDK

Create a new PyCharm project on your PC, click File-Settings-Project-Python Interpreter. On Package Tab, click “+” icon to add packages to the current project. Search and install “djitellopy” and “azure-iot-device” respectively as follows in Fig. 1.
 
Fig. 1 Install Python Azure IoT SDK and Tello Python SDK

Create an Azure IoTHub and Register a New Device

We can create an IoT hub using the Azure Portal. Please refer to the “Create an IoT hub” section on this page: Connect Raspberry Pi to Azure IoT Hub. Then, in order to send the data to Azure IoT Hub, we should register a new device in the IoT hub. The “Register a new device in the IoT hub” section is available to help you with this. After the device is created, open the device from the list in the Devices pane. Copy the Primary Connection String. This connection string will be used by Python code to communicate with the hub.

Create and Debug Python Code on Your PC

Copy and paste the following code to your PyCharm project.

01.from djitellopy import tello
02.from azure.iot.device.aio import IoTHubDeviceClient
03.from azure.iot.device import Message
04.import asyncio
05.import time
06.import uuid
07. 
08.me =  tello.Tello()
09.me.connect()
10. 
11.async def  send_tello_telemetry(device_client):
12.    # Connect the client.
13.    await device_client.connect()
14. 
15.    # Send tello telemetry
16.    while True:
17.        msg =  Message(str(me.get_current_state()))
18.        msg.message_id =  uuid.uuid4()
19.        msg.correlation_id =  "correlation-1234"
20.        msg.custom_properties["tornado-warning"] =  "yes"
21.        msg.content_encoding =  "utf-8"
22.        msg.content_type =  "application/json"
23.        print("sending message: " +  str(me.get_current_state()))
24.        await device_client.send_message(msg)
25.        time.sleep(5)
26. 
27. 
28.def main():
29.    # The connection string for a device.
30.    conn_str =  "your connection string"
31.    # The client object is used to interact with your Azure IoT hub.
32.    device_client =  IoTHubDeviceClient.create_from_connection_string(conn_str)
33. 
34.    print("IoTHub Device Client Sending Tello Telemetry")
35.    print("Press Ctrl+C to exit")
36.    loop =  asyncio.get_event_loop()
37.    try:
38.        loop.run_until_complete(send_tello_telemetry(device_client))
39.    except KeyboardInterrupt:
40.        print("User initiated exit")
41.    except Exception:
42.        print("Unexpected exception!")
43.        raise
44.    finally:
45.        loop.run_until_complete(device_client.shutdown())
46.        loop.close()
47. 
48. 
49.if __name__ == "__main__":
50.    main()

In this Python application, we define a function to receive the current status of the Tello, which includes current acceleration, speed, battery percentage, distance value from TOF, fly time, height, temperature, IMU attitude data and barometer value. Then send the information to Azure IoThub every 5 seconds. 
Please do substitute the connection string with yours that created in section “Create an Azure IoTHub and Register a New Device”. Then, power on the Tello, connect your PC with Tello by Wi-Fi. You will notice that the LED on the Tello will flash quickly with yellow color. Press Run or Debug button to start the process. You will see the output in output window as shown in Fig. 2.
 
Fig. 2 Realtime Debug Information

Monitor the Message with Azure IoT Explorer

Install and run Azure IoT Explorer, choose the device that you created. And then click telemetry. We will see the data as soon as the Python application send out the message. It is shown in Fig. 3.
 
Fig. 3 Telemetry monitor by Azure IoT Explorer

Resources

  1. MS Docs for Azure IoT Hub.
  2. MS Docs for Connect Raspberry Pi to Azure IoT Hub.
  3. Azure IoT Explorer: Install and use Azure IoT Explorer