Usage Example for ESP32 - Get Sensor Data
Last revision 2026/01/07
This article offers a detailed guide on using the ESP32 with Arduino IDE to acquire sensor data, specifically focusing on BLE Sensor Beacon and LM35 Temperature Sensor, including hardware preparation, sample code, and step-by-step operation procedures.
Hardware Preparation
- TEL0149 Gravity: BLE Sensor Beacon (configured per Configuration Guide)
- Gravity: Analog LM35 Temperature Sensor (or other analog sensors)
- ESP32 board
Software Preparation
- Arduino IDE with ESP32 environment: How to use FireBeetle_ESP32_E for the first time?
Sample Code
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
Changed to a beacon scanner to report iBeacon, EddystoneURL and EddystoneTLM beacons by beegee-tokyo
*/
#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLEEddystoneURL.h>
#include <BLEEddystoneTLM.h>
#include <BLEBeacon.h>
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8))
float Sensor_Data;
int scanTime = 5; //In seconds
BLEScan *pBLEScan;
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice)
{
if (advertisedDevice.haveName())
{
if(String(advertisedDevice.getName().c_str()) == "Gravity: Sensor Beacon"){
Serial.print("Device name: ");
Serial.println(advertisedDevice.getName().c_str());
std::string strManufacturerData = advertisedDevice.getManufacturerData();
uint8_t cManufacturerData[100];
strManufacturerData.copy((char *)cManufacturerData, strManufacturerData.length(), 0);
Serial.printf("strManufacturerData: %d ", strManufacturerData.length());
for (int i = 0; i < strManufacturerData.length(); i++)
{
Serial.printf("[%X]", cManufacturerData[i]);
}
Sensor_Data = int(cManufacturerData[2]<<8 | cManufacturerData[3]);
Serial.println();
Serial.print("Voltage:");Serial.print(int(Sensor_Data));Serial.println("mV");
Serial.print("Temp_LM35:");Serial.print(Sensor_Data/10);Serial.println("℃");
Serial.println("------------------");
}
}
}
};
void setup()
{
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop()
{
// put your main code here, to run repeatedly:
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}
Operation Steps
- Set up the Arduino IDE & ESP32 environment.
- Upload the sample code to the ESP32.
- Open the Serial Monitor (baud rate 115200) to view data.
Running Results
The ESP32 will output voltage and temperature data to the Serial Monitor.
Was this article helpful?
