Project 7: Bluetooth
In today’s project we will start to use the ESP32’s Bluetooth feature. There’s something interestting about todays project. It is specialized for android only, meanwhile i’m an IOS user. I need to borrow my friend’s phone for this one.
What are we going to do today isn’t something new so lets hope we could grasp these easily. We’ll be turning on led but instead of using push button, we’ll be using our phone.
Breadboard, ESP32, jumper wires, 330 ohm resistor, and also a phone (preferably with android operating system) are needed in today’s project.
Bluetooth Classic
This is the first step towards our bluetooth exploration. We need to set up our ESP32 and Android phone to connect the program.
Setting up
#include “BluetoothSerial.h”#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endifBluetoothSerial SerialBT;void setup() {
Serial.begin(115200);
SerialBT.begin(“ESP32test”); //Bluetooth device name
Serial.println(“The device started, now you can pair it with bluetooth!”);
}void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
We can find this code from File → Examples → Bluetooth Serial.
After the program is uploaded, open serial monitor and press EN button on the ESP32
Now, how to use our phone as the controller? First of all, download Serial Bluetooth Terminal in Play Store, connect with the ESP32 through Bluetooth. We could just type start to hello, or anything. The message will show in both Arduino IDE Serial Monitor and in the Bluetooth Terminal.
Bluetooth BLE
Set Up ESP32
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");BLEDevice::init("Long name works now");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("Testing Bluetooth Fayy");
pService->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising();
// this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Characteristic defined! Now you can read it in your phone!");
}void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
After that, as usual put them together according to this scheme
Connecting the jumper’s tricky, may we be careful enough in this part. Keep an eye on the scheme. Every single resistor needs to be parallel with LED and connected with GPIO 4.
Run the program to see how the LED will reacts
#include "BluetoothSerial.h"
#include <Wire.h>
#include <stdio.h>#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endifBluetoothSerial SerialBT;const int ledPin1 = 4;
String message = "";
char incomingChar;void setup() {
pinMode(ledPin1, OUTPUT);
Serial.begin(115200);
SerialBT.begin("ESP32");
Serial.println("The device started, now you can pair it with bluetooth!");
}void loop() {
if (SerialBT.available()){
char incomingChar = SerialBT.read();
if (incomingChar != '\n'){
message += String(incomingChar);
}
else{
message = "";
}
Serial.write(incomingChar);
}if (message =="start"){
digitalWrite(ledPin1, HIGH);
delay(100);
}
}