Converting ESP8266 project to ESP32 project

Hello,

I'm working on a project to control rovers via webapp. I was working with esp8266 until recently I decided to switch to esp32. I used the arduino ide to flash some code, it does get uploaded. But when I plug it into a pixhawk 2.4.8, nothing seems to work. So far I flashed on a set_actuator code that worked fine on the esp8266. I did make changes to make it work on the 32 but doesnt seem to work. Find the code below. What am I missing here?

(ps: my tx and rx pins are inserted in pin 36,37 respectively)

Edit: please ignore all serial.println() operations. I had used a separate serial earlier but got no response, so I tried to make it into a single serial.

#include <MAVLink.h>

void setup() {
    Serial.begin(921600);
    Serial1.begin(921600, SERIAL_8N1, 36, 37); // Initialize Serial2 with RX on pin 36 and TX on pin 37
}

void loop() {
    setOffboardMode();
    delay(3000);
    disableRCChecks();
    delay(3000);
    disablePreflightChecks();
    delay(3000);
    disableCommanderChecks();
    delay(3000);
    armPixhawk();
    delay(5000);

    setActuator();
    delay(5000);
    Serial1.println("Actuator set");

    stopActuators();
    delay(3000);
    Serial1.println("Actuators stopped");

    disarmPixhawk();
    delay(3000);
}

void disableRCChecks() {
    mavlink_message_t msg;
    uint8_t buf[MAVLINK_MAX_PACKET_LEN];

    mavlink_msg_param_set_pack(
        255, 190, &msg,
        1, 0, "COM_RC_IN_MODE", 2, MAV_PARAM_TYPE_UINT8
    );

    uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
    Serial1.write(buf, len);

    Serial1.println("RC checks disabled");
}

void sendMavlinkMessage(const mavlink_message_t &msg) {
    uint8_t buf[MAVLINK_MAX_PACKET_LEN];
    uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
    Serial1.write(buf, len);
    Serial1.println("Sent MAVLink command.");
}

void disablePreflightChecks() {
    mavlink_message_t msg;

    mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "ARM_IMU_GYR", 0, MAV_PARAM_TYPE_REAL32);
    sendMavlinkMessage(msg);

    mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "ARM_MAG", 0, MAV_PARAM_TYPE_REAL32);
    sendMavlinkMessage(msg);

    Serial1.println("Preflight checks disabled.");

    mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "RC_IN_MODE", 4, MAV_PARAM_TYPE_UINT8);
    sendMavlinkMessage(msg);
}

void disableCommanderChecks() {
    mavlink_message_t msg;

    mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_ARM_CHK_SUM", 0, MAV_PARAM_TYPE_UINT8);
    sendMavlinkMessage(msg);

    mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_ARM_CHK_LOC_POS", 0, MAV_PARAM_TYPE_UINT8);
    sendMavlinkMessage(msg);

    mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_ARM_CHK_GLB_POS", 0, MAV_PARAM_TYPE_UINT8);
    sendMavlinkMessage(msg);

    mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_ARM_CHK_MISS", 0, MAV_PARAM_TYPE_UINT8);
    sendMavlinkMessage(msg);

    mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_ARM_CHK_OFFB", 0, MAV_PARAM_TYPE_UINT8);
    sendMavlinkMessage(msg);

    mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_ARM_CHK_HOME", 0, MAV_PARAM_TYPE_UINT8);
    sendMavlinkMessage(msg);

    mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_HEALTH_SUM", 0, MAV_PARAM_TYPE_UINT8);
    sendMavlinkMessage(msg);
    Serial1.println("Commander checks disabled.");
}

void setActuator() {
    mavlink_message_t msg;

    mavlink_msg_command_long_pack(255, 190, &msg, 1, 1, MAV_CMD_DO_SET_ACTUATOR, 0, 1, 1, 0, 0, 0, 0, 0);
    sendMavlinkMessage(msg);

    delay(3000);

    mavlink_msg_command_long_pack(255, 190, &msg, 1, 1, MAV_CMD_DO_SET_ACTUATOR, 0, -1, 1, 0, 0, 0, 0, 0);
    sendMavlinkMessage(msg);
    delay(3000);

    mavlink_msg_command_long_pack(255, 190, &msg, 1, 1, MAV_CMD_DO_SET_ACTUATOR, 0, 1, -1, 0, 0, 0, 0, 0);
    sendMavlinkMessage(msg);

    delay(3000);
    mavlink_msg_command_long_pack(255, 190, &msg, 1, 1, MAV_CMD_DO_SET_ACTUATOR, 0, -1, -1, 0, 0, 0, 0, 0);
    sendMavlinkMessage(msg);

    delay(3000);
}

void armPixhawk() {
    mavlink_message_t msg;
    uint8_t buf[MAVLINK_MAX_PACKET_LEN];

    mavlink_msg_command_long_pack(
        255, 190, &msg,
        1, 0,
        MAV_CMD_COMPONENT_ARM_DISARM,
        0,
        1, 0, 0, 0, 0, 0, 0
    );

    uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
    Serial1.write(buf, len);

    Serial1.println("Pixhawk armed");
}

void disarmPixhawk() {
    mavlink_message_t msg;
    uint8_t buf[MAVLINK_MAX_PACKET_LEN];

    mavlink_msg_command_long_pack(
        255, 190, &msg,
        1, 0,
        MAV_CMD_COMPONENT_ARM_DISARM,
        0,
        0, 0, 0, 0, 0, 0, 0
    );

    uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
    Serial1.write(buf, len);

    Serial1.println("Pixhawk disarmed");
}

void setManualMode() {
    mavlink_message_t msg;

    mavlink_msg_set_mode_pack(255, 190, &msg, 1, MAV_MODE_MANUAL_ARMED, MAV_MODE_FLAG_MANUAL_INPUT_ENABLED);
    sendMavlinkMessage(msg);

    Serial1.println("Set to Manual mode.");
}

void setOffboardMode() {
    mavlink_message_t msg;

    mavlink_msg_set_mode_pack(255, 190, &msg, 1, MAV_MODE_GUIDED_ARMED, MAV_MODE_FLAG_CUSTOM_MODE_ENABLED);
    sendMavlinkMessage(msg);

    Serial1.println("Set to Offboard mode.");
}

void setMissionMode() {
    mavlink_message_t msg;

    mavlink_msg_set_mode_pack(255, 190, &msg, 1, MAV_MODE_AUTO_ARMED, MAV_MODE_FLAG_CUSTOM_MODE_ENABLED);
    sendMavlinkMessage(msg);

    Serial1.println("Set to Mission mode.");
}

void stopActuators() {
    mavlink_message_t msg;

    // Assuming we're setting control values to stop actuators.

    mavlink_actuator_control_target_t act_control;
    act_control.time_usec = 0;
    act_control.group_mlx = 0;  // actuator group
    act_control.controls[0] = 0;
    // ... set other controls as needed
    mavlink_msg_actuator_control_target_encode(255, 190, &msg, &act_control);

    sendMavlinkMessage(msg);
}

[quote="arya_124, post:1, topic:1285378"]
Serial1.begin(921600, SERIAL_8N1, 36, 37); // Initialize Serial2 with RX on pin 36 and TX on pin 37
[/quote]Which ESP32 are you using ? On most devKits, gpio 37 is not exposed. and if it is, chances are it's INOUT only. Why not try using Serial2 first and try using the default pins.

hey @Deva_Rishi , i am using ESSPRESSIF ESP32-S3-Wroom-1.

The key word.
There is still quiet a few variants, but if your board has PSRAM, it states here not to use GPIO 35, 36 & 37. Anyway just try on the default UART1 pins first (18 & 17)

ESP32-S3-WROOM-1-N4 4 MB flash
ESP32-S3-WROOM-1-N8 8 MB flash
ESP32-S3-WROOM-1-N16 16 MB flash
ESP32-S3-WROOM-1-H4 4 MB flash
ESP32-S3-WROOM-1-N4R2 4 MB flash 2 MB spiram (Quad SPI) -
ESP32-S3-WROOM-1-N8R2 8 MB flash 2 MB spiram (Quad SPI) -
ESP32-S3-WROOM-1-N16R2 16 MB flash 2 MB spiram (Quad SPI) -
ESP32-S3-WROOM-1-N4R8 4 MB flash 8 MB spiram (Octal SPI) - GPIO35, GPIO36, GPIO37
ESP32-S3-WROOM-1-N8R8 8 MB flash 8 MB spiram (Octal SPI) - GPIO35, GPIO36, GPIO37
ESP32-S3-WROOM-1-N16R8 16 MB flash 8 MB spiram (Octal SPI) - GPIO35, GPIO36, GPIO37

It seems to me you are sending both the mavlink message and the debug message on the same serial1 connection ( but I never used this library ):

void sendMavlinkMessage(const mavlink_message_t &msg) {
    uint8_t buf[MAVLINK_MAX_PACKET_LEN];
    uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
    Serial1.write(buf, len);
    Serial1.println("Sent MAVLink command.");

Edit:

hey @18f , I forgot to attach this message earlier. Could you figure out which type of esp32 this is? I have no clue how to find which type it is.

Hey david,

I sent 'em through different serial connections earlier- got no response

It's an esp32-s3 wroom

Hey @sonofcy ,

be sure to check @18f 's (post #5). We are tryna figure out which one from the list they have given.

Okay quick update people, I am getting a "waiting to download" response on my serial monitor. How can I rectify that?

Can you not get that info by loading a simple blink sketch, telling the IDE the board is a dev esp32 if it doesn't autodetect it, then look at the board attributes under tools?

Try loading the blink sketch and click upload.

I/O0 should not be 0 on reset ( check if you have something pulling it down )

I am unsure about the led pin. do you mind checking the image and letting me know?

Appreciate the quick response

(By the way, same error message whichever pin i try)

hey david,

theres nothing pulling down on the reset button. By the way please talk to me in baby language, I'm new to this :))

The blink sketch uses LED_BUILTIN, you don't need to know the pin #. Use the tools menu after it is uploaded to determine ram and flash sizes.


Screenshot 2024-07-26 at 10.05.45

okay now it works.

Which post 'solved' it? Mark that post Solved.

It is on the bottom of the module. Mine is an N8R2.

1 Like