Connecting two esp32 devices via bluetooth using micropython



  • I want to transfer joystick data from one esp32 to another, so that I can make a remote controlled car. I'm having trouble connecting the two esp32 devices. I asked chatGPT and it gave me many codes using the bluetooth module and the BluetoothSocket class, but it seems like the BluetoothSocket class ins't in the bluetooth module.
    import bluetooth

    This is the code:

    Set the MAC address of the other ESP32 device

    other_device_mac = "yy:yy:yy:yy:yy:yy"

    Create a Bluetooth socket and listen for incoming connections

    sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    sock.bind((other_device_mac, 1))
    sock.listen(1)

    Wait for a connection from the other device

    print("Waiting for connection...")
    client_sock, client_info = sock.accept()
    print("Accepted connection from", client_info)

    Receive data from the other device

    data = client_sock.recv(1024)
    print("Received data:", data.decode())

    Close the client socket and the Bluetooth socket

    client_sock.close()
    sock.close()

    I get this error:

    Traceback (most recent call last):
    File "<stdin>", line 7, in <module>
    AttributeError: 'module' object has no attribute 'BluetoothSocket'

    Does anyone have any suggestions to help connect the two esp32a via bluetooth, so that data transfer is possible?


Log in to reply
 

Pycom on Twitter