How to send messages from an ESP32 to a PIXHAWK4 using mavling

Hello, I’m new user trying to learn about all this topics and I’m using a PixHawk4 and an ESP32 this is connected to the telem2 port and I’m triying to send a heartbeat and watching it in MavProxy but I can’t watch any message I tried with a heartbeat, with a distance sensor and statustext messages but I can’t watch them

this is my code:

#include <Arduino.h>
#include <MAVLink.h>
#include <ESP32Servo.h>
#include <NewPing.h>

#define RX2_PIN 16 // Cambia este valor según tu configuración
#define TX2_PIN 17 // Cambia este valor según tu configuración

HardwareSerial SerialMAV(1); // Usar UART2

void MessageToPixHawk(const char* message);
void sendHeartbeat(void);

float distance = 0;

void setup() {


  SerialMAV.begin(57600, SERIAL_8N1, RX2, TX2); // Configurar puerto serial para MAVLink

  Serial.println("ESP32 inicializado correctamente.");


void requestDataStream(uint8_t target_system, uint8_t target_component, uint8_t stream_id, uint16_t frequency, uint8_t start_stop) {
  mavlink_message_t msg;
  uint8_t buf[MAVLINK_MAX_PACKET_LEN];
  mavlink_msg_request_data_stream_pack(1, 200, &msg, target_system, target_component, stream_id, frequency, start_stop);
  uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
  SerialMAV.write(buf, len);

void loop() {

  static uint32_t lastRequest = 0;
  uint32_t now = millis();

  // Enviar mensaje cada 5 segundos
  if (now - lastRequest > 5000) {
    lastRequest = now;


  delay(100); // Evitar el "watchdog timer" reset


void MessageToPixHawk(const char* message){

  mavlink_message_t msg;
  uint8_t buffer[ MAVLINK_MAX_PACKET_LEN ];
  uint16_t len;

  mavlink_msg_statustext_pack( 2, 200, &msg, MAV_SEVERITY_INFO, message, 0, 0 );
  len = mavlink_msg_to_send_buffer( buffer, &msg );

  SerialMAV.write( buffer, len );

  // Depuración
  Serial.print("Enviando mensaje a PixHawk: ");


void sendHeartbeat(void){

  mavlink_message_t msg;
  uint8_t buffer[MAVLINK_MAX_PACKET_LEN];
  uint16_t len;

  len = mavlink_msg_to_send_buffer(buffer, &msg);

  SerialMAV.write(buffer, len);


can somebody help me please??

First check if the heartbeat arrives. In PX4, when you enter mavlink status in the MAVLink shell, do you see that messages are arriving?

I’m trying to see the heartbeat that I’m sending in mavproxy but I can’t see it, just appear other heartbeats from I don’t know where