ESP32 Bluetooth: Difference between revisions
Created page with "===Interfacing LDR with ESP32 and Transmitting Sensor Data using Bluetooth=== '''Interfacing LDR with ESP32 and Transmitting Sensor Data using Bluetooth''' demonstrates how an LDR sensor can be interfaced with an ESP32 board to measure light intensity and transmit the sensor value wirelessly using Bluetooth. The ESP32 reads the analog value from the LDR and sends the value through Bluetooth to a paired device. <center> '''LDR → ESP32 → Bluetooth → Paired Device'..." |
|||
| (11 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
=== | ===ESP32 Bluetooth Communication=== | ||
''' | '''ESP32 Bluetooth Communication''' demonstrates how the built-in Bluetooth functionality of the ESP32 can be used for wireless communication between the ESP32 board and a Bluetooth-enabled device such as a smartphone, tablet, or computer. | ||
The ESP32 | The ESP32 can send and receive data wirelessly using Bluetooth without requiring a physical serial connection. | ||
<center> | <center> | ||
''' | '''Bluetooth Device → Wireless Communication → ESP32''' | ||
</center> | </center> | ||
== Objectives == | == Objectives == | ||
* To study the | * To study the Bluetooth functionality available in the ESP32. | ||
* To | * To understand how to initialize Bluetooth communication. | ||
* To | * To pair the ESP32 with a Bluetooth-enabled device. | ||
* To | * To send data from the ESP32 to a Bluetooth device. | ||
* To | * To receive commands or data from a Bluetooth device. | ||
* To control ESP32 peripherals using Bluetooth commands. | |||
== Software == | == Software == | ||
* Arduino IDE | * Arduino IDE | ||
* ESP32 | * ESP32 board package | ||
* | * Bluetooth-enabled smartphone, tablet, or computer | ||
== Hardware Modules == | == Hardware Modules == | ||
* ESP32 Board | * ESP32 Development Board | ||
* PC / Laptop | * PC / Laptop | ||
* Bluetooth-enabled device | * Smartphone / Bluetooth-enabled device | ||
* | * External sensors or actuators as required | ||
== Theory == | == Theory == | ||
'''Bluetooth''' is a wireless communication technology used for exchanging data between electronic devices over short distances. | |||
The ESP32 contains built-in Bluetooth hardware, allowing it to communicate with compatible Bluetooth devices without requiring an external Bluetooth module. | |||
The ESP32 can be programmed to operate as a Bluetooth device with a user-defined device name. | |||
<center> | <center> | ||
''' | '''ESP32 Bluetooth → Wireless Data Transmission → Smartphone / PC''' | ||
</center> | </center> | ||
Bluetooth communication can be used for: | |||
* Sending sensor readings | |||
* Receiving control commands | |||
* Controlling LEDs | |||
* Controlling buzzers | |||
* Monitoring system status | |||
* Sending data to a mobile application | |||
* Wireless serial communication | |||
== ESP32 Bluetooth == | |||
The ESP32 provides built-in Bluetooth functionality. | |||
For Bluetooth Classic communication in the Arduino environment, the '''BluetoothSerial''' library can be used. | |||
The | The library provides a serial-like interface for sending and receiving data wirelessly. | ||
The basic sequence is: | |||
<center> | <center> | ||
''' | '''Initialize Bluetooth → Set Device Name → Pair Device → Send/Receive Data''' | ||
</center> | </center> | ||
The | == BluetoothSerial Library == | ||
The '''BluetoothSerial''' library provides functions for Bluetooth Classic communication. | |||
The library is included using: | |||
<pre> | |||
#include "BluetoothSerial.h" | |||
</pre> | |||
A Bluetooth serial object is then created: | |||
<pre> | <pre> | ||
BluetoothSerial SerialBT; | |||
</pre> | </pre> | ||
Bluetooth communication is started using: | |||
<pre> | |||
SerialBT.begin("ESP32_Demo"); | |||
</pre> | |||
Here, '''ESP32_Demo''' is the Bluetooth device name that appears when searching for the ESP32 from another Bluetooth device. | |||
== Bluetooth Communication == | == Bluetooth Communication == | ||
Bluetooth communication works in two directions. | |||
=== Sending Data === | |||
The | The ESP32 can send data to a connected Bluetooth device using: | ||
<pre> | <pre> | ||
SerialBT.print("Hello"); | |||
SerialBT.println("ESP32"); | |||
</pre> | </pre> | ||
The '''print()''' function sends data without automatically adding a new line. | |||
The '''println()''' function sends data followed by a new line. | |||
<center> | |||
'''ESP32 → Bluetooth → Smartphone / PC''' | |||
</center> | |||
=== Receiving Data === | |||
The ESP32 can check whether data has been received using: | |||
<pre> | <pre> | ||
SerialBT.available() | |||
</pre> | </pre> | ||
Received characters can be read using: | |||
<pre> | <pre> | ||
char c = SerialBT.read(); | |||
</pre> | </pre> | ||
The received characters can then be processed as commands or sensor-control instructions. | |||
<center> | <center> | ||
''' | '''Smartphone / PC → Bluetooth → ESP32''' | ||
</center> | </center> | ||
== Bluetooth Device Name == | |||
The ESP32 Bluetooth device name can be specified while starting Bluetooth. | |||
Example: | |||
<pre> | <pre> | ||
SerialBT.begin("ESP32_Demo"); | |||
</pre> | </pre> | ||
After Bluetooth is started, the device appears as: | |||
'''ESP32_Demo''' | |||
on the Bluetooth device used for pairing. | |||
The name can be changed as required. | |||
For example: | |||
<pre> | <pre> | ||
SerialBT.begin("MyESP32"); | |||
</pre> | </pre> | ||
== | == Connections == | ||
Bluetooth communication itself does not require external communication wires between the ESP32 and the smartphone or computer. | |||
{| class="wikitable" | |||
! Device | |||
! Connection | |||
! Function | |||
|- | |||
| ESP32 | |||
| Built-in Bluetooth | |||
| Wireless communication | |||
|- | |||
| Smartphone / PC | |||
| Bluetooth | |||
| Wireless communication | |||
|} | |||
External sensors and actuators can be connected to the ESP32 separately depending on the application. | |||
== Basic Bluetooth Communication Flow == | |||
The Bluetooth communication process can be summarized as: | |||
# Start the Arduino IDE. | |||
# Include the '''BluetoothSerial''' library. | |||
# Create a Bluetooth serial object. | |||
# Start Bluetooth using '''SerialBT.begin()'''. | |||
# Assign a name to the ESP32 Bluetooth device. | |||
# Search for the ESP32 using a Bluetooth-enabled device. | |||
# Pair/connect the device with the ESP32. | |||
# Send data from the ESP32 using '''SerialBT.print()''' or '''SerialBT.println()'''. | |||
# Check for incoming data using '''SerialBT.available()'''. | |||
# Read incoming data using '''SerialBT.read()'''. | |||
# Process the received data according to the application. | |||
== Algorithm == | == Algorithm == | ||
# Start the Arduino IDE. | # Start the Arduino IDE. | ||
# Include the '''BluetoothSerial''' library. | # Include the '''BluetoothSerial''' library. | ||
# Create a '''BluetoothSerial''' object. | |||
# Start Bluetooth communication with a suitable device name. | |||
# Display the Bluetooth status on the Serial Monitor. | |||
# Create a | # Wait for a Bluetooth device to connect. | ||
# Check whether data is available from the Bluetooth device. | |||
# Start Bluetooth with | # Read the received data. | ||
# | # Process the received command or data. | ||
# Send a response using Bluetooth. | |||
# | |||
# | |||
# | |||
# | |||
# | |||
# Repeat the process continuously. | # Repeat the process continuously. | ||
<center> | <center> | ||
''' | '''Start → Initialize Bluetooth → Connect Device → Send/Receive Data → Process Data → Repeat''' | ||
</center> | </center> | ||
== Working == | == Working == | ||
When the ESP32 program starts, the Bluetooth interface is initialized using the '''BluetoothSerial''' library. | |||
The ESP32 becomes discoverable using the Bluetooth device name specified in the program. | |||
After pairing or connecting a smartphone or computer, data can be exchanged wirelessly. | |||
The | The ESP32 can send messages using: | ||
< | <pre> | ||
SerialBT.println("Hello from ESP32"); | |||
</ | </pre> | ||
It can also receive data from the connected Bluetooth device. | |||
For example, a smartphone can send: | |||
<pre> | |||
ON | |||
</pre> | |||
The | The ESP32 can receive this command and use it to control an output device such as an LED. | ||
Similarly, a command such as: | |||
<pre> | |||
OFF | |||
</pre> | |||
can be used to turn the LED OFF. | |||
<center> | <center> | ||
''' | '''Bluetooth Command → ESP32 → Program Processing → Output Control''' | ||
</center> | </center> | ||
<div style="margin-top:15px;"> | |||
[[Media:Cognifront-bt-app.zip|<span style="display:inline-block; padding:10px 20px; background:#092328; color:white; border-radius:5px; font-weight:bold; text-decoration:none;">Download Cognifront Bluetooth App</span>]]<br> | |||
Kindly install Cognifront Bluetooth App on your mobile phone (Android Support only) for doing communication with ESP32. | |||
</div> | |||
== | == Example Program == | ||
The Bluetooth | The following program demonstrates basic Bluetooth communication with the ESP32. | ||
<pre> | <pre> | ||
/* | |||
======================================== | |||
ESP32 Bluetooth + LM35 + Buzzer Demo | |||
======================================== | |||
Bluetooth Commands: | |||
ON -> Turn LED ON | |||
OFF -> Turn LED OFF | |||
TEMP -> Read LM35 Temperature | |||
STATUS -> Check ESP32 Status | |||
BLINK -> Blink LED 5 Times | |||
BUZZON -> Turn Buzzer ON | |||
BUZZOFF -> Turn Buzzer OFF | |||
======================================== | |||
Connections: | |||
LM35: | |||
VCC -> 3.3V | |||
OUT -> GPIO34 | |||
GND -> GND | |||
Buzzer: | |||
+ -> GPIO25 | |||
- -> GND | |||
LED: | |||
Built-in LED -> GPIO2 | |||
Bluetooth Device Name: | |||
ESP32_Demo | |||
======================================== | |||
*/ | |||
#include "BluetoothSerial.h" | |||
BluetoothSerial SerialBT; | |||
# | #define LED_PIN 2 | ||
#define LM35_PIN 34 | |||
#define BUZZER_PIN 25 | |||
String incomingData = ""; | |||
void setup() { | |||
pinMode(LED_PIN, OUTPUT); | |||
pinMode(BUZZER_PIN, OUTPUT); | |||
digitalWrite(LED_PIN, LOW); | |||
digitalWrite(BUZZER_PIN, LOW); | |||
Serial.begin(115200); | |||
Serial.begin( | |||
// Start Bluetooth | |||
SerialBT.begin("ESP32_Demo"); | |||
Serial.println(" | Serial.println("Bluetooth Started"); | ||
SerialBT.println("ESP32 Bluetooth Ready"); | |||
SerialBT.println("Commands:"); | |||
SerialBT.println("ON, OFF, TEMP, STATUS"); | |||
SerialBT.println("BLINK, BUZZON, BUZZOFF"); | |||
} | } | ||
void loop() | void loop() { | ||
{ | |||
while (SerialBT.available()) { | |||
char c = SerialBT.read(); | |||
if (c == '\n') { | |||
incomingData.trim(); | |||
incomingData.toUpperCase(); | |||
Serial.print("Received: "); | |||
Serial.println(incomingData); | |||
// ====================== | |||
// LED ON | |||
// ====================== | |||
if (incomingData == "ON") { | |||
digitalWrite(LED_PIN, HIGH); | |||
SerialBT.println("LED ON"); | |||
} | |||
// ====================== | |||
// LED OFF | |||
// ====================== | |||
else if (incomingData == "OFF") { | |||
digitalWrite(LED_PIN, LOW); | |||
SerialBT.println("LED OFF"); | |||
} | |||
// ====================== | |||
// TEMP READ | |||
// ====================== | |||
else if (incomingData == "TEMP") { | |||
float temperatureC = readTemperature(); | |||
SerialBT.print("Temperature: "); | |||
SerialBT.print(temperatureC); | |||
SerialBT.println(" °C"); | |||
Serial.print("Temperature: "); | |||
Serial.println(temperatureC); | |||
// Auto buzzer alert | |||
if (temperatureC > 35) { | |||
digitalWrite(BUZZER_PIN, HIGH); | |||
SerialBT.println("WARNING: HIGH TEMPERATURE!"); | |||
} | |||
else { | |||
digitalWrite(BUZZER_PIN, LOW); | |||
} | |||
} | |||
// ====================== | |||
// STATUS | |||
// ====================== | |||
else if (incomingData == "STATUS") { | |||
SerialBT.println("ESP32 Running OK"); | |||
} | |||
// ====================== | |||
// BLINK | |||
// ====================== | |||
else if (incomingData == "BLINK") { | |||
SerialBT.println("Blinking LED"); | |||
for (int i = 0; i < 5; i++) { | |||
digitalWrite(LED_PIN, HIGH); | |||
delay(300); | |||
digitalWrite(LED_PIN, LOW); | |||
delay(300); | |||
} | |||
SerialBT.println("Blink Complete"); | |||
} | |||
// ====================== | |||
// BUZZER ON | |||
// ====================== | |||
else if (incomingData == "BUZZON") { | |||
digitalWrite(BUZZER_PIN, HIGH); | |||
SerialBT.println("Buzzer ON"); | |||
} | |||
// ====================== | |||
// BUZZER OFF | |||
// ====================== | |||
else if (incomingData == "BUZZOFF") { | |||
digitalWrite(BUZZER_PIN, LOW); | |||
SerialBT.println("Buzzer OFF"); | |||
} | |||
// ====================== | |||
// UNKNOWN COMMAND | |||
// ====================== | |||
else { | |||
SerialBT.println("Unknown Command"); | |||
} | |||
incomingData = ""; | |||
} | |||
else { | |||
incomingData += c; | |||
} | |||
} | |||
} | |||
// ===================================== | |||
// FUNCTION: Read LM35 Temperature | |||
// ===================================== | |||
float readTemperature() { | |||
int adcValue = analogRead(LM35_PIN); | |||
// Convert ADC to Voltage | |||
float voltage = adcValue * (3.3 / 4095.0); | |||
// LM35 = 10mV per °C | |||
float temperatureC = voltage * 100.0; | |||
return temperatureC; | |||
} | } | ||
</pre> | </pre> | ||
== | == Example of Command-Based Communication == | ||
Bluetooth can also be used to send commands to the ESP32. | |||
For example: | |||
{| class="wikitable" | |||
! Command | |||
! Function | |||
|- | |||
| ON | |||
| Turn an output ON | |||
|- | |||
| OFF | |||
| Turn an output OFF | |||
|- | |||
| STATUS | |||
| Send ESP32 status | |||
|- | |||
| SENSOR | |||
| Send sensor reading | |||
|} | |||
A command received through Bluetooth can be compared in the program and the required operation can be performed. | |||
Example: | |||
<pre> | |||
if (command == "ON") { | |||
digitalWrite(LED_PIN, HIGH); | |||
SerialBT.println("LED ON"); | |||
} | |||
else if (command == "OFF") { | |||
digitalWrite(LED_PIN, LOW); | |||
SerialBT.println("LED OFF"); | |||
} | |||
</pre> | </pre> | ||
== Applications == | |||
ESP32 Bluetooth communication can be used in many applications, including: | |||
* Wireless LED control | |||
* Home automation | |||
* Sensor monitoring | |||
* Robot control | |||
* Wireless data logging | |||
* Industrial control systems | |||
* Smart appliances | |||
* Mobile-controlled projects | |||
* Wireless serial communication | |||
== Safety Precautions == | |||
* Check the ESP32 power supply before starting the experiment. | |||
* Use the correct GPIO pins for external sensors and actuators. | |||
* Do not exceed the GPIO current limits of the ESP32. | |||
* Ensure that the Bluetooth device is properly paired before testing communication. | |||
* Keep the Bluetooth-enabled devices within the appropriate communication range. | |||
* Use suitable driver circuits when controlling high-current loads. | |||
== Observation == | |||
After starting the program, the ESP32 appears as the configured Bluetooth device name. | |||
After pairing the ESP32 with a smartphone or computer, messages can be transmitted wirelessly. | |||
Observe the transmitted and received data on the Serial Monitor and the connected Bluetooth device. | |||
== Result == | == Result == | ||
The ESP32 successfully | The ESP32 successfully communicates with a Bluetooth-enabled device using its built-in Bluetooth interface. | ||
Data can be transmitted from the ESP32 to the connected device, and commands or data can be received by the ESP32 through Bluetooth. | |||
The | The Bluetooth interface can therefore be used for wireless monitoring, control, and data communication in ESP32-based applications. | ||
Latest revision as of 05:39, 4 September 2026
ESP32 Bluetooth Communication
ESP32 Bluetooth Communication demonstrates how the built-in Bluetooth functionality of the ESP32 can be used for wireless communication between the ESP32 board and a Bluetooth-enabled device such as a smartphone, tablet, or computer.
The ESP32 can send and receive data wirelessly using Bluetooth without requiring a physical serial connection.
Bluetooth Device → Wireless Communication → ESP32
Objectives
- To study the Bluetooth functionality available in the ESP32.
- To understand how to initialize Bluetooth communication.
- To pair the ESP32 with a Bluetooth-enabled device.
- To send data from the ESP32 to a Bluetooth device.
- To receive commands or data from a Bluetooth device.
- To control ESP32 peripherals using Bluetooth commands.
Software
- Arduino IDE
- ESP32 board package
- Bluetooth-enabled smartphone, tablet, or computer
Hardware Modules
- ESP32 Development Board
- PC / Laptop
- Smartphone / Bluetooth-enabled device
- External sensors or actuators as required
Theory
Bluetooth is a wireless communication technology used for exchanging data between electronic devices over short distances.
The ESP32 contains built-in Bluetooth hardware, allowing it to communicate with compatible Bluetooth devices without requiring an external Bluetooth module.
The ESP32 can be programmed to operate as a Bluetooth device with a user-defined device name.
ESP32 Bluetooth → Wireless Data Transmission → Smartphone / PC
Bluetooth communication can be used for:
- Sending sensor readings
- Receiving control commands
- Controlling LEDs
- Controlling buzzers
- Monitoring system status
- Sending data to a mobile application
- Wireless serial communication
ESP32 Bluetooth
The ESP32 provides built-in Bluetooth functionality.
For Bluetooth Classic communication in the Arduino environment, the BluetoothSerial library can be used.
The library provides a serial-like interface for sending and receiving data wirelessly.
The basic sequence is:
Initialize Bluetooth → Set Device Name → Pair Device → Send/Receive Data
BluetoothSerial Library
The BluetoothSerial library provides functions for Bluetooth Classic communication.
The library is included using:
#include "BluetoothSerial.h"
A Bluetooth serial object is then created:
BluetoothSerial SerialBT;
Bluetooth communication is started using:
SerialBT.begin("ESP32_Demo");
Here, ESP32_Demo is the Bluetooth device name that appears when searching for the ESP32 from another Bluetooth device.
Bluetooth Communication
Bluetooth communication works in two directions.
Sending Data
The ESP32 can send data to a connected Bluetooth device using:
SerialBT.print("Hello");
SerialBT.println("ESP32");
The print() function sends data without automatically adding a new line.
The println() function sends data followed by a new line.
ESP32 → Bluetooth → Smartphone / PC
Receiving Data
The ESP32 can check whether data has been received using:
SerialBT.available()
Received characters can be read using:
char c = SerialBT.read();
The received characters can then be processed as commands or sensor-control instructions.
Smartphone / PC → Bluetooth → ESP32
Bluetooth Device Name
The ESP32 Bluetooth device name can be specified while starting Bluetooth.
Example:
SerialBT.begin("ESP32_Demo");
After Bluetooth is started, the device appears as:
ESP32_Demo
on the Bluetooth device used for pairing.
The name can be changed as required.
For example:
SerialBT.begin("MyESP32");
Connections
Bluetooth communication itself does not require external communication wires between the ESP32 and the smartphone or computer.
| Device | Connection | Function |
|---|---|---|
| ESP32 | Built-in Bluetooth | Wireless communication |
| Smartphone / PC | Bluetooth | Wireless communication |
External sensors and actuators can be connected to the ESP32 separately depending on the application.
Basic Bluetooth Communication Flow
The Bluetooth communication process can be summarized as:
- Start the Arduino IDE.
- Include the BluetoothSerial library.
- Create a Bluetooth serial object.
- Start Bluetooth using SerialBT.begin().
- Assign a name to the ESP32 Bluetooth device.
- Search for the ESP32 using a Bluetooth-enabled device.
- Pair/connect the device with the ESP32.
- Send data from the ESP32 using SerialBT.print() or SerialBT.println().
- Check for incoming data using SerialBT.available().
- Read incoming data using SerialBT.read().
- Process the received data according to the application.
Algorithm
- Start the Arduino IDE.
- Include the BluetoothSerial library.
- Create a BluetoothSerial object.
- Start Bluetooth communication with a suitable device name.
- Display the Bluetooth status on the Serial Monitor.
- Wait for a Bluetooth device to connect.
- Check whether data is available from the Bluetooth device.
- Read the received data.
- Process the received command or data.
- Send a response using Bluetooth.
- Repeat the process continuously.
Start → Initialize Bluetooth → Connect Device → Send/Receive Data → Process Data → Repeat
Working
When the ESP32 program starts, the Bluetooth interface is initialized using the BluetoothSerial library.
The ESP32 becomes discoverable using the Bluetooth device name specified in the program.
After pairing or connecting a smartphone or computer, data can be exchanged wirelessly.
The ESP32 can send messages using:
SerialBT.println("Hello from ESP32");
It can also receive data from the connected Bluetooth device.
For example, a smartphone can send:
ON
The ESP32 can receive this command and use it to control an output device such as an LED.
Similarly, a command such as:
OFF
can be used to turn the LED OFF.
Bluetooth Command → ESP32 → Program Processing → Output Control
Download Cognifront Bluetooth App
Kindly install Cognifront Bluetooth App on your mobile phone (Android Support only) for doing communication with ESP32.
Example Program
The following program demonstrates basic Bluetooth communication with the ESP32.
/*
========================================
ESP32 Bluetooth + LM35 + Buzzer Demo
========================================
Bluetooth Commands:
ON -> Turn LED ON
OFF -> Turn LED OFF
TEMP -> Read LM35 Temperature
STATUS -> Check ESP32 Status
BLINK -> Blink LED 5 Times
BUZZON -> Turn Buzzer ON
BUZZOFF -> Turn Buzzer OFF
========================================
Connections:
LM35:
VCC -> 3.3V
OUT -> GPIO34
GND -> GND
Buzzer:
+ -> GPIO25
- -> GND
LED:
Built-in LED -> GPIO2
Bluetooth Device Name:
ESP32_Demo
========================================
*/
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
#define LED_PIN 2
#define LM35_PIN 34
#define BUZZER_PIN 25
String incomingData = "";
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Serial.begin(115200);
// Start Bluetooth
SerialBT.begin("ESP32_Demo");
Serial.println("Bluetooth Started");
SerialBT.println("ESP32 Bluetooth Ready");
SerialBT.println("Commands:");
SerialBT.println("ON, OFF, TEMP, STATUS");
SerialBT.println("BLINK, BUZZON, BUZZOFF");
}
void loop() {
while (SerialBT.available()) {
char c = SerialBT.read();
if (c == '\n') {
incomingData.trim();
incomingData.toUpperCase();
Serial.print("Received: ");
Serial.println(incomingData);
// ======================
// LED ON
// ======================
if (incomingData == "ON") {
digitalWrite(LED_PIN, HIGH);
SerialBT.println("LED ON");
}
// ======================
// LED OFF
// ======================
else if (incomingData == "OFF") {
digitalWrite(LED_PIN, LOW);
SerialBT.println("LED OFF");
}
// ======================
// TEMP READ
// ======================
else if (incomingData == "TEMP") {
float temperatureC = readTemperature();
SerialBT.print("Temperature: ");
SerialBT.print(temperatureC);
SerialBT.println(" °C");
Serial.print("Temperature: ");
Serial.println(temperatureC);
// Auto buzzer alert
if (temperatureC > 35) {
digitalWrite(BUZZER_PIN, HIGH);
SerialBT.println("WARNING: HIGH TEMPERATURE!");
}
else {
digitalWrite(BUZZER_PIN, LOW);
}
}
// ======================
// STATUS
// ======================
else if (incomingData == "STATUS") {
SerialBT.println("ESP32 Running OK");
}
// ======================
// BLINK
// ======================
else if (incomingData == "BLINK") {
SerialBT.println("Blinking LED");
for (int i = 0; i < 5; i++) {
digitalWrite(LED_PIN, HIGH);
delay(300);
digitalWrite(LED_PIN, LOW);
delay(300);
}
SerialBT.println("Blink Complete");
}
// ======================
// BUZZER ON
// ======================
else if (incomingData == "BUZZON") {
digitalWrite(BUZZER_PIN, HIGH);
SerialBT.println("Buzzer ON");
}
// ======================
// BUZZER OFF
// ======================
else if (incomingData == "BUZZOFF") {
digitalWrite(BUZZER_PIN, LOW);
SerialBT.println("Buzzer OFF");
}
// ======================
// UNKNOWN COMMAND
// ======================
else {
SerialBT.println("Unknown Command");
}
incomingData = "";
}
else {
incomingData += c;
}
}
}
// =====================================
// FUNCTION: Read LM35 Temperature
// =====================================
float readTemperature() {
int adcValue = analogRead(LM35_PIN);
// Convert ADC to Voltage
float voltage = adcValue * (3.3 / 4095.0);
// LM35 = 10mV per °C
float temperatureC = voltage * 100.0;
return temperatureC;
}
Example of Command-Based Communication
Bluetooth can also be used to send commands to the ESP32.
For example:
| Command | Function |
|---|---|
| ON | Turn an output ON |
| OFF | Turn an output OFF |
| STATUS | Send ESP32 status |
| SENSOR | Send sensor reading |
A command received through Bluetooth can be compared in the program and the required operation can be performed.
Example:
if (command == "ON") {
digitalWrite(LED_PIN, HIGH);
SerialBT.println("LED ON");
}
else if (command == "OFF") {
digitalWrite(LED_PIN, LOW);
SerialBT.println("LED OFF");
}
Applications
ESP32 Bluetooth communication can be used in many applications, including:
- Wireless LED control
- Home automation
- Sensor monitoring
- Robot control
- Wireless data logging
- Industrial control systems
- Smart appliances
- Mobile-controlled projects
- Wireless serial communication
Safety Precautions
- Check the ESP32 power supply before starting the experiment.
- Use the correct GPIO pins for external sensors and actuators.
- Do not exceed the GPIO current limits of the ESP32.
- Ensure that the Bluetooth device is properly paired before testing communication.
- Keep the Bluetooth-enabled devices within the appropriate communication range.
- Use suitable driver circuits when controlling high-current loads.
Observation
After starting the program, the ESP32 appears as the configured Bluetooth device name.
After pairing the ESP32 with a smartphone or computer, messages can be transmitted wirelessly.
Observe the transmitted and received data on the Serial Monitor and the connected Bluetooth device.
Result
The ESP32 successfully communicates with a Bluetooth-enabled device using its built-in Bluetooth interface.
Data can be transmitted from the ESP32 to the connected device, and commands or data can be received by the ESP32 through Bluetooth.
The Bluetooth interface can therefore be used for wireless monitoring, control, and data communication in ESP32-based applications.