Connecting to a bluetooth lightbulb and writing to character attribute (gattc)

arosboro
Posts: 3
Joined: Fri Apr 20, 2018 1:19 am

Connecting to a bluetooth lightbulb and writing to character attribute (gattc)

Postby arosboro » Sat Apr 21, 2018 4:10 pm

I'm trying to turn on a light that I have significant documentation for. I have been able to do this with a raspberry pi and pygatt libraries using some python code i was able to find. The bulb is documented here: http://www.yeelight.com/download/yeelig ... e_v1.0.pdf

My difficulty comes in understanding the examples of the IDF for bluetooth callbacks. The project I have conceptualized is to attach a pir sensor to the esp32 and turn the yeelights into a hallway light when the sensor detects motion.

I can think this through functionally as follows:

1. Motion on PIR Sensor pin
2. Connect to Hardcoded BT address #1
3. Write 18 character control sequence on handle 18 of 4 character shortened UUID characteristic for primary service
4. Repeat step 2-3 for BT address #2
5. Start countdown until 5 min or some delay without interruption from step 1
6. Run through steps 2-4 but with 18 character sequence to turn off lights

This seems like a simple thing to do, but I have not delved in C++ for some time, and can not find which callback constants would be appropriate to use in this case for a gattc callback or a gap callback. Or how to re-factor these steps w/ a callback based program.

Any ideas would be much appreciated. In the meantime, I'm reading Koban's ESP32 notes and poking around the gattc_demo.c in eclipse.

Thanks

chegewara
Posts: 2230
Joined: Wed Jun 14, 2017 9:00 pm

Re: Connecting to a bluetooth lightbulb and writing to character attribute (gattc)

Postby chegewara » Sun Apr 22, 2018 1:53 pm

https://www.youtube.com/watch?v=DxAGLC1ySNo
https://www.youtube.com/watch?v=NSqrYcwmTeE

Now, if i would try to make it work then i would start with nRF connect on smartphone and play for some time with different values to understand what to send to different characteristic and watch results, also it lets to watch notification values on 3 characteristics.

Im not sure you want to use Kolban's BLE library or write code from skratch, but in my case its easier to use Neil's library. You can write app in 1-2 days in esp-idf C++ or arduino.

Your plan looks good. This lightbulb is nice because it has internal on/off delay which you can use in your app, this will let you simplify app (characteristic 0xFFF2).

arosboro
Posts: 3
Joined: Fri Apr 20, 2018 1:19 am

Re: Connecting to a bluetooth lightbulb and writing to character attribute (gattc)

Postby arosboro » Mon Apr 23, 2018 4:13 pm

Thanks @chegewara, I see you're fairly active. I'm just getting started mostly as a hobbyist. I wasn't aware of Neil's Arduino library, but this is exactly on target for what I could understand.

I have things working with one light, and am trying to juggle a vector<BLEAddress> and vector<BLEClient *> to connect to two lights in total. I recall reading that the idf was hard coded to one connection, and even disconnecting and reconnecting would not work according to one of your posts.

I know the Service and Characteristic, and I can hard code the BLEAddresses, is it possible to write to a remote characteristic without first scanning and going through the discovery? This would simplify things significantly, as I could write my command and iterate through the devices I know about.

the following generates this output:

Code: Select all

std::vector<BLEClient *>::iterator client = clients.begin();
  for (std::vector<BLEAddress>::iterator server = lights.begin(); server != lights.end(); ++server) {
    pServerAddress = new BLEAddress(*server);
    pActiveClient = *client;
//    timerWrite(timer, 0);
    if (connectToServer(*pServerAddress, pActiveClient)) {
      Serial.println("We are now connected to a BLE Server.");
      Serial.println("Setting new characteristic value to \"" + command + "\"");
      // Set the characteristic's value to be the array of bytes that is actually a string.
      pRemoteCharacteristic->writeValue(command.c_str(), command.length());
    } else {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    delay(5000);
    pActiveClient->disconnect();
    ++client;
  }

bool connectToServer(BLEAddress pAddress, BLEClient *pClient) {    
    Serial.print("Forming a connection to ");
    Serial.println(pAddress.toString().c_str());
    pClient->connect(pAddress);
    Serial.println(" - Connected to server");

    // Obtain a reference to the service we are after in the remote BLE server.
    BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
    if (pRemoteService == nullptr) {
      Serial.print("Failed to find our service UUID: ");
      Serial.println(serviceUUID.toString().c_str());
      return false;
    }
    Serial.println(" - Found our service");


    // Obtain a reference to the characteristic in the service of the remote BLE server.
    pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
    if (pRemoteCharacteristic == nullptr) {
      Serial.print("Failed to find our characteristic UUID: ");
      Serial.println(charUUID.toString().c_str());
      return false;
    }
    Serial.println(" - Found our characteristic");

    // Read the value of the characteristic.
    std::string value = pRemoteCharacteristic->readValue();
    Serial.print("The characteristic value was: ");
    Serial.println(value.c_str());

    pRemoteCharacteristic->registerForNotify(notifyCallback);
}
------
Found our device![0;31mE (43062) BT: btc_search_callback BLE observe complete. Num Resp 1
[0m
Forming a connection to f4:b8:5e:df:86:7c
[0;31mE (43073) BT: btc_gattc_call_handler()[0m
[0;31mE (43179) BT: bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x003e[0m
[0;31mE (43179) BT: p_conn is NULL in bta_gattc_conn_cback
[0m
[0;31mE (43182) BT: bta_gattc_conn_cback() - cif=4 connected=0 conn_id=4 reason=0x003e[0m
[0;31mE (43190) BT: p_conn is NULL in bta_gattc_conn_cback
[0m
- Connected to server
Then hangs :(

Who is online

Users browsing this forum: No registered users and 165 guests