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