📡 ESP32 Bluetooth connection in Arduino IDE

📡 ESP32 Bluetooth connection in Arduino IDE

Daniil Zhuk
The development of projects integrating data transfer via Bluetooth involves the transition to more modern ESP32 microcontrollers that support digital data exchange via Wi-Fi, Bluetooth, ADC ports, DAC, audio channels, SD card protocol, the possibility of energy-saving deep sleep mode and a number of other innovations.

These features make it possible to effectively use these microcontrollers in applications of the “Internet of Things” and projects of the “smart home” series in combination with the ARDUINO and Raspberry Pi modules.

A distinctive feature of ESP32 controllers is the presence of two integrated Bluetooth modules: standard Bluetooth and Bluetooth Low Energy (BLE). The BLE module consumes significantly less power than standard classic Bluetooth protocols. Energy savings are achieved due to the fact that information packets are sent periodically, as needed.

This type of data exchange does not support file sharing. For example, music or video cannot be transferred.

BLE modules can be used to identify device types, exchange information with smartwatches, GPS trackers, fitness bracelets and other applications that require the rapid exchange of short information packets that reflect the change in monitored parameters.

For example, it is possible to display the battery charge level, the position of a point on the route in the navigator, and monitor physiological data.

Accordingly, standard Bluetooth performs file sharing functions, and represents one of the Bluetooth SPP (Serial Port Protocol) options, supporting work with most existing devices with a standard serial protocol.

SHARING DATA BETWEEN SMARTPHONE AND ESP32 THROUGH BLUETOOTH
Consider the exchange of data between the smartphone and the ESP32 controller via any Bluetooth terminal program, with which we will send data to the microcontroller.

Our task is to establish a wireless connection between the smartphone and the ESP32 and display the read data, turning the LED on and off.

In this case, the reception of a logical unit is displayed by turning on the LED, and the reception of a logical zero - by switching off. For development, we will use the already familiar Arduino IDE. We will start the program by adding the BluetoothSerial library file, which switches the module to Bluetooth SPP mode.
#include "BluetoothSerial.h"

Next - we make the choice of the object for the transfer of data:

BluetoothSerial ESP_BT;

The next step is to set the speed of serial data transmission (we will have 9600 baud) and initialize the Bluetooth signal with the name that the smartphone will have when the connection is established. The port to which the LED is connected is assigned as an output for our device.

void setup() {
Serial.begin(9600);
ESP_BT.begin("ESP32_LED_Control");
Serial.println("Bluetooth Device is Ready to Pair");
pinMode (LED_BUILTIN, OUTPUT);
}

Checking the input data from the Bluetooth module will be organized in the body of an infinite void loop. In this case, the detection of incoming data is stored in a given variable of the input signal.

In addition, the variable value is printed to the serial monitor, for cross-checking information received from ARDUINO.

if (ESP_BT.available())
{
incoming = ESP_BT.read();
Serial.print("Received:"); Serial.println(incoming);

Thus, the obtained data stored in the input variable is available for comparison with the expected value and the subsequent command implementation. Note that the format of the value sent from Bluetooth is “char”, while ARDUINO reads the decimal value of the symbol.

In this case, for the char ‘0’, the decimal value is 48, for the char ‘1’ - 49 (see the ASCII code table).

The next section of the code compares the input variable with the values of 48 and 49 (checking the logical levels 0 and 1) and turns on the LED, provided that the unit is received. In the case of a logical zero, the LED remains off.

if (incoming == 49)
{
digitalWrite(LED_BUILTIN, HIGH);
ESP_BT.println("LED turned ON");
}
 
if (incoming == 48)
{
digitalWrite(LED_BUILTIN, LOW);
ESP_BT.println("LED turned OFF");
}

Check the work of the Bluetooth module ESP32. We select the board and the port, after connecting the module to the computer on which the ARDUINO IDE is installed. After waiting for the code to load, we launch the serial monitor in debug mode and then search for a Bluetooth device with the name ESP32_LED_Control in the Bluetooth settings of the smartphone.

By opening any Bluetooth terminal application, we will be able to enter and send a unit. You can find such applications on Google Play or the AppStore for the query “terminal bluetooth”.

After receiving the information, the ESP32 module energizes the LED and issues a confirmation message. In parallel, in the decimal system, we can view the received ESP32 Bluetooth data displayed on the serial monitor.

 By the same principle, the LED is also turned off when sending a zero from a mobile application.

Here is the full program code:

#include "BluetoothSerial.h" 
 
BluetoothSerial ESP_BT; 
 
int incoming;
int LED_BUILTIN = 2;
 
void setup() {
  Serial.begin(9600);
  ESP_BT.begin("ESP32_LED_Control"); 
  Serial.println("Bluetooth Device is Ready to Pair");  
 
  pinMode (LED_BUILTIN, OUTPUT);
}
 
void loop() {
 
  if (ESP_BT.available()) 
  {
    incoming = ESP_BT.read(); 
    Serial.print("Received:"); Serial.println(incoming);
 
    if (incoming == 49)  
        {
        digitalWrite(LED_BUILTIN, HIGH);
        ESP_BT.println("LED turned ON");
        }
 
    if (incoming == 48)  
        {
        digitalWrite(LED_BUILTIN, LOW);
        ESP_BT.println("LED turned OFF");
        }    
  }
  delay(20);
}

Add a comment

* Comments must be approved before being displayed.