No Heartbeat Message

Hello,

I’m trying to read heartbeat messages from my PX4 6X but unfortunately I can’t read any messages. I have connected the Telem2 Port to my Arduino
TX of the PX4 to the Arduino RX pin
GND - GND

This is my code:

#include <mavlink.h>

const int ledPin = 13; 

void setup() {
  Serial.begin(57600); 
  pinMode(ledPin, OUTPUT); 
}

void loop() {
  if (Serial.available() > 0) {
    uint8_t c = Serial.read();
    mavlink_message_t msg;
    mavlink_status_t status;

    if (mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status)) {
      // check if there is a Heartbeat 
      if (msg.msgid == MAVLINK_MSG_ID_HEARTBEAT) {
        mavlink_heartbeat_t heartbeat;
        mavlink_msg_heartbeat_decode(&msg, &heartbeat);

        // show informationen  Heartbeat-Stream anzeigen
        Serial.print("System type: ");
        Serial.println(heartbeat.type);
        Serial.print("Autopilot type: ");
        Serial.println(heartbeat.autopilot);
        Serial.print("Base mode: ");
        Serial.println(heartbeat.base_mode);
        

      
        digitalWrite(ledPin, HIGH);
        delay(1000); 
        digitalWrite(ledPin, LOW);
      }
    }
    
    }
     // if there is no message print "no message" and blink LED 
     else {      
     Serial.print("No Heartbeat\n");
        digitalWrite(ledPin, LOW);
        delay(500);
        digitalWrite(ledPin, HIGH); 
}
  }
 

Does anyone have an idea why it is not working?

Hello!

You need to loop through all available chars and feed them to mavlink_parse_char in a loop, one char at a time.

Right now you are only feeding one char to the parser whenever there is serial data available and then your waiting with delay slowing everything down.

1 Like

Thanks for the helpful advice, I will change the code and try it.

I modified the code and replaced the if-else statement with a while loop but still I can’t receive the heartbeat.
Also I’m not sure about the baudrate, I tried 57600 and 115200 but both with the same result (115200 is the baudrate in my groundcontrol software).
Any suggestions what I can else try?

#include <mavlink.h>

const int ledPin = 13; 

void setup() {
  Serial.begin(115200); 
  pinMode(ledPin, OUTPUT); 
}

void loop() {
  while (Serial.available() > 0) {
    uint8_t c = Serial.read();
    mavlink_message_t msg;
    mavlink_status_t status;

    if (mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status)) {
      // Check if there is a Heartbeat
      if (msg.msgid == MAVLINK_MSG_ID_HEARTBEAT) {
        mavlink_heartbeat_t heartbeat;
        mavlink_msg_heartbeat_decode(&msg, &heartbeat);

        // Show information from Heartbeat message
        Serial.print("System type: ");
        Serial.println(heartbeat.type);
        Serial.print("Autopilot type: ");
        Serial.println(heartbeat.autopilot);
        Serial.print("Base mode: ");
        Serial.println(heartbeat.base_mode);

        //digitalWrite(ledPin, HIGH);
        //delay(1000);
        //digitalWrite(ledPin, LOW);
      }
    }
  }

  // If there is no message, print "No Heartbeat" and blink LED
  if (!Serial.available()) {
    Serial.print("No Heartbeat\n");
    //digitalWrite(ledPin, LOW);
    //delay(500);
    //digitalWrite(ledPin, HIGH);
  }
}

Ok, make sure both ends are using mavlink v2 or v1 and not a mix. Also, as a try, you can print the incoming bytes to check if they make sense. You’re looking for magic bytes (in hex 0xFD or 0xFE). Also see: Serialization · MAVLink Developer Guide

1 Like