Skip to main content

MAVLink ADSB_VEHICLE

What this example shows​

Switching the receiver from CSV to MAVLink output and decoding the ADSB_VEHICLE message it sends for every tracked aircraft. The sketch also registers a generic frame callback that prints the raw payload bytes of every MAVLink message the receiver sends. CSV is the receiver's default output according to its datasheet, and the receiver on this board reports AT+ADSB_RX_PROTOCOL_DECODED=CSV with firmware 2.89.28.0, the version the datasheet is written for.

The example code​

This is 6_MavlinkAdsbVehicle.ino from the pinned library, without the file's header comment. The sketch body is exactly as shipped.

ESP32S3 Dev Module
6_MavlinkAdsbVehicle.ino
#include "ADSB-Receiver-SOLDERED.h"

// Create an instance of the ADS-B object
ADSBReceiver adsb;

// Called once per decoded ADSB_VEHICLE message, one per tracked aircraft.
void onAdsbVehicle(const MavlinkAdsbVehicle &v)
{
Serial.print("ICAO ");
Serial.print(v.icaoAddress, HEX);

if (v.hasValidCallsign())
{
Serial.print(" call=");
Serial.print(v.callsign);
}
if (v.hasValidCoords())
{
Serial.print(" lat=");
Serial.print(v.latDegrees(), 5);
Serial.print(" lon=");
Serial.print(v.lonDegrees(), 5);
}
if (v.hasValidAltitude())
{
Serial.print(" alt=");
Serial.print(v.altitudeMeters());
Serial.print("m");
}
if (v.hasValidVelocity())
{
Serial.print(" vel=");
Serial.print(v.horVelocity / 100.0);
Serial.print("m/s");
}
if (v.hasValidSquawk())
{
Serial.print(" squawk=");
Serial.print(v.squawk);
}
Serial.print(" tslc=");
Serial.print(v.tslc);
Serial.println("s");
}

// Called for every MAVLink frame received, whatever its message id.
void onFrame(const MavlinkFrame &f)
{
Serial.print("[mavlink] msgid=");
Serial.print(f.msgid);
Serial.print(" sysid=");
Serial.print(f.sysid);
Serial.print(" crcValid=");
Serial.print(f.crcValid ? "yes" : "no");
Serial.print(" payload=");
for (uint8_t i = 0; i < f.payloadLen; i++)
{
if (f.payload[i] < 0x10)
Serial.print('0');
Serial.print(f.payload[i], HEX);
}
Serial.println();
}

void setup()
{
Serial.begin(115200);
adsb.begin(115200);

// Register the callbacks that will receive the decoded and raw MAVLink data.
adsb.onMavlinkAdsbVehicle(onAdsbVehicle);
adsb.onMavlinkFrame(onFrame);

// The module defaults to CSV output, so switch it to MAVLink here.
if (adsb.enterConfiguration())
{
adsb.setAdsbRxProtocolDecoded(DecodedProtocol::MAVLINK);
adsb.exitConfiguration(115200);
}
}

void loop()
{
adsb.poll();
}

Expected result​

Open Tools → Serial Monitor, set the baud rate to 115200 and reset the board. Two kinds of line appear. An ICAO line comes from the decoded ADSB_VEHICLE message: the aircraft's address in hex, then the callsign, position, altitude in metres, speed in metres per second and squawk for whichever of those the message marks as valid, and finally tslc, the seconds since the last contact with that aircraft. A [mavlink] line comes from every frame the receiver sends, with its message id, system id, whether the checksum was valid and the payload as hex bytes. The library notes that the receiver also sends a HEARTBEAT message (id 0) once a second alongside the aircraft data, so expect [mavlink] msgid=0 lines too.

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
[mavlink] msgid=0 sysid=1 crcValid=yes payload=000000001B08000403
[mavlink] msgid=246 sysid=1 crcValid=yes payload=8C1F4700E36E371B5B160C0B2015AC00983A075A0000BF01B50A00575A5A33353237000003
ICAO 471F8C call=WZZ3527 lat=45.66177 lon=18.53415 alt=11277.60m vel=230.47m/s squawk=2741 tslc=0s
[mavlink] msgid=246 sysid=1 crcValid=yes payload=D2A94C00DB336F1BDB69AF0A6062B5000032DA5E0000BF012A1400525952364C4600000003
ICAO 4CA9D2 call=RYR6LF lat=46.02726 lon=17.92681 alt=11887.20m vel=242.82m/s squawk=5162 tslc=0s
[mavlink] msgid=246 sysid=1 crcValid=yes payload=67AA4B006FD40B1B81CE790BC0BBB0002477AA610000BF01631800544859314A4B00000003
ICAO 4BAA67 call=THY1JK lat=45.37601 lon=19.25321 alt=11582.40m vel=250.02m/s squawk=6243 tslc=0s
[mavlink] msgid=246 sysid=1 crcValid=yes payload=2D0C4400634A081BDEB4720B806EA7001437A45C0000BF011F100041554136373500000003
ICAO 440C2D call=AUA675 lat=45.35282 lon=19.20668 alt=10972.80m vel=237.16m/s squawk=4127 tslc=0s
[mavlink] msgid=246 sysid=1 crcValid=yes payload=15024C00E5ADC11A9283B20B3C816F006C39944F6EFCBF0105120041534C3131330000000301
ICAO 4C0215 call=ASL113 lat=44.89006 lon=19.62485 alt=7307.58m vel=203.72m/s squawk=4613 tslc=1s
[mavlink] msgid=0 sysid=1 crcValid=yes payload=000000001B08000403

Functions used​

ADSBReceiver()returns None

Creates the receiver object. With no arguments it uses the library's board defaults: hardware UART 1, IO17 for the receiver's TX line, IO18 for its RX line and IO21 for its RESET line, which is how this board is wired.

ReturnsConstructor; no return value.

Parameters

TypeNameDescription
uint8_tuartNumESP32 hardware UART to use. Optional, default 1. Avoid 0, which is usually the USB console.
int8_trxPinESP32 GPIO wired to the module's TX pin. Optional, default 17.
int8_ttxPinESP32 GPIO wired to the module's RX pin. Optional, default 18.
int8_tresetPinESP32 GPIO wired to the module's RESET pin. Optional, default 21. Pass -1 if it is not connected.
begin(uint32_t initialBaud, size_t rxBufferSize)returns void

Opens the UART link to the receiver. Call it once in setup().

ReturnsNothing.

Parameters

TypeNameDescription
uint32_tinitialBaudBaud rate to start at. Optional, default 115200.
size_trxBufferSizeSize of the UART receive buffer in bytes. Optional, default 4096.
onMavlinkAdsbVehicle(MavlinkAdsbVehicleCallback cb)returns void

Registers the function that is called for every ADSB_VEHICLE frame (message id 246) that passes the checksum test.

ReturnsNothing.

Parameters

TypeNameDescription
void (*)(const MavlinkAdsbVehicle &vehicle)cbThe function to call. See the table below the functions for the fields and helper functions of MavlinkAdsbVehicle.
onMavlinkFrame(MavlinkFrameCallback cb)returns void

Registers the function that is called for every complete MAVLink frame, whatever its message id. The frame's crcValid is only true for message ids the library can check (HEARTBEAT, id 0, and ADSB_VEHICLE, id 246); for any other message it stays false even when the frame is fine.

ReturnsNothing.

Parameters

TypeNameDescription
void (*)(const MavlinkFrame &frame)cbThe function to call. The frame carries msgid, sysid, compid, seq, payload, payloadLen and crcValid.
enterConfiguration()returns bool

Moves the receiver from RUN to CONFIGURATION state. It sends AT+CONFIG=1, switches the local UART to 115200 and confirms the new state with AT+CONFIG?.

Returnstrue if the receiver reports being in CONFIGURATION state, false otherwise.

setAdsbRxProtocolDecoded(DecodedProtocol p)returns bool

Chooses the format of the decoded aircraft data. The example selects MAVLink; the library sends the name Mavlink to the receiver.

Returnstrue if the receiver answered AT+OK, false otherwise.

Parameters

TypeNameDescription
DecodedProtocolpDecodedProtocol::NONE, DecodedProtocol::CSV or DecodedProtocol::MAVLINK.
exitConfiguration(uint32_t runBaud)returns bool

Moves the receiver from CONFIGURATION back to RUN state. It sends AT+CONFIG=0, switches the local UART to runBaud and confirms the new state with AT+CONFIG?.

Returnstrue if the receiver reports being in RUN state, false otherwise.

Parameters

TypeNameDescription
uint32_trunBaudBaud rate for RUN state. Optional, default 115200. It must match the receiver's stored BAUDRATE setting.
poll()returns void

Reads every byte that has arrived and fires the registered callbacks. It tells CSV lines and MAVLink frames apart byte by byte, so the same call serves both formats. It does not block, so call it on every pass of loop().

ReturnsNothing.

What the callback receives​

The MavlinkAdsbVehicle object holds the raw MAVLink units, and its helper functions convert them and tell you which fields the receiver filled in.

Field or functionMeaning
icaoAddressThe aircraft's 24-bit ICAO address. The example prints it in hex.
hasValidCallsign() and callsignWhether the message carries a callsign, and the callsign text.
hasValidCoords(), latDegrees() and lonDegrees()Whether the position is valid, and latitude and longitude in degrees. The raw lat and lon are degrees times 10^7.
hasValidAltitude() and altitudeMeters()Whether the altitude is valid, and the altitude in metres. The raw altitude is in millimetres.
hasValidVelocity() and horVelocityWhether the speed is valid, and the horizontal speed in cm/s. The example divides by 100 to print m/s.
hasValidSquawk() and squawkWhether the message carries a squawk code, and the code.
tslcSeconds since the last contact with this aircraft.

Putting it together​

Register both callbacks first, then switch the receiver to MAVLink output while it is in CONFIGURATION state and keep calling poll(). The decoded callback gives you tidy aircraft data, and the frame callback is the place to look when you want to see everything the receiver sends.

The sketch changes the receiver's output protocol. To get CSV output back for the Read aircraft reports sketch, run the sketch on the Configure protocols and settings page, which selects CSV.