Hello everyone,
I’m seeking advice from developers for the following idea:
-
Basic concept: I plan to mount a cell phone on a gimbal on my quadcopter, and set up an AWS OpenVPN server as an intermediary server to enable the cell phone and laptop to be in the same network via LTE. This will allow the cell phone to receive UDP messages from mission planner and forward them to the USB, as well as receive USB messages and forward them to the UDP network for mission planner.
in essence, the cell phone will function similarly to an “ESP8622 WIFI telemetry” -
Purpose: This setup is intended for maritime use of multicopters, where internet connectivity is limited. By utilizing the cell phone’s modem capabilities, I hope to improve the connectivity and functionality of the quadcopter in the sea.
I have already made mission planner plugins that display the ship’s position and data with an AIS antenna and transceiver. my dream is connecting all the maritime and drone datas in the LTE and mission planner environment. -
What I have done :
I’m very beginner in network and udp things, so I maybe have some mistakes or misconception here.
however, I already setup my aws server and network.
I have also made FTDI - USB C connection between Pixhawk cube and Galaxy android phone.
the only remaining task is to create android code for communicating. I have written the code without errors, but it does nothing. when I push “connect” button in mission planner, no heartbeat packet is received.
However, the usb data is being written(as indicated by the blinking of FTDI module TX led) and I have also confirmed that UDP data is being send and received. (I checked network packet through the “wire shark” program) -
mission planner environment :
connect option : udpcl, ip 10.8.0.10, port 1194
5.Android Code :
package com.example.cellphonemissioncomputerbegin6;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;
import static android.content.ContentValues.TAG;
public class MainActivity extends AppCompatActivity {
private static final String ACTION_USB_PERMISSION = "com.example.cellphonemissioncomputerbegin2.ACTION_USB_PERMISSION";
private static final String LAPTOP_IP = "10.8.0.6";
private static final int LAPTOP_PORT = 1194;
private static final int TIMEOUT = 1000; //TIMEOUT FOR USB
private UsbManager mUsbManager = null;
private UsbDevice mUsbDevice = null;
private UsbInterface mUsbInterface = null;
private UsbEndpoint mUsbEndpointIn = null;
private UsbEndpoint mUsbEndpointOut = null;
private UsbDeviceConnection mUsbConnection = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView telemetryTextView = findViewById(R.id.textView);
new Thread(() -> {
DatagramSocket socket = null;
while(true)
{
try{
socket = new DatagramSocket(1194);
break;
}
catch(Exception e)
{
Log.e(TAG,"Failed to create socket object");
}
}
while(true)
{
try {
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mUsbDevice = mUsbManager.getDeviceList().values().iterator().next();
}catch(Exception e)
{
Log.e(TAG,"Failed to create USB object");
continue;
}
if (!mUsbManager.hasPermission(mUsbDevice)) {
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE);
mUsbManager.requestPermission(mUsbDevice, pi);
Log.e(TAG, "USB Permission Requesting..");
} else {
Log.d(TAG,"USB permission already granted");
}
while(!mUsbManager.hasPermission(mUsbDevice)) {
Log.e(TAG, "USB Permission has not been granted");
}
try{
mUsbConnection = mUsbManager.openDevice(mUsbDevice);
mUsbInterface = mUsbDevice.getInterface(0);
mUsbEndpointIn = mUsbInterface.getEndpoint(0);
}
catch(Exception e)
{
if(mUsbConnection != null)
{
mUsbConnection.close();
if(mUsbInterface != null)
{
mUsbConnection.releaseInterface(mUsbInterface);
}
}
Log.e(TAG,"Failed to open USB connection");
continue;
}
//try-with-resources statement for automatic release of object
try{//Bind the socket to port 1194
InetAddress serverAddress = InetAddress.getByName(LAPTOP_IP);
byte[] buffer_8 = new byte[8];
byte[] buffer_usb_from_pixhawk = new byte[mUsbEndpointIn.getMaxPacketSize()];
int numBytesRead = 0;
// get data from USB
numBytesRead = mUsbConnection.bulkTransfer(mUsbEndpointIn, buffer_usb_from_pixhawk, buffer_usb_from_pixhawk.length, TIMEOUT);
// if there is data on USB
if (numBytesRead > 0) {
byte[] mUsbData = Arrays.copyOf(buffer_usb_from_pixhawk, numBytesRead);
// Send the USB data over the network
DatagramPacket packet = new DatagramPacket(mUsbData, mUsbData.length, serverAddress, LAPTOP_PORT);
socket.send(packet);
Log.d(TAG, "byte data sent");
System.out.println("usb data : " + mUsbData);
}
if (mUsbConnection != null) {
mUsbConnection.close();
if (mUsbInterface != null) {
mUsbConnection.releaseInterface(mUsbInterface);
}
}
try{
mUsbConnection = mUsbManager.openDevice(mUsbDevice);
mUsbInterface = mUsbDevice.getInterface(0);
mUsbEndpointOut = mUsbInterface.getEndpoint(1);
}
catch(Exception e)
{
Log.e(TAG, "Exception in USB writing object get");
}
byte[] buffer_usb_to_pixhawk = new byte[mUsbEndpointOut.getMaxPacketSize()];
DatagramPacket packet = new DatagramPacket(buffer_usb_to_pixhawk, buffer_usb_to_pixhawk.length);
//DatagramPacket packet = new DatagramPacket(buffer_2, buffer_2.length,serverAddress, LAPTOP_PORT);
socket.receive(packet);
String packetContent = new String(packet.getData());
Log.d(TAG,"packet message has been received successfully");
Log.d(TAG, packetContent);
int numBytesWritten = mUsbConnection.bulkTransfer(mUsbEndpointOut, packet.getData(), packet.getLength(), 1000); //timeout : ms
if (numBytesWritten < 0) {
Log.e(TAG, "Error writing UDP data to USB: " + numBytesWritten);
} else {
Log.d(TAG, "UDP data written to USB: " + numBytesWritten);
}
if (mUsbConnection != null) {
mUsbConnection.close();
if (mUsbInterface != null) {
mUsbConnection.releaseInterface(mUsbInterface);
}
}
} catch (Exception e) {
if (mUsbConnection != null) {
mUsbConnection.close();
if (mUsbInterface != null) {
mUsbConnection.releaseInterface(mUsbInterface);
}
}
Log.e(TAG, "Error while executing main try statement at line " + e.getStackTrace()[0].getLineNumber() + ": " + e);
}
}//end of while(true)
}).start(); //end of new Thread()
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}