0% found this document useful (0 votes)
324 views

ESP32 Bluetooth Classic With Arduino IDE - Getting Started Random Nerd Tutorials

The document describes how to set up Bluetooth Classic communication on an ESP32 using the Arduino IDE. It includes code to initialize Bluetooth Serial communication using the BluetoothSerial library, read temperature sensor data using the DallasTemperature library, and control an LED based on commands received over Bluetooth. Temperature readings are sent every 10 seconds over Bluetooth and commands like "led_on" can toggle the LED.

Uploaded by

Yusuf Maulana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
324 views

ESP32 Bluetooth Classic With Arduino IDE - Getting Started Random Nerd Tutorials

The document describes how to set up Bluetooth Classic communication on an ESP32 using the Arduino IDE. It includes code to initialize Bluetooth Serial communication using the BluetoothSerial library, read temperature sensor data using the DallasTemperature library, and control an LED based on commands received over Bluetooth. Temperature readings are sent every 10 seconds over Bluetooth and commands like "led_on" can toggle the LED.

Uploaded by

Yusuf Maulana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.

com/esp32-bluetooth-classic-arduino-ide/

1 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

2 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

3 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

4 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

//This example code is in the Public Domain (or CC0 li


//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Cla
//and also demonstrate that SerialBT have the same fun

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLU


#error Bluetooth is not enabled! Please run `make menu
#endif

BluetoothSerial SerialBT;

void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair
}

void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}

5 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

BluetoothSerial

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLU


#error Bluetooth is not enabled! Please run `make menu
#endif

BluetoothSerial
SerialBT

BluetoothSerial SerialBT;

setup()

6 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

ESP32test

SerialBT.begin("ESP32test"); //Bluetooth device name

loop()

if (Serial.available()) {
SerialBT.write(Serial.read());
}

SerialBT.write()

Serial.read()

7 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

if (SerialBT.available()) {
Serial.write(SerialBT.read());
}

8 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

9 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

10 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

11 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

led_on

led_off

12 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

13 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

/*********
Rui Santos
Complete project details at https://randomnerdtutori
*********/

14 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

#include <OneWire.h>
#include <DallasTemperature.h>

// Check if Bluetooth configs are enabled


#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLU
#error Bluetooth is not enabled! Please run `make menu
#endif

// Bluetooth Serial object


BluetoothSerial SerialBT;

// GPIO where LED is connected to


const int ledPin = 25;

// GPIO where the DS18B20 is connected to


const int oneWireBus = 32;
// Setup a oneWire instance to communicate with any On
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature se
DallasTemperature sensors(&oneWire);

// Handle received and sent messages


String message = "";
char incomingChar;
String temperatureString = "";

// Timer: auxiliar variables


unsigned long previousMillis = 0;
const long interval = 10000;

void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
// Bluetooth device name
SerialBT.begin("ESP32");

15 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

void loop() {
unsigned long currentMillis = millis
// Send temperature readings
if (currentMillis - previousMillis
previousMillis = currentMillis;
sensors.requestTemperatures();
temperatureString = String(sensors
SerialBT.println(temperatureString
}
// Read received messages (LED control command)
if (SerialBT.available()){
char incomingChar = SerialBT.read
if (incomingChar != '\n'){
message += String(incomingChar
}
else{
message = "";
}
Serial.write(incomingChar);
}
// Check received message and control output accordi
if (message =="led_on"){
digitalWrite(ledPin, HIGH);
}
else if (message =="led_off"){

BluetoothSerial

16 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

#include "BluetoothSerial.h"
#include <OneWire.h>
#include <DallasTemperature.h>

BluetoothSerial
SerialBT

BluetoothSerial SerialBT;

ledPin

const int ledPin = 25;

// GPIO where the DS18B20 is connected to


const int oneWireBus = 32;
// Setup a oneWire instance to communicate with any On
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature se
DallasTemperature sensors(&oneWire);

17 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

String message = "";

incomingChar

char incomingChar;

temperatureString

String temperatureString = "";

unsigned long previousMillis = 0;


const long interval = 10000;

setup() ledPin

pinMode(ledPin, OUTPUT);

18 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

SerialBT.begin("ESP32"); //Bluetooth device name

loop()

temperatureString

unsigned long currentMillis = millis


if (currentMillis - previousMillis >=
previousMillis = currentMillis;
sensors.requestTemperatures();
temperatureString = " " + String

temperatureString

SerialBT.println()

SerialBT.println(temperatureString);

19 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

\n

if (SerialBT.available()) {

incomingChar

char incomingChar = SerialBT.read();

incomingChar
\n

if (incomingChar!= '\n'){
message += String(incomingChar);
}

message

message = "";

led_on

20 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

digitalWrite(ledPin, HIGH);
}

led_off

else if (message =="led_off"){


digitalWrite(ledPin, LOW);
}

21 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

22 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

23 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

24 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

25 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

26 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

27 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

28 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

29 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

30 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

31 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

32 of 33 1/12/2020, 14:16
ESP32 Bluetooth Classic with Arduino IDE - Getting Started | Rando... https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/

33 of 33 1/12/2020, 14:16

You might also like