{"id":2089,"date":"2019-08-03T08:54:55","date_gmt":"2019-08-03T05:54:55","guid":{"rendered":"http:\/\/ekfecg.mysch.gr\/?page_id=2089"},"modified":"2019-08-03T08:54:55","modified_gmt":"2019-08-03T05:54:55","slug":"esp32-bluetooth-classic-with-arduino-ide-getting-started","status":"publish","type":"page","link":"https:\/\/georgochr.sites.sch.gr\/?page_id=2089","title":{"rendered":"ESP32 Bluetooth Classic with Arduino IDE \u2013 Getting Started"},"content":{"rendered":"\n<p>he ESP32 comes with Wi-Fi, Bluetooth Low Energy and Bluetooth \nClassic. In this tutorial, you\u2019ll learn how to use ESP32 Bluetooth \nClassic with Arduino IDE to exchange data between an ESP32 and an \nAndroid smartphone.<br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-Bluetooth-Classic-Arduino-IDE-BT.jpg?w=813&amp;ssl=1\" alt=\"ESP32 Bluetooth Classic and Android Smartphone BT\" class=\"wp-image-85219\"\/><\/figure>\n\n\n\n<p>We\u2019ll control an ESP32 output, and send sensor readings to an Android smartphone using Bluetooth Classic.<\/p>\n\n\n\n<p><strong>Note: <\/strong> this project is <strong>only<\/strong> compatible with Android smartphones.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Watch the Video Tutorial<\/h2>\n\n\n\n<p>You can watch the video tutorial or keep reading this page for the written instructions.<br><\/p>\n\n\n\n<figure><iframe src=\"https:\/\/www.youtube.com\/embed\/RStncO3zb8g?rel=0\" allowfullscreen=\"\" width=\"720\" height=\"405\"><\/iframe><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Bluetooth Classic with ESP32<\/h2>\n\n\n\n<p>At the moment, using Bluetooth Classic is much more simpler than <a href=\"https:\/\/randomnerdtutorials.com\/esp32-bluetooth-low-energy-ble-arduino-ide\/\">Bluetooth Low Energy<\/a>. If you\u2019ve already programmed an Arduino with a <a href=\"https:\/\/makeradvisor.com\/tools\/bluetooth-module-hc-06\/\">Bluetooth module like the HC-06<\/a>, this is very similar. It uses the standard serial protocol and functions.<br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/arduino-hc-05.jpg?w=813&amp;ssl=1\" alt=\"hc-05 bluetooth module arduino ESP32\" class=\"wp-image-85081\"\/><\/figure>\n\n\n\n<p>In this tutorial, we\u2019ll start by using an example that comes with the\n Arduino IDE. Then, we\u2019ll build a simple project to exchange data \nbetween the ESP32 and your Android smartphone.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>To follow this tutorial, you need the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32 DOIT DEVKIT V1 Board<\/a> (read <a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\">Best ESP32 development boards<\/a>)<\/li><li>Android Smartphone with Bluetooth<\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\">5mm LED<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">330 Ohm resistor<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-digital-temperature-sensor\/\" target=\"_blank\">DS18B20 temperature sensor<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">4.7k Ohm resistor<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a> <\/li><\/ul>\n\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&amp;utm_medium=post&amp;utm_campaign=post\" target=\"_blank\" rel=\"noreferrer noopener\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&amp;utm_medium=post&amp;utm_campaign=post\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=813&amp;ssl=1\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Bluetooth Terminal Application<\/h2>\n\n\n\n<p>To proceed with this tutorial, you need a Bluetooth Terminal application installed in your smartphone.<\/p>\n\n\n\n<p>We recommend using the Android app \u201c<a href=\"https:\/\/play.google.com\/store\/apps\/details?id=de.kai_morich.serial_bluetooth_terminal&amp;hl=en\" target=\"_blank\" rel=\"noreferrer noopener\">Serial Bluetooth Terminal<\/a>\u201d available in the Play Store.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/Bluetooth_Serial_app.png?w=813&amp;ssl=1\" alt=\"bluetooth serial application esp32\" class=\"wp-image-85082\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Serial to Serial Bluetooth<\/h2>\n\n\n\n<p>We\u2019ll program the ESP32 using Arduino IDE, so make sure you have the ESP32 add-on installed before proceeding:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong><a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\" target=\"_blank\">Windows:<\/a><\/strong><a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\" target=\"_blank\"> instructions \u2013 ESP32 Board in Arduino IDE<\/a><\/li><li><strong><a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-mac-and-linux-instructions\/\" target=\"_blank\">Mac and Linux: <\/a><\/strong><a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-mac-and-linux-instructions\/\" target=\"_blank\"> instructions \u2013 ESP32 Board in Arduino IDE<\/a><\/li><\/ul>\n\n\n\n<p>Open your Arduino IDE, and go to <strong>File <\/strong>&gt; <strong>Examples <\/strong>&gt; <strong>BluetoothSerial <\/strong>&gt; <strong>SerialtoSerialBT<\/strong>.<\/p>\n\n\n\n<p>The following code should load.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/This example code is in the Public Domain (or CC0 licensed, at your option.)\n\/\/By Evandro Copercini - 2018\n\/\/\n\/\/This example creates a bridge between Serial and Classical Bluetooth (SPP)\n\/\/and also demonstrate that SerialBT have the same functionalities of a normal Serial\n\n#include \"BluetoothSerial.h\"\n\n#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)\n#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it\n#endif\n\nBluetoothSerial SerialBT;\n\nvoid setup() {\n  Serial.begin(115200);\n  SerialBT.begin(\"ESP32test\"); \/\/Bluetooth device name\n  Serial.println(\"The device started, now you can pair it with bluetooth!\");\n}\n\nvoid loop() {\n  if (Serial.available()) {\n    SerialBT.write(Serial.read());\n  }\n  if (SerialBT.available()) {\n    Serial.write(SerialBT.read());\n  }\n  delay(20);\n}\n<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-Course\/raw\/master\/code\/Bluetooth_Classic_Example\/Bluetooth_Classic_Example.ino\" target=\"_blank\" rel=\"noreferrer noopener\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>This code establishes a two-way serial Bluetooth communication between two devices.<\/p>\n\n\n\n<p>The code starts by including the BluetoothSerial library.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"BluetoothSerial.h\"<\/code><\/pre>\n\n\n\n<p>The next three lines check if Bluetooth is properly enabled.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)\n#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it\n#endif<\/code><\/pre>\n\n\n\n<p>Then, create an instance of BluetoothSerial called SerialBT:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BluetoothSerial SerialBT;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the setup() initialize a serial communication at a baud rate of 115200.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Initialize the Bluetooth serial device and pass as an argument the Bluetooth Device name. By default it\u2019s called <strong>ESP32test<\/strong> but you can rename it and give it a unique name.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SerialBT.begin(\"ESP32test\"); \/\/Bluetooth device name<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>In the loop(), send and receive data via Bluetooth Serial.<\/p>\n\n\n\n<p>In the first if statement, we check if there are bytes being received\n in the serial port. If there are, send that information via Bluetooth \nto the connected device.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (Serial.available()) {\n  SerialBT.write(Serial.read());\n}<\/code><\/pre>\n\n\n\n<p>SerialBT.write() sends data using bluetooth serial.<\/p>\n\n\n\n<p>Serial.read() returns the data received in the serial port.<\/p>\n\n\n\n<p>The next if statement, checks if there are bytes available to read in\n the Bluetooth Serial port. If there are, we\u2019ll write those bytes in the\n Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (SerialBT.available()) {\n  Serial.write(SerialBT.read());\n}<\/code><\/pre>\n\n\n\n<p>It will be easier to understand exactly how this sketch works in the demonstration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the Code<\/h2>\n\n\n\n<p>Upload the previous code to the ESP32. Make sure you have the right board and COM port selected.<\/p>\n\n\n\n<p>After uploading the code, open the Serial Monitor at a baud rate of 115200. Press the ESP32 Enable button.<\/p>\n\n\n\n<p>After a few seconds, you should get a message saying: \u201c<em>The device started, now you can pair it with bluetooth!<\/em>\u201d.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/device_started_bluetooth-esp32.png?w=813&amp;ssl=1\" alt=\"ESP32 Bluetooth Classic and Android Smartphone Demonstration\" class=\"wp-image-85083\"\/><\/figure>\n\n\n\n<p>Go to your smartphone and open the \u201c<strong>Serial Bluetooth Terminal<\/strong>\u201d app. Make sure you\u2019ve enable your smartphone\u2019s Bluetooth.<\/p>\n\n\n\n<p>To connect to the ESP32 for the first time, you need to pair a new device.<br>Go to&nbsp;<strong>Devices.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/devices.png?w=813&amp;ssl=1\" alt=\"ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal \" class=\"wp-image-85086\"\/><\/figure>\n\n\n\n<p>Click the settings icon, and select&nbsp;<strong>Pair new device<\/strong>. You should get a list with the available Bluetooth devices, including the <strong>ESP32test<\/strong>. Pair with the <strong>ESP32test<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/pair-new-device-bluetooth-esp32.png?w=813&amp;ssl=1\" alt=\"ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal Pair new device\" class=\"wp-image-85087\"\/><\/figure>\n\n\n\n<p>Then, go back to the Serial Bluetooth Terminal. Click the icon at the top to connect to the ESP32. You should get a \u201c<strong>Connected\u201d<\/strong> message.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/connected-to-esp32.png?w=813&amp;ssl=1\" alt=\"ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal connected\" class=\"wp-image-85090\"\/><\/figure>\n\n\n\n<p>After that, type something in the Serial Bluetooth Terminal app. For example, \u201c<strong>Hello<\/strong>\u201d.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/send-hello-bluetooth-esp32.png?w=813&amp;ssl=1\" alt=\"ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal Demonstration\" class=\"wp-image-85091\"\/><\/figure>\n\n\n\n<p>You should instantly receive that message in the Arduino IDE Serial Monitor.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/hello-message-received-bluetooth.png?w=813&amp;ssl=1\" alt=\"ESP32 Bluetooth Classic demonstration serial monitor Arduino IDE\" class=\"wp-image-85092\"\/><\/figure>\n\n\n\n<p>You can also exchange data between your Serial Monitor and your \nsmartphone. Type something in the Serial Monitor top bar and press the \u201c<strong>Send<\/strong>\u201d button.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/send-message-via-bluetooth-1.png?w=813&amp;ssl=1\" alt=\"ESP32 Bluetooth Classic demonstration serial monitor Arduino IDE\" class=\"wp-image-85093\"\/><\/figure>\n\n\n\n<p>You should instantly receive that message in the Serial Bluetooth Terminal App.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/receive-message-bluetooth-application-esp32.png?w=813&amp;ssl=1\" alt=\"ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal Demonstration\" class=\"wp-image-85094\"\/><\/figure>\n\n\n\n<figure><iframe src=\"https:\/\/tpc.googlesyndication.com\/safeframe\/1-0-35\/html\/container.html\" width=\"0\" height=\"175\"><\/iframe><\/figure>\n\n\n\n<p>\n        \n        \n          Report this ad\n        \n        \n          <a href=\"https:\/\/www.mediavine.com\" target=\"_blank\" rel=\"noreferrer noopener\">\n            \n          <\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Exchange Data using Bluetooth Serial<\/h2>\n\n\n\n<p>Now that you know how to exchange data using Bluetooth Serial, you \ncan modify the previous sketch to make something useful. For example, \ncontrol the ESP32 outputs when you receive a certain message, or send \ndata to your smartphone like sensor readings.<\/p>\n\n\n\n<p>The project we\u2019ll build sends temperature readings every 10 seconds to your smartphone. We\u2019ll be using the <a href=\"https:\/\/randomnerdtutorials.com\/esp32-with-multiple-ds18b20-temperature-sensors\/\">DS18B20 temperature sensor<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/dallas_temperature_sensor.jpg?w=813&amp;ssl=1\" alt=\"ds18b20 temperature sensor\" class=\"wp-image-85095\"\/><\/figure>\n\n\n\n<p>Through the Android app, we\u2019ll send messages to control an ESP32 output. When the ESP32 receives the <strong>led_on<\/strong> message, we\u2019ll turn the GPIO on, when it receives the <strong>led_off<\/strong> message, we\u2019ll turn the GPIO off.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic<\/h3>\n\n\n\n<p>Before proceeding with this project, assemble the circuit by following the next schematic diagram.<\/p>\n\n\n\n<p>Connect an LED to GPIO25, and connect the DS18B20 data pin to GPIO32.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/bluetooth_classic_project_example-Copy.jpg?w=813&amp;ssl=1\" alt=\"Bluetooth Classic ESP32 communication\" class=\"wp-image-85096\"\/><\/figure>\n\n\n\n<p><strong>Recommended reading:<\/strong><a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\"> ESP32 Pinout Reference: Which GPIO pins should you use?<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<p>To work with the DS18B20 temperature sensor, you need to install the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/PaulStoffregen\/OneWire\" target=\"_blank\">One Wire library by Paul Stoffregen<\/a> and the <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/milesburton\/Arduino-Temperature-Control-Library\" target=\"_blank\">Dallas Temperature library<\/a>. Follow the next instructions to install these libraries, if you haven\u2019t already.<\/p>\n\n\n\n<p><strong>One Wire library<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/PaulStoffregen\/OneWire\/archive\/master.zip\" target=\"_blank\" rel=\"noreferrer noopener\">Click here to download the One Wire&nbsp;library<\/a>. You should have a .zip folder in your Downloads<\/li><li>Unzip the&nbsp;<em>.zip<\/em>&nbsp;folder and you should get&nbsp;<strong>OneWire-master&nbsp;<\/strong>folder<\/li><li>Rename your folder from&nbsp;<del><strong>OneWire-master<\/strong><\/del>&nbsp;to<strong>&nbsp;OneWire<\/strong><\/li><li>Move the&nbsp;<strong>OneWire&nbsp;<\/strong>folder to your Arduino IDE installation&nbsp;<strong>libraries&nbsp;<\/strong>folder<\/li><li>Finally, re-open your Arduino IDE<\/li><\/ol>\n\n\n\n<p><strong>Dallas Temperature library<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/milesburton\/Arduino-Temperature-Control-Library\/archive\/master.zip\" target=\"_blank\" rel=\"noreferrer noopener\">Click here to download the Dallas Temperature&nbsp;library<\/a>. You should have a .zip folder in your Downloads<\/li><li>Unzip the&nbsp;<em>.zip<\/em>&nbsp;folder and you should get&nbsp;<strong>Arduino-Temperature-Control-Library-master&nbsp;<\/strong>folder<\/li><li>Rename your folder from&nbsp;<del><strong>Arduino-Temperature-Control-Library-master<\/strong><\/del>&nbsp;to&nbsp;<strong>DallasTemperature<\/strong><\/li><li>Move the&nbsp;<strong>DallasTemperature<\/strong>folder to your Arduino IDE installation&nbsp;<strong>libraries&nbsp;<\/strong>folder<\/li><li>Finally, re-open your Arduino IDE<\/li><\/ol>\n\n\n\n<p>After assembling the circuit and installing the necessary libraries, copy the following sketch to your Arduino IDE.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*********\n  Rui Santos\n  Complete project details at https:\/\/randomnerdtutorials.com\n*********\/\n\n\/\/ Load libraries\n#include \"BluetoothSerial.h\"\n#include &lt;OneWire.h>\n#include &lt;DallasTemperature.h>\n\n\/\/ Check if Bluetooth configs are enabled\n#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)\n#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it\n#endif\n\n\/\/ Bluetooth Serial object\nBluetoothSerial SerialBT;\n\n\/\/ GPIO where LED is connected to\nconst int ledPin =  25;\n\n\/\/ GPIO where the DS18B20 is connected to\nconst int oneWireBus = 32;          \n\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(oneWireBus);\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);\n\n\/\/ Handle received and sent messages\nString message = \"\";\nchar incomingChar;\nString temperatureString = \"\";\n\n\/\/ Timer: auxiliar variables\nunsigned long previousMillis = 0;    \/\/ Stores last time temperature was published\nconst long interval = 10000;         \/\/ interval at which to publish sensor readings\n\nvoid setup() {\n  pinMode(ledPin, OUTPUT);\n  Serial.begin(115200);\n  \/\/ Bluetooth device name\n  SerialBT.begin(\"ESP32\");\n  Serial.println(\"The device started, now you can pair it with bluetooth!\");\n}\n\nvoid loop() {\n  unsigned long currentMillis = millis();\n  \/\/ Send temperature readings\n  if (currentMillis - previousMillis >= interval){\n    previousMillis = currentMillis;\n    sensors.requestTemperatures(); \n    temperatureString = String(sensors.getTempCByIndex(0)) + \"C  \" +  String(sensors.getTempFByIndex(0)) + \"F\";\n    SerialBT.println(temperatureString); \n  }\n  \/\/ Read received messages (LED control command)\n  if (SerialBT.available()){\n    char incomingChar = SerialBT.read();\n    if (incomingChar != '\\n'){\n      message += String(incomingChar);\n    }\n    else{\n      message = \"\";\n    }\n    Serial.write(incomingChar);  \n  }\n  \/\/ Check received message and control output accordingly\n  if (message ==\"led_on\"){\n    digitalWrite(ledPin, HIGH);\n  }\n  else if (message ==\"led_off\"){\n    digitalWrite(ledPin, LOW);\n  }\n  delay(20);\n}\n<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-Course\/raw\/master\/code\/Bluetooth_Classic_Project\/Bluetooth_Classic_Project.ino\" target=\"_blank\" rel=\"noreferrer noopener\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Let\u2019s take a quick look at the code and see how it works.<\/p>\n\n\n\n<p>Start by including the necessary libraries. The BluetoothSerial library for Bluetooth, and the OneWire and DallasTemperature for the DS18B20 temperature sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"BluetoothSerial.h\"\n#include &lt;OneWire.h>\n#include &lt;DallasTemperature.h><\/code><\/pre>\n\n\n\n<p>Create a BluetoothSerial instance called SerialBT.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BluetoothSerial SerialBT;<\/code><\/pre>\n\n\n\n<p>Create a variable called ledPin to hold the GPIO you want to control. In this case, GPIO25 has an LED connected.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const int ledPin =  25;<\/code><\/pre>\n\n\n\n<p>Define the DS18B20 sensor pin and create objects to make it work. The temperature sensor is connected to GPIO32.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ GPIO where the DS18B20 is connected to\nconst int oneWireBus = 32;          \n\/\/ Setup a oneWire instance to communicate with any OneWire devices\nOneWire oneWire(oneWireBus);\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);<\/code><\/pre>\n\n\n\n<p>Create an empty string called message to store the received messages.<\/p>\n\n\n\n<figure><iframe src=\"https:\/\/tpc.googlesyndication.com\/safeframe\/1-0-35\/html\/container.html\" width=\"300\" height=\"250\"><\/iframe><\/figure>\n\n\n\n<p>\n        \n        \n          Report this ad\n        \n        \n          <a href=\"https:\/\/www.mediavine.com\" target=\"_blank\" rel=\"noreferrer noopener\">\n            \n          <\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String message = \"\";<\/code><\/pre>\n\n\n\n<p>Create a char variable called incomingChar to save the characters coming via Bluetooth Serial.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char incomingChar;<\/code><\/pre>\n\n\n\n<p>The temperatureString variable holds the temperature readings to be sent via Bluetooth.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String temperatureString = \"\";<\/code><\/pre>\n\n\n\n<p>Create auxiliar timer variables to send readings every 10 seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unsigned long previousMillis = 0;   \/\/ Stores last time temperature was published\nconst long interval = 10000;        \/\/ interval at which to publish sensor readings<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the setup(), set the ledPin as an output.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pinMode(ledPin, OUTPUT);<\/code><\/pre>\n\n\n\n<p>Initialize the ESP32 as a bluetooth device with the \u201cESP32\u201d name.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SerialBT.begin(\"ESP32\"); \/\/Bluetooth device name <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>In the&nbsp;loop(), send the temperature readings, read the received messages and execute actions accordingly.<\/p>\n\n\n\n<p>The following snippet of code, checks if 10 seconds have passed since\n the last reading. If it\u2019s time to send a new reading, we get the latest\n temperature and save it in Celsius and Fahrenheit in the temperatureString variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unsigned long currentMillis = millis();\nif (currentMillis - previousMillis >= interval) {\n    previousMillis = currentMillis;\n    sensors.requestTemperatures(); \n    temperatureString = \" \" + String(sensors.getTempCByIndex(0)) + \"C  \" +  String(sensors.getTempFByIndex(0)) + \"F\";<\/code><\/pre>\n\n\n\n<p>Then, to send the temperatureString via bluetooth, use SerialBT.println().<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SerialBT.println(temperatureString);  <\/code><\/pre>\n\n\n\n<p>The next if statement reads incoming messages. When you receive \nmessages via serial, you receive a character at a time. You know that \nthe message ended, when you receive <strong>\\n<\/strong>.<\/p>\n\n\n\n<p>So, we check if there\u2019s data available in the Bluetooth serial port.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (SerialBT.available()) {<\/code><\/pre>\n\n\n\n<p>If there is, we\u2019ll save the characters in the incomingChar variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char incomingChar = SerialBT.read();<\/code><\/pre>\n\n\n\n<p>If the incomingChar is different than \\n, we\u2019ll concatenate that char character to our message.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (incomingChar!= '\\n'){\n  message += String(incomingChar);\n}<\/code><\/pre>\n\n\n\n<p>When we\u2019re finished reading the characters, we clear the message variable. Otherwise all received messages would be appended to each other.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>message = \"\";<\/code><\/pre>\n\n\n\n<p>After that, we have two if statements to check the content of the message. If the message is <strong>led_on<\/strong>, the LED turns on.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (message ==\"led_on\"){\n   digitalWrite(ledPin, HIGH);\n}<\/code><\/pre>\n\n\n\n<p>If the message is <strong>led_off<\/strong>, the LED turns off.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>else if (message ==\"led_off\"){\n  digitalWrite(ledPin, LOW);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the Project<\/h2>\n\n\n\n<p>Upload the previous sketch to your \nESP32 board. Then, open the Serial Monitor, and press the ESP32 Enable \nbutton. When you receive the following message, you can go to your \nsmartphone and connect with the ESP32.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/device_started_bluetooth-esp32-1.png?w=813&amp;ssl=1\" alt=\"Testing ESP32 Bluetooth Classic Project Code Example\" class=\"wp-image-85097\"\/><\/figure>\n\n\n\n<p>Then, you can write the<strong>\u201cled_on\u201d<\/strong>&nbsp;and&nbsp;<strong>\u201cled_off\u201d&nbsp;<\/strong>messages to control the LED.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/control-led-on.jpg?w=813&amp;ssl=1\" alt=\"Testing ESP32 Bluetooth Classic Project Code Example\" class=\"wp-image-85098\"\/><\/figure>\n\n\n\n<p>The application has several buttons in which you can save default messages. For example, you can associate&nbsp;<strong>M1&nbsp;<\/strong>with the \u201c<strong>led_on<\/strong>\u201d message, and&nbsp;<strong>M2&nbsp;<\/strong>with the \u201c<strong>led_off<\/strong>\u201d message.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/default-messages.png?w=813&amp;ssl=1\" alt=\"\" class=\"wp-image-85099\"\/><\/figure>\n\n\n\n<p>Now, you are able to control the ESP32 GPIOs.<\/p>\n\n\n\n<p>At the same time, you should be receiving the temperature readings every 10 seconds.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/bluetooth-receive-sensor-readings-esp32.jpg?w=813&amp;ssl=1\" alt=\"\" class=\"wp-image-85100\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In summary, the ESP32 supports BLE \nand Bluetooth Classic. Using Bluetooth Classic is as simple as using \nserial communication and its functions.<\/p>\n\n\n\n<p>If you want to learn how to use BLE with the ESP32, you can read our guide:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-bluetooth-low-energy-ble-arduino-ide\/\">Getting Started with ESP32 Bluetooth Low Energy (BLE) on Arduino IDE<\/a><\/li><\/ul>\n\n\n\n<p>We hope you\u2019ve found this tutorial useful. For more projects with the ESP32 you can check our project\u2019s compilation: <a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">20+ ESP32 Projects and Tutorials<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>he ESP32 comes with Wi-Fi, Bluetooth Low Energy and Bluetooth Classic. In this tutorial, you\u2019ll learn how to use ESP32 Bluetooth Classic with Arduino IDE to exchange data between an ESP32 and an Android smartphone. We\u2019ll control an ESP32 output, and send sensor readings to an Android smartphone using Bluetooth Classic. Note: this project is [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":2073,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-2089","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/georgochr.sites.sch.gr\/index.php?rest_route=\/wp\/v2\/pages\/2089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/georgochr.sites.sch.gr\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/georgochr.sites.sch.gr\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/georgochr.sites.sch.gr\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/georgochr.sites.sch.gr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2089"}],"version-history":[{"count":0,"href":"https:\/\/georgochr.sites.sch.gr\/index.php?rest_route=\/wp\/v2\/pages\/2089\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/georgochr.sites.sch.gr\/index.php?rest_route=\/wp\/v2\/pages\/2073"}],"wp:attachment":[{"href":"https:\/\/georgochr.sites.sch.gr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}