How do I use MAVLink Endpoints to get gamepad data?

Passing by here to say that Cockpit v1.18.0-beta.5 now includes support for receiving generic data via WebSocket, which makes the whole process of sending and showing user-generated data in Cockpit, be it from the vehicle or another system, much easier.

I’ve added an example extension here so anyone can copy/fork from it to create its own. It’s as simple as running a WebSocket server and sending data through it, which can be accomplished in a dozen lines of Python, which can be seen in the example below:

import asyncio
import websockets

async def handler(websocket):
    while True:
        await websocket.send("variable_name=variable_value")
        await asyncio.sleep(1)

async def main():
    async with websockets.serve(handler, "0.0.0.0", 8765):
        await asyncio.Future()

if __name__ == "__main__":
    asyncio.run(main())

The most important part for someone spinning up its own extension is to put the Network Mode on Host mode, which is done with the following permission:

{
  "HostConfig": {
    "NetworkMode": "host"
  }
}

This allows the extension to bind the WebSocket server port and receive Cockpit connections.

One can send all types of primitive data, like strings, booleans and numbers. It’s as simple as sending the variable name and value separated by an equal sign, each par in its own message.

1 Like