Advertising dynamic data using ble on esp32

Zee_jim
Posts: 7
Joined: Wed Dec 05, 2018 9:07 am

Advertising dynamic data using ble on esp32

Postby Zee_jim » Tue Sep 24, 2019 6:47 am

Hello guys i am trying to send Sensor data from esp32 to a ble gateway which means the gateway can not connect to the esp32 to read the Sensor data unless i send the Sensor data with the advertising data.

Code: Select all

/*
 * ESP32-Dynabeacon
 * Non-connectable beacon that changes scan response data periodically
 * 
 * Tested on ESP32 devkit
 * 
 * Created on 23-1-2018 by RammaK
 * Based on Neil Kolban's ESP32-BLE library at https://github.com/nkolban/ESP32_BLE_Arduino
*/

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

BLEAdvertisementData advert;
BLEAdvertising *pAdvertising;

int i = 0;

//manufacturer code (0x02E5 for Espressif)
int man_code = 0x02E5;

String str[10] = {"Hello", "Darkness", "My", "Old", "Friend"};

//function takes String and adds manufacturer code at the beginning 
void setManData(String c, int c_size, BLEAdvertisementData &adv, int m_code) {
  
  String s;
  char b2 = (char)(m_code >> 8);
  m_code <<= 8;
  char b1 = (char)(m_code >> 8);
  s.concat(b1);
  s.concat(b2);
  s.concat(c);

  
  adv.setManufacturerData(s.c_str());
  
}

void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");

  BLEDevice::init("MyESP32");
  BLEServer *pServer = BLEDevice::createServer();

  pAdvertising = pServer->getAdvertising();
  advert.setName("ESP32-new");
  pAdvertising->setAdvertisementData(advert);
  pAdvertising->start();
}

void loop() {

  BLEAdvertisementData scan_response;
  
  setManData(str[i], str[i].length() , scan_response, man_code);

  pAdvertising->stop();
  pAdvertising->setScanResponseData(scan_response);
  pAdvertising->start();
  
  i++;
  if(i > 4) {
    i = 0;
  }
  
  delay(2000);
}
I got this code from this issue https://github.com/nkolban/esp32-snippets/issues/375,
With this code i get to advertise String str[10] = {"Hello", "Darkness", "My", "Old", "Friend"}; and i get the Hex format in my gateway, then i made a count which increases in the void loop() which i then convert to a string then i pass it through conversion to Hex function. when i check in the serial monitor by doing Serial.print(s.c_str()), i get the count increasing but i didnt get it advertising on my gateway.
i will be glad to receive anyone's help on this..
Thanks...

Who is online

Users browsing this forum: Bing [Bot], MSN [Bot] and 29 guests