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








Friday, July 12, 2024

Install Linux programs with Appimage

For an index to all my stories click this text.

As you might know I switched from using Windows on my desktop machine to Ubuntu. And to be more specific to Kubuntu. Kubuntu has the Windows look and feel but still is full Linux with all it's benefits: loads of software freely available, easy to use, great security so no need for a virus killer, great file system so no need for disk defragmentation etc. etc. etc. This all leads to an enormous speed improvement on the same computer that was running windows.

At first I was a bit sceptical. I did not know if all the software I needed was available. So what I did is I installed Kubuntu on a USB stick and run it from that stick. That way my Windows drive was intact and I could switch anytime I like back to Windows.
Well after working 2 months from a USB stick and never turning back to Windows I was convinced and installed Kubuntu permanently on my harddisk.

Installing software


Installing software in Kubutu is easy. Kubuntu includes a program that is called Discover. Starting that program will show you a list with all available software. Clicking on a title installs that package for you. Easy: no difficult Linux commands to learn.

Everything you need is there: Office, Thonny, audio playes, video players, terminal programs (for ssh into raspberry pi's), Cirkit and Fritzing for schematics, web-browsers, mail programs, Cura and Prusa slicer (for your 3D printer, the Gimp, text editors, Discord etc. etc. etc. All the packages look great and they have that Windows look and feel so no steep learning curves.

Arduino ide

I only had a small problem with the Arduino IDE.



The Arduino IDE is available.

However there is an issue with it.




The version that is available is Arduino 1.8.19
Nothing wrong with that although version 2 is already for some time available.

But what is worse is that Kubuntu incorporates a lower version of Python than the Arduino IDE needs........
That would not bother me so much if it would work with all the Microcontrollers I work with. The Arduino IDE would work with the ESP32 and the Raspberry Pi Pico series but it does not work with the ESP8266 and that is a bummer.

Fortunately there is a solution !!!


Appimage to the rescue

There is a new way to install software packages on Linux. It is called Appimage.
Basically you download an Appimage for a software package and it contains all necessities for many Linus distributions.

AppImages are self-contained apps which can simply be downloaded & run on any Linux distribution.

This is a quote from their website.


So you can just download an Appimage, move it to a directory where you want it, click on it and it runs. And it should run on many different Linux distributions like Ubuntu, Kubuntu, Debian, Mint, Fedora, OpenSuse etc.etc.etc.

Appimagehub

There are universal software packages for all Linux distributions. But where to find the available packages.



Luckily there is Appimagehub.
Here is the link:  https://www.appimagehub.com/browse

On this website there are (at the moment of this writing) 1380 program packages available.


And look what I found in the programming section !!


Clicking on the icon revealed this screen.

Scroll down and there is a download button. Click that button.


A pop-up window appears and after a few seconds you can click the download button.
The file will get downloaded into your download folder.

I created a new directory in my Home folder and moved the file into that directory.



Click on the icon with your right mouse button and change the files attributes to Is executable.


If you now click the icon a pop-up window appears asking if you want to execute the program.
Just click Execute.


And there is the Arduino IDE 2.2.1
As you can see I installed the ESP32, ESP8266 and Raspberry Pi Pico boards and the ESP8266 programs now compile without a problem.

Appimage pro's and cons

This is a great tool but there are some pro's and cons you should be aware off.

The pro's

- Appimage's should work on any computer running Linux.
- Any user can install a program packed as an Appimage. No need to get root privileges or using the terminal with sudo to install software.
- Everything needed to install and run a program is contained in the appimage of that program. So no need to install extra programs or libraries.
- Appimages do not alter your system libraries or congiguration. So it will not cause problems with other programs.

The biggest benefit is of course that appimages run on any Linux distribution.

The con's

- The standard program installation methods like Discover on Ubuntu (Kubuntu) regularly check if there are new updates for your programs. Appimage does not do that. So you will have to check for updates yourself regularly.
- Not all programs or applications are available as an Appimage
- The installed Appimages are not shown in your Application Launcher or program menu. You have to keep track yourself where they are stored.

Removing an Appimage

If you need to remove an Appimage because you do not need the program anymore or want to download an update just find the Appimage in your directory and delete the file. That's all.

Till next time.
Have fun


Luc Volders