Latest Updates

random

BLE Advertising Using ESP32

In this project we are implementing BLE advertisement. If someone carrying a smartphone with enabled Bluetooth comes near this, the person will be able to see the advertisement.

What is BLE?
Bluetooth Low Energy, BLE for short, is a power-conserving variant of Bluetooth. BLE’s primary application is short distance transmission of small amounts of data.

BLE Server and Client
There are two type of devices the server and client. The esp32 can act like server or client
The server advertises its existence, so it can be found by other devices, and contains the data that the client can read. The client scans the nearby devices, and when it finds the server it is looking for, it establishes a connection and listens for incoming data. This is called point-to-point communication.

BLE Supports two mode: -
  • Broadcast mode: the server transmits data to many clients that are connected.
  • Mesh network: All the devices are connected. This is a many to many connection.
Source code :


Steps: -
1) put source code in Arduino IDE
2) Download the uRF android app from play store
2) Download the uRF android app from play store
3) Start bluetooth of your cellphone
4) Scan device and go to bonded tab and see the following details shown in the image.

Output: -
              
Note: - you can see url https://Avotrix.com is advertise on device
Explanation: -
1) Include all the necessary libraries regarding BLE
  • #include "sys/time.h"
    #include "BLEDevice.h"
    #include "BLEServer.h"
    #include "BLEUtils.h"
    #include "esp_sleep.h"
2) Initialize wakeup, boots data of rtc memory
  • #define GPIO_DEEP_SLEEP_DURATION     1 
    RTC_DATA_ATTR static time_t last;       
    RTC_DATA_ATTR static uint32_t bootcount;
3) Generate UUId and set advertising message on BLE
  • BLEAdvertising *pAdvertising;
    struct timeval now; 
    void setBeacon() {
      char beacon_data[22];
      uint16_t beconUUID = 0xFEAA;
      uint16_t volt = 3300; // 3300mV = 3.3V
      uint16_t temp = (uint16_t)((float)23.00);
      uint32_t tmil = now.tv_sec*10;
      uint8_t temp_farenheit;
      float temp_celsius;
      temp_farenheit= temprature_sens_read();
      temp_celsius = ( temp_farenheit - 32 ) / 1.8;
      temp = (uint16_t)(temp_celsius);
      BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
      oAdvertisementData.setFlags(0x06); // GENERAL_DISC_MODE 0x02 | BR_EDR_NOT_SUPPORTED 0x04
      oAdvertisementData.setCompleteServices(BLEUUID(beconUUID));
        beacon_data[0] = 0x10;  // Eddystone Frame Type (Eddystone-URL)
        beacon_data[1] = 0x20;  // Beacons TX power at 0m
        beacon_data[2] = 0x03;  // URL Scheme 'https://'
        beacon_data[3] = 'A';  // URL add  1
        beacon_data[4] = 'v';  // URL add  2
        beacon_data[5] = 'o';  // URL add  3
        beacon_data[6] = 't';  // URL add  4
        beacon_data[7] = 'r';  // URL add  5
        beacon_data[8] = 'i';  // URL add  6
        beacon_data[9] = 'x';  // URL add  7
        beacon_data[10] = '.';  // URL add  8
        beacon_data[11] = 'c';  // URL add  9
        beacon_data[12] = 'o';  // URL add 10
        beacon_data[13] = 'm';  // URL add 11

      oAdvertisementData.setServiceData(BLEUUID(beconUUID), std::string(beacon_data, 14));
    //14:- the size of url that you have put  in beacon_data[]array.
      pAdvertising->setScanResponseData(oAdvertisementData);
    }

5) Setup BLE setting and create BLE device and start advertising
  • void setup() { 
      Serial.begin(115200);
      gettimeofday(&now, NULL);
      Serial.printf("start ESP32 %d\n",bootcount++);
      Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n",now.tv_sec,now.tv_sec-last);
      last = now.tv_sec;
      // Create the BLE Device
      BLEDevice::init("ESP32");
      // Create the BLE Server
      BLEServer *pServer = BLEDevice::createServer();
      pAdvertising = pServer->getAdvertising();
      setBeacon();
       // Start advertising
      pAdvertising->start();
      Serial.println("Advertizing started...");
      delay(1000);
      pAdvertising->stop();
      Serial.printf("enter deep sleep\n");
      esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION);
      Serial.printf("in deep sleep\n");
    }
If you are still facing issue regarding this topic Feel free to Ask Doubts in the Comment Box Below and Don't Forget to Follow us on 👍 Social Networks😉
BLE Advertising Using ESP32 Reviewed by Avotrix.Author on Tuesday, August 25, 2020 Rating: 5

No comments:

All Rights Reserved by Avotrix © 2018 - 2019
Developed and created by Avotrix

Contact Form

Name

Email *

Message *

Powered by Blogger.