Can't access Bluetooth serial with Python ESP32


Using an ESP32, I want to send/stream sensor data per Bluetooth to my Macbook using a Python program to save the data to a database and to display it in realtime. A first test was to just establish a bluetooth connection, access the bluetooth serial port with python and print the numbers. However, running the program in the terminal, it gives this error message:

.../anaconda3/lib/python3.8/site-packages/serial/", line 325, in open
raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 16] could not open port /dev/tty.ESP32test-ESP32SPP: [Errno 16] Resource busy: '/dev/tty.ESP32test-ESP32SPP'

Here's my ESP32 code:

#include "BluetoothSerial.h"

#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it

BluetoothSerial SerialBT;

const int potPin = 34;

int potValue = 0;

void setup() {
  // Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  // Serial.println("The device started, now you can pair it with bluetooth!");

void loop() {

  potValue = analogRead(potPin);


Here's the python code:

import serial

#ser = serial.Serial("/dev/cu.usbserial-0001",115200)
ser = serial.Serial('/dev/tty.ESP32test-ESP32SPP',115200)
#ser = serial.Serial('/dev/cu.ESP32test-ESP32SPP',115200)

while True:
	data = ser.readline()

When accessing the USB serial with python, the code works as expected. Using the bluetooth port for the serial monitor of the Arduino IDE, data is also displayed as expected. However: when leaving Serial.begin(115200); in the setup, I must first access the usb serial port, before I can access the bluetooth port on the serial monitor. Otherwise the bluetooth serial says that the port is occupied.

Is this the right approach to access the bluetooth serial over Python? If yes, how can the error of the python code not being able to access the ESP bluetooth port, be corrected?


I found a kind of a solution for it. Not including lets it access the bluetooth serial and it prints the data. However, once the program is terminated, I must shut down and restart the ESP32 again to re-access the bluetooth serial. Otherwise it shows the error of 'Resource busy'.

The 'new' code to access the bluetooth serial:

import serial

ser = serial.Serial('/dev/cu.ESP32test-ESP32SPP',115200)

while True:
	data = ser.readline()

May anybody help me with this how I could just disconnect and reconnect the bluetooth, without having to shut down and restart the ESP32?

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