ESP32’s Bluetooth Connectivity to Arduino IDE (PC)

Hi all, I'm currently trying to program ESP32 wirelessly using its bluetooth module. The ESP32 reads data from MPU9250 that I want to save using serial monitor. I used the SerialtoSerialBT example to set up and pair the bluetooth device to my PC and have modified the original code to include bluetooth serial, but an error keeps popping up and the code does not get uploaded.

Can someone please point out what needs to be changed in order for this to work? I'm new to the entire Arduino architecture, would really appreciate any help.

Here's the modified code (from one of the MPU9250 library's examples):

#include "MPU9250.h"
#include <BluetoothSerial.h>

BluetoothSerial SerialBT;
MPU9250 mpu;

void setup() {
    Serial.begin(115200);
    SerialBT.begin("ESP32_BT"); // Set Bluetooth name
    Wire.begin();
    delay(2000);

    if (!mpu.setup(0x68)) {  // change to your own address
        while (1) {
            Serial.println("MPU connection failed. Please check your connection with `connection_check` example.");
            delay(5000);
        }
    }

    // calibrate anytime you want to
    Serial.println("Accel Gyro calibration will start in 5sec.");
    Serial.println("Please leave the device still on the flat plane.");
    mpu.verbose(true);
    delay(5000);
    mpu.calibrateAccelGyro();

    Serial.println("Mag calibration will start in 5sec.");
    Serial.println("Please Wave device in a figure eight until done.");
    delay(5000);
    mpu.calibrateMag();

    print_calibration();
    mpu.verbose(false);
}

void loop() {
    if (SerialBT.available()) {
        char command = SerialBT.read();
        processCommand(command);
    }

    if (mpu.update()) {
        static uint32_t prev_ms = millis();
        if (millis() > prev_ms + 25) {
            print_roll_pitch_yaw();
            prev_ms = millis();
        }
    }
}

void print_roll_pitch_yaw() {
    Serial.print("Yaw, Pitch, Roll: ");
    Serial.print(mpu.getYaw(), 2);
    Serial.print(", ");
    Serial.print(mpu.getPitch(), 2);
    Serial.print(", ");
    Serial.println(mpu.getRoll(), 2);
}

void print_calibration() {
    Serial.println("< calibration parameters >");
    Serial.println("accel bias [g]: ");
    Serial.print(mpu.getAccBiasX() * 1000.f / (float)MPU9250::CALIB_ACCEL_SENSITIVITY);
    Serial.print(", ");
    Serial.print(mpu.getAccBiasY() * 1000.f / (float)MPU9250::CALIB_ACCEL_SENSITIVITY);
    Serial.print(", ");
    Serial.print(mpu.getAccBiasZ() * 1000.f / (float)MPU9250::CALIB_ACCEL_SENSITIVITY);
    Serial.println();
    Serial.println("gyro bias [deg/s]: ");
    Serial.print(mpu.getGyroBiasX() / (float)MPU9250::CALIB_GYRO_SENSITIVITY);
    Serial.print(", ");
    Serial.print(mpu.getGyroBiasY() / (float)MPU9250::CALIB_GYRO_SENSITIVITY);
    Serial.print(", ");
    Serial.print(mpu.getGyroBiasZ() / (float)MPU9250::CALIB_GYRO_SENSITIVITY);
    Serial.println();
    Serial.println("mag bias [mG]: ");
    Serial.print(mpu.getMagBiasX());
    Serial.print(", ");
    Serial.print(mpu.getMagBiasY());
    Serial.print(", ");
    Serial.print(mpu.getMagBiasZ());
    Serial.println();
    Serial.println("mag scale []: ");
    Serial.print(mpu.getMagScaleX());
    Serial.print(", ");
    Serial.print(mpu.getMagScaleY());
    Serial.print(", ");
    Serial.print(mpu.getMagScaleZ());
    Serial.println();
}

void processCommand(char command) {
    // Process commands received over Bluetooth
    // Example:
    if (command == 'A') {
        // Do something
    } else if (command == 'B') {
        // Do something else
    }
}

And this is the error:

esptool.py v2.6
Serial port COM11
Connecting...
Traceback (most recent call last):
File "esptool.py", line 2959, in
File "esptool.py", line 2952, in _main
File "esptool.py", line 2653, in main
File "esptool.py", line 460, in connect
File "esptool.py", line 440, in _connect_attempt
File "esptool.py", line 379, in sync
File "esptool.py", line 322, in command
File "esptool.py", line 285, in write
File "site-packages\serial\serialwin32.py", line 323, in write
serial.serialutil.SerialTimeoutException: Write timeout
Failed to execute script esptool
An error occurred while uploading the sketch

Hi @supern0vasigma_008.

I'm not sure I understood correctly what you mean by this.

Is COM11 the serial port produced by the Bluetooth radio, or a serial port produced by connecting the ESP32 board to your computer with a USB cable?

Hi @ptillisch, thanks for responding. By "trying to program ESP32 wirelessly using its bluetooth module", I mean that I want to upload the given code onto ESP32 without using the USB wired connection to the computer, something similar to what the HC-05/ 06 module achieves. I'm not sure if it is possible but seeing the in-built Bluetooth feature of ESP, I thought it would be.

"Is COM11 the serial port produced by the Bluetooth radio, or a serial port produced by connecting the ESP32 board to your computer with a USB cable?"
Yes, so COM11 is the serial port produced for Bluetooth serial communication once I set up and pair the ESP32 Bluetooth to my PC using the SerialtoSerialBT example.

Have you considered using WiFi and uploading OTA (Over The Air) ?

Hi @UKHeliBob, I did try OTA and had setup a network COM port. The code was successfully uploaded as well, but the Serial Monitor wouldn't open up. It showed an error that the "serial monitor is not supported on network ports such as for the ESP 32 in this release", so I ditched WiFi for the time being and started looking at Bluetooth as the alternative.

That's because there is no serial COM port for it to work with

Take a look at the TelnetStream library available from the Library Manager in the IDE

Thanks for the suggestion. I read somewhere about Telnet as a possible solution to this, but couldn't find a comprehensive resource on it, would you please direct me to one that shows how to use it from scratch?

@juraj is the user to ask

Thanks @Juraj

Thanks @Juraj and @UKHeliBob, I'll try using Telnet for my application, will keep everyone posted.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.