Friday, August 2, 2024

ESP32 Bluetooth Classic part 2

For an index to all my stories click this text.

This is the second story on how to use Bluetooth Classic with the ESP32. The first story covered how to send data from an Android App to the ESP32. You can re-read that story here:
http://lucstechblog.blogspot.com/2024/07/esp32-bluetooth-classic-part-1.html

This story expands the previous one and shows how to display sensor data on your phone's screen. In the first story I build an Android App with MIT's App Inventor. And I am going to modify that app for receiving data from the ESP32.

The hardware

As this is a tutorial and a base for using Bluetooth with the ESP32 I am going to attach two sensors to the ESP32 for generating data. The first one is a simple button and the second one is a Dallas DS18B20 digital thermometer chip. You can easily replace these with any other sensor you might have laying around.

For testing purposes I always build my projects on a breadboard and this is how that looks.



In the previous story I attached a led to D5 with a current delimiting resistor and that is still there. Use this for testing if sending a command from Android to the ESP is still working.

The Dallas DS18B20 is attached to a pull-up resistor and to D19 at the ESP32

Then there is a simple push-button which is connected to D21. I did not use a pull-up resistor on the button but will use the internal pull-up resisitor from the ESP32.

The App Inventor App


As we want to display the values of the button and the digital thermometer we need to add a field to our app's screen. I am just adding one field and the data will be displayed on alternate turns in that field.

So I added a new Label to the screen called Label2



On the left side of the screen click on Label and drag that to your screen. In my sample I put the new Label just above my ridiculous copyright label. The picture shows it as the rectangle.

Next step is to click on "Sensors" at the right side of the screen and click on the Clock sensor and drag that into your screen. It will then appear at the bottom as a non-visible component.
The clock is standard set to fire a signal every second. You can change that to your liking by clicking on the clock icon and then change the properties section at the right side of the designer (not seen in the above picture).

Now move over to the blocks section.



The complete blocks are almost identical to those of the previous story. The only modification is a new block used for receiving data.



This block is executed every second (remember, the property from the clock). First the bluetooth connection is tested and then the text in Label2 is set to the received information.

That is all.
Please delve deeper into App Inventor if you want to make alterations or add more functionality to this simple app. A bit of studying on App Inventor will show that it is fun to play with and easy to develop app's with.

Build the App. Download it to your computer and then transfer it over USB to your phone and install it.

The ESP32 Program

Basically this is the same program as the one from the previous story with some alterations. Let's have a look at the whole program.

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

#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
#endif

BluetoothSerial SerialBT;

#define ONE_WIRE_BUS 19

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature dallas(&oneWire);

String Charrcvd;
String text;
int led=5;
int button=21;

void setup() 
{
  Serial.begin(115200);
  SerialBT.begin("ESP32BTtest"); //Bluetooth device name
  Serial.println("Bluetooth started, now pair your phone");

  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop() 
{
   /* 
    * -----------------------------------
    * Bluetooth send part 
    * -----------------------------------
    */
   if(digitalRead(button) == 1)
   {    
   dallas.requestTemperatures();
   SerialBT.print("Temp: ");
   SerialBT.println(dallas.getTempCByIndex(0));
   Serial.println(dallas.getTempCByIndex(0));
   delay(1000);
   }
   else
   {
    SerialBT.println("Button pressed");
    delay(1000);
    SerialBT.println(" ");
   }

  /* 
   *  ----------------------------------
   *  Bluetooth receive part
   *  ----------------------------------
   */
  if (SerialBT.available()) 
  {
    Charrcvd = SerialBT.readString();
       {
        text = text + Charrcvd;
       }
  }
    if (text != "")
      {
      Serial.println(text.substring(0,text.length()-1));
      Serial.println(text.length());
      if (text.substring(0,text.length()-1)== "On") 
        {
          Serial.println("Received: On");
          digitalWrite(led, HIGH);
        }
      if (text.substring(0,text.length()-1)== "Off") 
        {
          Serial.println("Received: Off");
          digitalWrite(led, LOW);
        }
      text = "";
      delay(20);
      }
}


I will not discuss the complete program as a large part is already covered in the previous story on Bluetooth Classic. So check that here
http://lucstechblog.blogspot.com/2024/07/esp32-bluetooth-classic-part-1.html

Let me just highlite some details.

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

These lines import the libraries that are needed for the Dallas DS18B20 temperature sensor.

#define ONE_WIRE_BUS 19

The DS18B20 is attached to IO pin 19.

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature dallas(&oneWire);

An instance for the library is made and called "dallas"

In the loop these are the important parts:

   if(digitalRead(button) == 1)
   {    
   dallas.requestTemperatures();
   SerialBT.print("Temp: ");
   SerialBT.println(dallas.getTempCByIndex(0));
   Serial.println(dallas.getTempCByIndex(0));
   delay(1000);
   }


When the button is not pressed it's line is HIGH and then the Dallas DS18B20 sensor is checked and send over Bluetooth. The important command is SerialBT.print() This is the actual command to send data over Bluetooth.

   else
   {
    SerialBT.println("Button pressed");
    delay(1000);
    SerialBT.println(" ");
   }


If the button is pressed the command SerialBT.println() sends the line "Button pressed" over Bluetooth.

So actually sending data over Bluetooth is pretty easy.
SerialBT.print() sends the data over Bluetooth but does not finish the text line. SerialBT.println() sends and ends the text line. This is the same behaviour as sending text to the Serial Monitor with Serial.print() and Serial.println().

The result

And here is how this works out in real life.









 

 

 

 

 

 

 

 

Pressing the green "Led on" and red "Led off" buttons should still work and in the mean time the temperature and button is checked by the ESP32 and displayed in the app.


The next steps.


This should be enough to get you going. There are plenty of projects that can benefit from Bluetooth communication. Think about projects in places where there is no wifi available and you still want to check data. A bycicle computer is a perfect example. And I am sure you can come up with some practical camping projects.

Till next time
have fun

Luc Volders