Monitoring Drones Over Cellular Network — Part2
By: Sriramreddy Mandhati
Tags: Raspberry Pi, Drones, Telemetry, Mapping

6. Setting Up AWS Server

First, you need to open an AWS account. Even though this is a commercial service, don’t worry there is a free tier.

Log in to AWS and go to EC2 under Compute service. Here we can create a new instance or virtual server.

2. Go to instances → Select your region → Click launch instance to create a virtual server

3. Select ubuntu server 18.04.LTS. The server you are going to create will run Ubuntu Server 18.04 LTS

4. Select General-purpose instance and click review and launch. (We don’t need a high-performance server. We just need to act the server as a relay)

5. Click launch

6. Select Create a new key pair and provide a key pair name. In our case, we named it aws_key.

7. Click Download Key Pair and save it in a convenient location. This key is necessary to access the AWS server.

8. Click Launch Instances. Your virtual server is now ready to use.

IMPORTANT: If you are working on a windows computer, it is required to change the access permission of the AWS key file. (Omit the step 9 if you are on Linux)

9.1. Open PowerShell and go to the location you save the parameter file.

9.2. Execute the following commands to modify the file permissions of the aws_key.pem file. Reference

$path = ".\aws_key.pem"

# Reset to remove existing permissions

icacls.exe $path /reset

# Give current user explicit read-permission

icacls.exe $path /GRANT:R "$($env:USERNAME):(R)"

# Disable inheritance and remove inherited permissions

icacls.exe $path /inheritance:r

10. Click View Instances

It will take a few minutes to initialize your virtual server. It should look as follows when AWS successfully initiated your virtual server.

11. Select your instance and click connect. It will provide information on how you can connect to your instance using SSH. In our case, it's as below.

12. Copy the command in the example section (highlighted in the above figure)

13. Open terminal/command prompt from the location you saved your aws_key.pem file and run the command to access the server. A successful connection will look like this

Note: SSH has to be enabled

Tip: How to install openSSH in windows

7. Forwarding Telemetry Data and Video Stream to AWS server

7.1 Assigning local ports to mavproxy and mjpeg streamer:

Turn on the UAV (ACS and flight controller) and make sure the internet connection is enabled through the cellular network. Now assign preferred local ports and start telemetry broadcasting and video streaming. The following are the commands to make it.

Note: This have to be done in your ACS (i.e. Raspberry Pi connected to the drone)

MavProxy

Open a new terminal: Run the following command: this will forward the mavlink data to the local port 14450

Tip: you can change MyCopter to the name of your drone

$ mavproxy.py — master=/dev/ttyAMA0 — baudrate 921600 — out=tcpin:127.0.0.1:14550 — aircraft MyCopter

MJPEG-streamer

2. Open a new terminal and run the following command: This will start the video server on the local port 8080

$ export LD_LIBRARY_PATH=/home/pi/mjpg-streamer/mjpg-streamer-experimental/ $ /home/pi/mjpg-streamer/mjpg-streamer-experimental/mjpg_streamer -o “output_http.so -w /home/pi/mjpg-streamer/mjpg-streamer-experimental/www” -i “input_raspicam.so -fps 15”

7.2 Remote forward the local ports of the RPi to the AWS server to send telemetry and video data:

Note: This have to be done in your ACS (i.e. Raspberry Pi connected to the drone)

SSH tunneling will be used to forward the ports from ACS to AWS server.

Copy the aws_key.pem (your AWS key) file to your RPi home location.

2. Copy and note your AWS server address.

3. Create ssh configuration file

$ sudo nano ~/.ssh/config

4. Edit the file as follows, save and exit,

Host alias_for_your_host

HostName your_aws_server_address

User ubuntu

IdentityFile path_to_the_pem_key

RemoteForward 14455 localhost:14455

RemoteForward 8080 localhost:8080

*Change the following with your details Your_host_name, Your_aws_server_address, and Path_to_the_pem_key.

5. Run the following command to forward the data to the AWS server (SSH remote port forwarding)

$ ssh alias_for_your_host

Now the telemetry data is forwarded to the AWS server.

7.3 Retrieving the Telemetry and Video Data from AWS Server to GCS

Note: This have to be done in your GCS (i.e. Ground Control Station)

It is assumed that a windows computer is used as the ground control station. If you are using a Linux based system, it will be much easier for you. Make sure your GCS is connected to the internet.

Copy the AWS .pem key (aws_key.pem) to a convenient location. Hereafter its mentioned as aws_key_location

2. Go to the folder C:\Users\your_user_name\.ssh

3. Create a new file and name config (make sure there is no extension)

4. Open the file using a text editor and enter add the following lines, and save it

Host your_AWS_host_alies

HostName your_aws_host_address

User ubuntu

IdentityFile “aws_key_location”

LocalForward 14455 localhost:14455

LocalForward 8080 localhost:8080

*note: you have to change the your_AWS_host_alies, your_aws_host_address (AWS server address), and aws_key_location

Example:

5. Now open the command prompt and run the following command

$ ssh your_AWS_host_alies

All set. Now you can access the video stream using your Ground control stations port 8080 and telemetry data using local port 14455

6. In order to check everything works, open your browser, and enter 127.0.0.1:8080. You should be able to see the video stream from your drone

7. In the mission planner, select TCP in the connection option and click connect. Enter address 127.0.0.1 and port 14455. And click ok.

8. You have successfully now transmitted the telemetry data of your drone to your ground control station via the internet.

8. Putting it all together

It’s not practical to connect to RPi everytime in the field and run the commands there. We have to put it together to automate the process of starting the telemetry and video stream.

The automation is done by either using .bashrc file or any other auto executed script after booting the system. What this does is it runs all the commands in the .bashrc file or other script, whenever the system booted.

Note: The following steps are be done in your ACS (i.e. Raspberry Pi connected to the drone)

First, we need to install a package named screen

$ sudo apt-get install screen

2. Create a shell script named ssh_connect.sh to establish SSH connection from RPi to AWS

$ cd ~

$ sudo nano ssh_connect.sh

3. Inside the file add the following code

#!/bin/sh

while ! ssh -o ServerAliveCountMax=2 -o ServerAliveInterval=10 alias_for_your_host ; do

Sleep 2;

done;

* note: you have to change alias_for_your_host to the alias you used in configuring SSH in your RPi (Step)

4. Save and exit

5. Run the following command to give executable permission.

$ sudo chmod +x ssh_connect.sh

6. Form your home folder open .bashrc

$ cd ~

$ sudo nano .bashrc

7. Add the following code to the end of the file

export LD_LIBRARY_PATH=/home/pi/mjpg-streamer/mjpg-streamer-experimental/

#start ssh

sudo service ssh start

#run the mavproxy in background

[[ $TERM != "screen" ]] && exec screen -S telemetry -dm mavproxy.py — master=/dev/ttyAMA0 — baudrate 921600 — out=tcpin:127.0.0.1:14550 — aircraft MyCopter &

#run video server

[[ $TERM != "screen" ]] && exec screen -S camera -dm /home/pi/mjpg-streamer/mjpg-streamer-experimental/mjpg_streamer -o “output_http.so -w /home/pi/mjpg-streamer/mjpg-streamer-experimental/www” -i “input_raspicam.so -fps 15” &

[[ $TERM != "screen" ]] && exec screen -S tunnel -dm ssh -dm ./ssh_connect.sh &

In the AWS server

Note: The following steps are be done in your AWS vertual server

Remote login to the AWS server

2. Open ssh configuration file

$ sudo nano /etc/ssh/sshd_config

3. Uncomment and edit the following parameters in the sshd_config file, Save and Exit.

ClientAliveInterval 9

ClientAliveCountMax 2

That’s ALL!

All you have to do is turn on the drone (which will power both RPi and flight controller), it will automatically connect to the internet and transmit telemetry and video data.

In the GCS,

follow the steps from 7.3.5 to retrieve the telemetry data and video stream.

Happy Flying.