MAVLink-ESP32 connection

Hello, I am trying to send data from an ESP32 to an Orange Cube+ with ArduPilot. To simulate a sensor connected to the ESP32, I am using a potentiometer and the c_library_v2 library with the following code:

#include <Arduino.h>
#include <stdint.h>

extern "C" {
  #include "mavlink/common/mavlink.h"
}


// UART2 del ESP32 hacia Cube Orange
HardwareSerial CubeSerial(2);

// IDs MAVLink
static uint8_t system_id    = 42;   // ESP32
static uint8_t component_id = 200;  // Sensor externo

// Pin ADC
const int POT_PIN = 34;

// Parámetros ADC ESP32
const float ADC_MAX = 4095.0;
const float VREF    = 3.3;

void setup() {

  // UART hacia Cube Orange
  CubeSerial.begin(115200, SERIAL_8N1, 16, 17);

  // Configurar ADC
  analogReadResolution(12); // 0–4095
  analogSetAttenuation(ADC_11db); // rango ~0–3.3V

  delay(1000); // tiempo para que el Cube arranque
}

void loop() {

  // Leer potenciómetro
  uint16_t adc_raw = analogRead(POT_PIN);

  // Convertir a voltaje
  float voltage = (adc_raw / ADC_MAX) * VREF;

  // Preparar MAVLink
  mavlink_message_t msg;
  uint8_t buffer[MAVLINK_MAX_PACKET_LEN];

  mavlink_msg_named_value_float_pack(
    system_id,
    component_id,
    &msg,
    millis(),
    "POT_VOLT",
    voltage
  );

  uint16_t len = mavlink_msg_to_send_buffer(buffer, &msg);
  CubeSerial.write(buffer, len);

  delay(200); // 5 Hz (ideal para sensores)
}

SERIAL2_PROTOCOL = 2
SERIAL2_BAUD = 115
BRD_SER2_RTSCTS = 0
LOG_DISARMED = 1
LOG_BACKEND_TYPE = 1

I am using those parameters in mission planner, but I don’t get any communication, can you help me please?

I connected tx rx and gnd between esp32 an cube orange+

Which pin number of ESP you connected to which pin_number on cube?
Did you checked what the ESP sends?
Do you have the possibilty to connect the ESP via a TTL UART 2 USB Converter directly to our PC?

Hi thanks for answering, according to my code, I have been connecting pin 16 to tx in serial 4 and pin 17 to rx and common GND. We should see something like POT_VOLT: 1.87. Regarding your last question, I need the connection between the orange cube and the esp without any intermediary PC.

My idea was first to test the ESP independent from your FC just to see if the output is correct on the corret pins.

well the ESP32 is conected to the serial COM of my PC and I can see the value of the analog lecture

Is the ESP32 connected via the onboard USB port to your PC or via the Serial2. To check if it is working correct on Serial2 you have to monitor this directly with your PC.

ESP32 IS connected in this moment to the USB port of mi PC and ESP32 is connected to telem2/serial2 of the cube orange

As I understood your problem is that you don’t know if the ESP is sending the correct information to the FC. So you have to look directly to this interface. Also I think you don’t have a complete electronic lab with a storage oszilloscope or a protocoll analyzer. So a simple way is to test the two components FC and ESP seperate with the use of the PC. But therefore a TTL UART to USB converter is necessary.

Hi, I dont need communication of my ESP32 with my computer, with the computer I am programming with arduino IDE I will send you a draw with the actual connections

I know what you finally want, but are you 100% sure that the ESP is sending the correct information on Serial2 Tx pin?

Well right now thats hard to know, if you see my code and there is not any problem there we can think in a physical problem in the signal.

#include <Arduino.h>
#include <stdint.h>

extern “C” {
#include “mavlink/common/mavlink.h”
}

// UART2 del ESP32 hacia Cube Orange
HardwareSerial CubeSerial(2);

// IDs MAVLink
static uint8_t system_id    = 42;   // ESP32
static uint8_t component_id = 200;  // Sensor externo

// Pin ADC
const int POT_PIN = 34;

// Parámetros ADC ESP32
const float ADC_MAX = 4095.0;
const float VREF    = 3.3;

void setup() {
Serial.begin(115200); // ADD THIS
// UART hacia Cube Orange
CubeSerial.begin(115200, SERIAL_8N1, 16, 17);

// Configurar ADC
analogReadResolution(12); // 0–4095
analogSetAttenuation(ADC_11db); // rango ~0–3.3V

delay(1000); // tiempo para que el Cube arranque
}

void loop() {
while (CubeSerial.available() > 0) Serial.write(CubeSerial.write); // ADD THIS
// Leer potenciómetro
uint16_t adc_raw = analogRead(POT_PIN);

// Convertir a voltaje
float voltage = (adc_raw / ADC_MAX) * VREF;

// Preparar MAVLink
mavlink_message_t msg;
uint8_t buffer[MAVLINK_MAX_PACKET_LEN];

mavlink_msg_named_value_float_pack(
system_id,
component_id,
&msg,
millis(),
“POT_VOLT”,
voltage
);

uint16_t len = mavlink_msg_to_send_buffer(buffer, &msg);
CubeSerial.write(buffer, len);

delay(200); // 5 Hz (ideal para sensores)
}


Let’s check one by one. Replace your code with this code and compile again. Then check the serial monitor with 115200 baudrate to see if any data is being received from the FC.

if “Nothing” then something is wrong with phisycal connection cable

If “YES,” then

// MAVLink IDs
static uint8_t system_id = 42; // ESP32
static uint8_t component_id = 200; // External Sensor

You wrote this ID, but you didn’t attach your parameter ID here. What does SYSID_THISMAV contain? make sure that values is the same with 42

if “ALL” seems okay then please change GPIO to other numbers because some Esp32 have a different GPIO purpose and i’m afraid you use GPIO which is not suppose tobe use

and just set
HardwareSerial CubeSerial(2);
to
HardwareSerial CubeSerial(1);

good luck

Hello, thank you very much for your reply. Unfortunately, after following your advice, I still cannot see anything in the messages section of Mission Planner. I would also like to point out that the code does not have the option to display data in the Arduino IDE serial port, but in previous tests I was able to see the analog reading from the Arduino IDE serial port.

i mean Serial Monitor from arduino not on MP

like this

if you paste the source from my (above) you must see the data flow on that monitor, if not as i mention above then something is wrong with the phisycal connections

Hi thank you for the clarification, it seems that there a flow of data, but nothing in mission planner

Oke good! at least we know both are communicated as expected

so you are not answer my next question on my first post above

the SYSID_THISMAV has the same value in the full parameter list, I changed ESP32 pins and I changed to HardwareSerial CubeSerial(1);

Oh don’t change anything as we are now know both are communicated

alright next please compile again this source

#include <Arduino.h>
#include <stdint.h>

extern “C” {
#include “mavlink/common/mavlink.h”
}

// UART2 del ESP32 hacia Cube Orange
HardwareSerial CubeSerial(2);

// IDs MAVLink
static uint8_t system_id    = 42;   // ESP32
static uint8_t component_id = 200;  // Sensor externo

// Pin ADC
const int POT_PIN = 34;

// Parámetros ADC ESP32
const float ADC_MAX = 4095.0;
const float VREF    = 3.3;

void setup() {
Serial.begin(115200); // ADD THIS
// UART hacia Cube Orange
CubeSerial.begin(115200, SERIAL_8N1, 16, 17);

// Configurar ADC
analogReadResolution(12); // 0–4095
analogSetAttenuation(ADC_11db); // rango ~0–3.3V

delay(1000); // tiempo para que el Cube arranque
}

void loop() {
// Leer potenciómetro
uint16_t adc_raw = analogRead(POT_PIN);

// Convertir a voltaje
float voltage = (adc_raw / ADC_MAX) * VREF;

// Preparar MAVLink
mavlink_message_t msg;
uint8_t buffer[MAVLINK_MAX_PACKET_LEN];

// ADD THIS
Serial.println(voltage);

mavlink_msg_named_value_float_pack(
system_id,
component_id,
&msg,
millis(),
“POT_VOLT”,
voltage
);

uint16_t len = mavlink_msg_to_send_buffer(buffer, &msg);
CubeSerial.write(buffer, len);

delay(200); // 5 Hz (ideal para sensores)
}

and check again on Serial Monitor (while you turn your potensiometer randomly) and please paste that Serial Monitor data in here

There you have it, still nothing in ms

alright great! so there is nothing is wrong with anything so far

so my last shot is

change SYSID_THISMAV to “1”

and also change

// IDs MAVLink
static uint8_t system_id = 42; // ESP32
to also “1” and compile

SAVE and RESTART your Cube!!

you should have what you want! but if still doesn’t then i’m sorry my knowledge are limited about this limit :sleepy_face: