Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 Support #20

Open
Jip-Hop opened this issue Nov 21, 2019 · 6 comments
Open

ESP32 Support #20

Jip-Hop opened this issue Nov 21, 2019 · 6 comments
Assignees
Labels

Comments

@Jip-Hop
Copy link

Jip-Hop commented Nov 21, 2019

I got really enthusiastic when I saw this library! But can't get it to work with my DOIT ESP32 Devkit V1. Seems like a great candidate because of it's inbuilt Bluetooth LE and Arduino IDE compatibility.

Can this library work with the ESP32? Or does it need changes?

I found this fork or Web-Bluetooth-Terminal, which supposedly adds ESP32 support. But it didn't work for me either.

I'm using this sketch from this tutorial on the ESP32 to enable Bluetooth LE. It uses the UART service UUID 6E400001-B5A3-F393-E0A9-E50E24DCCA9E and a characteristic of 6E400002-B5A3-F393-E0A9-E50E24DCCA9E for receiving data with "WRITE" and a characteristic of 6E400003-B5A3-F393-E0A9-E50E24DCCA9E used to send data with "NOTIFY". This also matches the UUID and characteristic in the Web Bluetooth Terminal fork by kpatel122. So I had a good feeling about it 😝

I can connect to the ESP32 with Adafruit Bluefruit LE Connect for OS X. It connects to the ESP32 just fine, logs the received data and I can send values to it as well, which shows up in the Arduino Serial Monitor. So far so good.

Screenshot 2019-11-21 at 10 26 39

Screenshot 2019-11-21 at 10 42 55

But then trying to connect with Web Bluetooth Terminal in the latest Google Chrome, it won't list any devices. Same with the fork. I tried using the underlying BluetoothTerminal.js API directly in this JSitor snippet, but it doesn't list any device in the permission dialog either. It just says: "No compatible devices found."

Looking in chrome://bluetooth-internals it shows me the following info for the ESP32:

ESP32: UNSUPPORTED_DEVICE
Name: ESP32
Address: 30:AE:A4:40:50:7E
GATT Connected: Not Connected
Latest RSSI:Unknown
Services: Unknown

If I enable the Experimental Web Platform features in chrome://flags (which I just did for testing, because I can't expect users of my web app to do this), I can send an receive data from the chrome://bluetooth-internals page, but the ESP32 still won't show up in Web Bluetooth Terminal, not in the fork and not in my JSitor snippet.

Screenshot 2019-11-21 at 10 28 31

Is this a dead end? Would be great to know what can be done to get this working with the ESP32.

@loginov-rocks loginov-rocks self-assigned this Nov 25, 2019
@immakermatty
Copy link

Any updates? I would love to have ESP32 support too!

@immakermatty
Copy link

immakermatty commented Mar 29, 2020

I figured it out! Use this code

#include <Arduino.h>

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

#define BAUDRATE 115200

//#define SERVICE_UUID "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
//#define CHARACTERISTIC_UUID "6e400002-b5a3-f393-e0a9-e50e24dcca9e"

#define SERVICE_UUID "FFE0"
#define CHARACTERISTIC_UUID "FFE1"

BLEServer *pServer;
BLEService *pService;
BLECharacteristic *pCharacteristic;

bool deviceConnected = false;
std::string rxValue = "";

class MyServerCallbacks : public BLEServerCallbacks
{
    void onConnect(BLEServer *pServer)
    {
        Serial.println("= Connected =");
        deviceConnected = true;
    }
    void onDisconnect(BLEServer *pServer)
    {
        Serial.println("= Disonnected =");
        deviceConnected = false;
    }
};

class MyCallbacks : public BLECharacteristicCallbacks
{
    void onWrite(BLECharacteristic *pCharacteristic)
    {
        Serial.println("= Receive =");
        rxValue = pCharacteristic->getValue();
        
        Serial.println(rxValue.c_str());
    }
};

void setup()
{
    Serial.begin(BAUDRATE);

    BLEDevice::init("ESP32");
    pServer = BLEDevice::createServer();
    pServer->setCallbacks(new MyServerCallbacks());

    pService = pServer->createService(SERVICE_UUID);

    pCharacteristic = pService->createCharacteristic(
        CHARACTERISTIC_UUID,
        BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE);

    pCharacteristic->addDescriptor(new BLE2902());
    pCharacteristic->setCallbacks(new MyCallbacks());

    pService->start();
    pServer->getAdvertising()->start();
}

void loop()
{
    static unsigned long millis_last = 0;

    if (millis() - millis_last >= 1000) {
        millis_last = millis();

        if (deviceConnected)
        {
            byte msg[] = "Hello world!\n";

            Serial.println("= Sending =");
            pCharacteristic->setValue(msg, sizeof(msg));
            pCharacteristic->notify();
        }
    }
}

@dheera
Copy link

dheera commented Apr 11, 2020

@immakermatty @Jip-Hop
Here's my working ESP32 version
https://github.com/dheera/ESP32-Web-Bluetooth-Terminal

The main change is that RX and TX need to be different characteristics. I have also included a reference sketch in my fork that works.

@puttley
Copy link

puttley commented Feb 21, 2021

Hello,
I have tried to connect my ESP32 and receive a GATT error message.

Requesting bluetooth device...
"ESP32" bluetooth device selected
Connecting to GATT server...
GATT server connected
Getting service...
Service found
Getting characteristic...
CharacteristicTx found
CharacteristicRx found
Starting notifications...
NotSupportedError: GATT Error Unknown.

@alexwohlbruck
Copy link

Hello, I have tried to connect my ESP32 and receive a GATT error message.

Requesting bluetooth device... "ESP32" bluetooth device selected Connecting to GATT server... GATT server connected Getting service... Service found Getting characteristic... CharacteristicTx found CharacteristicRx found Starting notifications... NotSupportedError: GATT Error Unknown.

I am also getting this "GATT Error Unknown" message when implementing my own web app with the Bluetooth API. Not a very useful message, I'll say that.

My device has an ESP32 chip running this code sample on Micropython:
https://github.com/micropython/micropython/blob/master/examples/bluetooth/ble_uart_peripheral.py

@Godefridus
Copy link

I created a sketch for the ESP32 Dev Kit to communicate with my iPhone using the Bluefruit Connect app. I added code for the Control Pad arrows and numbers:
https://github.com/Godefridus/ESP32/blob/main/ESP32_UART.ino
Read the heading of the sketch for explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants