comprender qué es Bluetooth;
diferenciar Bluetooth Classic y BLE;
emparejar el ESP32 con un celular;
enviar información desde el ESP32;
recibir comandos desde el celular;
controlar LEDs mediante Bluetooth;
enviar valores de sensores;
construir una pequeña terminal inalámbrica.
diferenciar Bluetooth Classic y BLE;
emparejar el ESP32 con un celular;
enviar información desde el ESP32;
recibir comandos desde el celular;
controlar LEDs mediante Bluetooth;
enviar valores de sensores;
construir una pequeña terminal inalámbrica.
Ejercicio 1 — Crear nuestro primer dispositivo Bluetooth
¿Quién está anunciando el dispositivo?
¿Quién busca dispositivos?
¿Qué representa "ESP32_Clase"?
¿Quién está anunciando el dispositivo?
¿Quién busca dispositivos?
¿Qué representa "ESP32_Clase"?
blu.ino
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_Clase");
Serial.println("Bluetooth iniciado");
}
void loop() {
}Ejercicio 2 — Enviar un mensaje por Bluetooth
envíar.ino
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_Clase");
}
void loop() {
SerialBT.println("Hola desde ESP32");
delay(1000);
}Ejercicio 3 — Contador Bluetooth
contador.ino
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
int contador = 0;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_Clase");
}
void loop() {
SerialBT.print("Contador: ");
SerialBT.println(contador);
contador++;
delay(1000);
}Ejercicio 4 — Simular un sensor
sensor.ino
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_Sensor");
}
void loop() {
int temperatura = random(20, 35);
SerialBT.print("Temperatura: ");
SerialBT.print(temperatura);
SerialBT.println(" C");
delay(1000);
}Ejercicio 5 — Enviar un valor analógico
Potenciómetro
3.3 V ───── extremo
GPIO34 ──── centro
GND ─────── extremo
Potenciómetro
3.3 V ───── extremo
GPIO34 ──── centro
GND ─────── extremo
adc.ino
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
const int sensor = 34;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_Sensor");
}
void loop() {
int valor = analogRead(sensor);
SerialBT.print("ADC: ");
SerialBT.println(valor);
delay(500);
}Ejercicio 6 — Celular → ESP32
esp32.ino
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_Receptor");
Serial.println("Bluetooth iniciado");
}
void loop() {
if (SerialBT.available()) {
char dato = SerialBT.read();
Serial.print("Recibido: ");
Serial.println(dato);
}
}Ejercicio 9 — Encender LED desde el celular
led.ino
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
const int led = 2;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_LED");
pinMode(led, OUTPUT);
SerialBT.println("Control LED");
SerialBT.println("1 = Encender");
SerialBT.println("0 = Apagar");
}
void loop() {
if (SerialBT.available()) {
char comando = SerialBT.read();
if (comando == '1') {
digitalWrite(led, HIGH);
SerialBT.println("LED ENCENDIDO");
}
if (comando == '0') {
digitalWrite(led, LOW);
SerialBT.println("LED APAGADO");
}
}
}Ejercicio 10 — Comandos con palabras
comandos.ino
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
const int led = 2;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_Control");
pinMode(led, OUTPUT);
SerialBT.println("Sistema Bluetooth iniciado");
SerialBT.println("Comandos:");
SerialBT.println("ON");
SerialBT.println("OFF");
SerialBT.println("ESTADO");
}
void loop() {
if (SerialBT.available()) {
String comando = SerialBT.readStringUntil('\n');
comando.trim();
comando.toUpperCase();
if (comando == "ON") {
digitalWrite(led, HIGH);
SerialBT.println("LED ENCENDIDO");
}
else if (comando == "OFF") {
digitalWrite(led, LOW);
SerialBT.println("LED APAGADO");
}
else if (comando == "ESTADO") {
if (digitalRead(led)) {
SerialBT.println("LED: ENCENDIDO");
}
else {
SerialBT.println("LED: APAGADO");
}
}
else {
SerialBT.println("Comando desconocido");
}
}
}