Friday, July 19, 2024

ESP32 Bluetooth Classic part 1

For an index to all my stories click this text.

For communication with an ESP32 we mostly use Wifi. Wifi is easy to use and almost everywhere available. However there are times when you do not want or can use wifi. An example is when you are walking or biking in a rural environment. When this is the case you can communicate through Bluetooth with the ESP32.

Two kinds of Bluetooth.

There are two kinds of Bluetooth. There is normal Bluetooth called Classic Bluetooth and there is Bluetooth Low Energy (BLE for short). Classic Bluetooth was the first and is still used. With Classic Bluetooth you can send audio and large amounts of data (like music, apps, photo's etc) to another Bluetooth device. So when you are pairing your phone with headphones you will be using Classic Bluetooth. This uses more power as BLE.
Bluetooth Low Energy (BLE) uses far less energy (duh !!) and can transmit data over a longer range. The setback is that it can only send small amounts of data so no music or photo's. But it is excellent for sending sensor data etc.

This story focusses on sending data from your Android Phone to an ESP32 using Classic Bluetooth.

MIT's App Inventor

If you want to develop App's for your Android Phone in a quick and easy to learn way I recommend MIT's App Inventor. I have used this many times in previous stories dating back to my first Android App in 2015: 
http://lucstechblog.blogspot.com/2015/04/wijnmaker-app-many-years-ago-i-started.html

More stories can be found on my index page:
http://lucstechblog.blogspot.com/p/index-of-my-stories.html
Check them out for learning more about building your own apps. You can go straight to MIT's App Inventor by this link: https://appinventor.mit.edu/

I must admit that the last year I have been studying different ways to build apps for your phone. The first one is by building Javascript programs and turning them into an App. Which has been covered on this weblog in previous stories. And there is B4X which is a totally different programming language that turns a Basic-like scripting language into native Java. This last one is much more difficult to learn for beginners but produces Apps which are about 1/3 the size of an App made with MIT's App Inventor. I will be covering B4X in a future story. If you are curious though, check it out at https://www.b4x.com/

For now we stick to MIT's App Inventor.

The example

I am showing a simple setup. A led connected to the ESP32 and a simple App with a button to choose the available Bluetooth device and two buttons to set the led On and Off.

The hardware setup.

Nothing Special here. 


Just the Doit ESP32 Devkit connected to a led (with a current delimiting resistor of 220 Ohm) on pin D5.

Designing the App

I can not give a complete tutorial on App Inventor here as it is to comprehensive. So I'll just give a brief discussion on how it is done. If you need to know more look at my other stories on App Inventor or follow the tutorials on their website and the many examples that can be found on Youtube etc. You can find App Inventor here: https://appinventor.mit.edu/

Sign in with your google account and start a new project.

In the Designer set the Background color of the screen to Blue.



Add two buttons. Set the text to the first button to: Led on and make the text white. Set the background to green. The text of the second button is set to Led Off and the background is set to Red. Size them to your liking.

At the top of the page a ListPicker element is created with black Textcolor and a white background with the text "Pick your Bluetooth device"

Below the buttons I put a label with a yellow text saying "(c) Luc Volders 2022" which is off course bullshit but gives the app a nice touch.

From the Connectivity items (at the left part on the screen) add BlueToothClient and from the User Interface items add Notifier. These will not be visible on the screen but work in the background.

That is it.
On to the Blocks section.



Let's have a look at the individual blocks.



The ListPicker does it's job right when the app starts.
The Listpicker checks all available Bluetooth clients and puts the names and Bluetooth adresses in a list.



If you click on the ListPicker a new screen opens with the names and Bluetooth adresses available in your surroundings. When you click on the name you want to connact to BluetoothClient1 will connect to that device. The notifier will then show on your phone's screen wether the connectio was successfull or failed.



As the phone is now connected to the ESP32 the program just sits there waiting till you press the Led On or Led Off button.
When you press Button1 (Led On) the program checks wether Bluetooth is still connected and if so it sends the text "On\n" over Bluetooth to your phone. On is of course obvious. The "\n" after "On" will send a newline character. This makes any text displayed on the Serial Monitor on the ESP32 print the text and advances to a new line on the screen. It also allows us in the ESP32 program to check wether the complete text has been send.



Actually the code for the second button is the same as for the first. Only this time we send the text "Off\n"

And that is all.

Obviously you can expand this with as many buttons as you want and alter the texts that are send. I fancy vivid colors but adjust the colour settings to your own likings.

Now you can use the AI companion on your Phone to test the App or in Mit's App Inventor build the APK file and transfer that over USB to your phone.

If you do not have the time to build the app or start with App Inventor you can download the ready-to-use APK file here. Download it, transfer it to your phone and install it:


https://www.mediafire.com/file/gtcztpmompfbuwa/ESP32Bluetooth.apk/file

The ESP32 program.

The Arduino IDE provides us with the BluetoothSerial library. The Library automatically installed when the ESP32 board is installed.
There are some examples included but I am not going to use these as they only send 1 character. We are going to send words like in this example "On" and "Off". So I made my own program.

#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

String Charrcvd;
String text;

BluetoothSerial SerialBT;

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

  pinMode(5, OUTPUT);
}

void loop() 
{
  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(5, HIGH);
        }
      if (text.substring(0,text.length()-1)== "Off") 
        {
          Serial.println("Received: Off");
          digitalWrite(5, LOW);
        }
      text = "";
      delay(20);
      }
}

As usual in my tutorials I'll explain the parts of the program.

#include "BluetoothSerial.h"

Obviously we need to install the Bluetooth library.

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

These lines test wether Bluetooth is installed and enabled and otherwise throw an error.

String Charrcvd;
String text;

Two String variables are created. Charrcvd is used to collect individual characters over Bluetooth and text is used to combine these characters to a text.

BluetoothSerial SerialBT;

A so called instance of the BluetoothSerial is created with the name SerialBT.

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

  pinMode(5, OUTPUT);
}

In the setup first the Serial monitor is opened so we can check what is happening on our computer screen. This will only work if the ESP32 is connected over USB to our computer. When the ESP32 is working standalone this has no use. But it is great for testing purposes.
And lastly we define pin 5 (D5) where the led is connected as an OUTPUT.

The loop is where the fun happens so I'll go over thaqt in steps.

  if (SerialBT.available())
  {
    Charrcvd = SerialBT.readString();
       {
        text = text + Charrcvd;
       }
  }

The first line tests wether BT is available. Then each character that is received is put into the variable Charrcvd. And the next line takes that character and adds it to the variable text.

    if (text != "")
      {
      Serial.println(text.substring(0,text.length()-1));
      Serial.println(text.length());

Start with testing if there is any text received so the variable text is not empty.
For checking we print the received text minus the last character in the Serial Monitor and as an extra check we also rpint the length of the recived text.

The reason why we take the text minus the last caharacter is because the last caharacter is the Newline (\n) which is send.

      if (text.substring(0,text.length()-1)== "On")
        {
          Serial.println("Received: On");
          digitalWrite(5, HIGH);
        }

So if the text without the last caharacter is "On" we print "Received: On" in the Serial Monitor. And then we set the pin 5 High resulting in the led going on.

      if (text.substring(0,text.length()-1)== "Off")
        {
          Serial.println("Received: Off");
          digitalWrite(5, LOW);
        }


This is the same as the previous part only we test for "Off" and then set the led off.

      text = "";
      delay(20);


The text variable is made empty so we can receive new words, and there is a slight delay.

Using the example

First part is to make the App in App Inventor. Make an APK file from it and transfer that to your phone. The easiest way to do that is to connect your phone with a USB cable to your computer and copy the APK file to the download directory on your phone. When done click on the APK file and install it.

In the settings of your phone activate Bluetooth

Compile the ESP32 program in the Arduino IDE and send it to the ESP32. Check the Serial Monitor to see if it started.

Now open the App on your phone.
Click on the text "Pick your bluetooth device" at the top of the screen and a list of bluetooth devices (if there are any) will be shown.
Click on "ESP32BTtest" and the app's main screen will re-appear and a small pop-up text will show that the App is connected to the ESP32.



This is how the app looks. Press on the green button to set the led on and press the red button to set the led off.

That is all.

Expanding

The ESP32 program and the App are small and easy to adapt to use more leds, relays, servo's, motor's, ventilators and whatever is near enough to be controlled.

If you feel adventurous you can even make this voice controlled without a Google Home or Amazon Echo. Look for an example on how to do voice recognition here: https://lucstechblog.blogspot.com/2016/01/voice-command.html

Next time: sending data from the ESP32 to your app.

Till then.
Have fun.

Luc Volders