I want to have an ESP32 send MAVLink data over serial to a PC. I compiled and uploaded main.cpp onto an ESP32, which for now only sends heartbeat messages. I am using this code for MAVLink.h. main.cpp is as follows:
#include <Arduino.h>
#include <MAVLink.h>
void setup() {
Serial.begin(115200);
}
void send_mavlink(mavlink_message_t *mavlink_message) {
uint8_t mavlink_message_buffer[MAVLINK_MAX_PACKET_LEN];
uint16_t mavlink_message_length = mavlink_msg_to_send_buffer(mavlink_message_buffer, mavlink_message);
Serial.write(mavlink_message_buffer, mavlink_message_length);
}
void send_heartbeat() {
mavlink_message_t mvl_tx_message;
mavlink_msg_heartbeat_pack(1, MAV_COMP_ID_AUTOPILOT1, &mvl_tx_message, MAV_TYPE_QUADROTOR, MAV_AUTOPILOT_GENERIC, MAV_MODE_FLAG_MANUAL_INPUT_ENABLED, 0, MAV_STATE_STANDBY);
send_mavlink(&mvl_tx_message);
}
void loop() {
send_heartbeat();
Serial.print("Plain text \n");
delay(1000);
}
On my computer, I want to use Pymavlink to read and interpret messages. I have the following code:
from pymavlink import mavutil
import time
conn = mavutil.mavlink_connection(device='COM7', baud=115200, timeout=1)
print('Entering while loop')
while True:
msg = conn.recv_match(blocking=False)
if msg is None:
pass
elif msg.get_type() != 'BAD_DATA':
print(msg)
else:
print(msg)
time.sleep(0.001)
However, this does not work. Sometimes I will get no read characters, other times it will print ~8 lines similar to this:
BAD_DATA {Bad prefix, data:['73']}
and then stop printing.
If I try the Serial Monitor of the Arduino IDE, every second I see characters being printed and at the end of the line Plain text, as I would expect. What am I doing wrong that prevents Python from reading serial?