Showing posts with label App Inventor. Show all posts
Showing posts with label App Inventor. Show all posts

Friday, May 9, 2025

Raspberry Pi pico with Bluetooth

For an index to all my stories click this text.

Some time ago I published a story on how to have the ESP32 communicate over bluetooth with your android Phone. Normally we use Wifi to communicate with the ESP32 but there might be circumstances in which you do not want that or can not use Wifi. You can re-read that story here: https://lucstechblog.blogspot.com/2024/08/esp32-bluetooth-classic-part-2.html

As you might know I wrote a book on the Raspberry Pi Pico which is a great microcontroller made by the Raspberry Pi Foundation. The Pico is not a linux computer but a Microcontroller like the Arduino's or ESP series. It has excellent features and a really good price-tag. A Pico can be had locally for around 5USD. One of the readers of my book who is also a follower of this blog asked if it was possible for the Raspberry Pi Pico to communicate with his phone.
Well that is not directly possible as the Raspberry Pi Pico has no communication means on board. There is standard no Wifi or Bluetooth like the ESP8266 or ESP32 have.
There is however a solution available.

Back in 2016 as faithfull readers of this blog might remember I wrote a story on how to attach an ultrasonic distance meter to an Attiny85 and send it's data to a phone using a HC-06 bluetooth module. These modules are cheap and easy to get.

We can use that same module and attach it to a Raspberry Pi Pico to send data to your phone or receive data and commands from your phone. And that is exactly what we are going to do. You can use a HC-05 or HC-06 for this. Just use that what is most easily available.

The story written in 2016 about the Attiny 85 used C++ (Arduino language). For the Raspberry Pi Pico I am going to write a program in MicroPython.
The Phone App is written in MIT's App Inventor. You can find App Inventor here: https://appinventor.mit.edu/

App Inventor has been featured in several stories and tutorials on this weblog which you can find here: http://lucstechblog.blogspot.com/p/index-of-my-stories.html

Bluetooth communication.

The Pico has so called UART connections. An UART is a, in this case build-in, hardware module that converts the data into a serial stream. The Pico has two UART's build in UART0 and UART1. I am using UART0 in this case.

To have the Pico communicate over bluetooth we need to attach a Bluetooth adapter that can convert the electrical signals from the Pico into a wireless transmission. The module often used for that is the HC-05 or the HC-06. So the HC-06 is connected to the internal UART. The breadboard setup shows how it is done.

The hardware setup



The Raspberry Pi Pico has ample I/O pins so for this test I am using 2 I/O pins for the communication with the HC-06 bluetooth module, and I use 4 I/O pins to attach leds.


The HC-06 is attached to the GND (pin 38) and 3V3-OUT (pin 36) for getting it's power. The TX line of the HC-06 is attached to the RX line (pin 2) of the Pico. The RX line of the HC-06 is attached to the TX line (pin 1) of the Raspberry Pi Pico. So the RX and TX lines are crossed. This makes sense. The Pico sends (TX) data to the Bluetooth module that receives that data (RX) and the other way round.

Next the 4 leds are connected to GND using a current delimiting resistor of 220Ohm and connected to the Pico's pins 15, 16, 17 and 19 which are GPIO 14, 13, 12 and 11.

Last year I made a Raspberry Pi Pico pin layout which you can print and lay on top of the Raspberry Pi Pico for easy reference. You can find that here: http://lucstechblog.blogspot.com/2021/03/raspberry-pico-pin-layout-help.html

The App

To have your Pico and Phone talk to eachother you need an App on your phone. The App I am going to show how to develop is for an Android Phone. As App Inventor nowadays also can be used to develop Apple app's you could adapt the program for an Iphone. However you're on your own here as I do not own an IPhone and am not willing to buy one due to it being terribly overpriced.

Unfortunately I do not have the space and time here to give you a complete App Inventor course. App Inventor does not have a steep learning curve and spending some hours with the program should get you going. Next to that there are several tutorials and examples on this weblog and App Inventor itself offers several tutorials. Check those here http://lucstechblog.blogspot.com/p/index-of-my-stories.html and here: https://appinventor.mit.edu/



This is how the screen will look eventually. At the top there is a ListPicker used for picking the right Bluetooth device. Below that there is a Tablearrangement. Inside the TableArrangement I placed 8 buttons that send commands for setting the led on or off.
Below that you see an empty square. That is a label (Label2) in which texts received from the Raspberry Pi pico will be shown.
And below that Label is Label1 with a ridiculous copyright notice.

Arrange the items to your own linking and give them the looks and colors of your choice. Play with this to learn how to use App Inventor and it's possibilities and functions.

There are 3 non-visible components at the bottom of the designer screen. The first one is the BluetoothClient component, the second is the Notifier component and the last is the clock component. You can find these in the menu on the left side of the designer screen (not shown here).
Drag them into the phone simulation screen and they will be placed at the bottom like shown here.

Now switch over to the blocks section.


This is a picture of all the necesary blocks. It may look complicated but if you get the hang of it, it really isn't.


Start with a Listpicker and put all the names and Bluetooth adresses in the list.

When the ListPicker has all the Bluetooth data and the user presses (clicks) on the desired Bluetooth device connect to it. When connected set a message on the screen (with the notifier) that the connection has succeeded or failed.


Create a block for each button that sends a particular text over Bluetooth when the button is clicked.


Here you can see the blocks that are used to set the first led on or off. When Button1 is clicked first the Bluetooth connection is tested. No use to try to send a command over Bluetooth when the device is not connected...
Then we send on1# if Button1 is pressed or off1# when Button2 is pressed.
The # is attached to the text as a means for the Pico's Python program to detect if receiving the message is completed.

The blocks for Button3 to Button8 are identical and only differ in the message they are going to send.



This last block uses the clock function to check every second wether there is a message received over Bluetooth. If a message is received it is displayed in Label2.

And that completes the App for your phone.

If you do not want to compose all the blocks yourself I have a copy of the above code available for you to download here:

https://www.mediafire.com/file/554kyre473onlh0/Pico_bluet_SR.aia/file

The Python program for the Pico.

Now we have a complete app available that can send and receive data over Bluetooth let's look at the Pico side. Here is the complete MicroPython program.

import os
import utime
from machine import UART, Pin
#print sys info
print(os.uname())
L1 = Pin(14,Pin.OUT)
L2 = Pin(13,Pin.OUT)
L3 = Pin(12,Pin.OUT)
L4 = Pin(11,Pin.OUT)
#indicate program started visually
led_onboard = machine.Pin(25, machine.Pin.OUT)
led_onboard.value(1) # onboard LED ON for 0.5 sec
utime.sleep(0.5)
led_onboard.value(0) # onboard LED ON for 0.5 sec
uart = machine.UART(0, baudrate=9600,
 bits=8, parity=None, stop=1)
print(uart)
uart.write("hello")
utime.sleep(1)
uart.write("The temperature is : ")
uart.write(str(35))
L1.value(1)
utime.sleep(1)
L1.value(0)
L2.value(1)
utime.sleep(1)
L2.value(0)
L3.value(1)
utime.sleep(1)
L3.value(0)
L4.value(1)
utime.sleep(1)
L4.value(0)
while True:
 b=""
 a=""
 c=""
 while a != "#" :
 a = uart.read(1)
 c = (a.decode('utf-8'))
 a = c
 if (c != "#"):
 b = b + c
 print("received chars: ",len(b)," = ",b)
 
 if "on1" in b:
 L1.value(1)
 uart.write("Led 1 On")
 if "off1" in b:
 L1.value(0)
 uart.write("Led 1 Off")
 
 if "on2" in b:
 L2.value(1)
 uart.write("Led 2 On")
 if "off2" in b:
 L2.value(0)
 uart.write("Led 2 Off")
 if "on3" in b:
 L3.value(1)
 uart.write("Led 3 On")
 if "off3" in b:
 L3.value(0)
 uart.write("Led 3 Off")
 
 if "on4" in b:
 L4.value(1)
 uart.write("Led 4 On")
 if "off4" in b:
 L4.value(0)
 uart.write("Led 4 Off")


As usual I will show how this program works by highlighting certain parts.

import os
import utime
from machine import UART, Pin

First we import all the necesary libraries.

L1 = Pin(14,Pin.OUT)
L2 = Pin(13,Pin.OUT)
L3 = Pin(12,Pin.OUT)
L4 = Pin(11,Pin.OUT)

The four led's are defined and the I/O pins where they are attached to are set as OUTput.

#indicate program started visually
led_onboard = machine.Pin(25, machine.Pin.OUT)
led_onboard.value(1) # onboard LED ON for 0.5 sec
utime.sleep(0.5)
led_onboard.value(0) # onboard LED ON for 0.5 sec

Then we blink the Pico's internal led as an indication that the program has started.

uart = machine.UART(0, baudrate=9600,
 bits=8, parity=None, stop=1)
print(uart)

These lines define the serial communication's settings and display them in Thonny's shell when the program runs.

uart.write("hello")
utime.sleep(1)
uart.write("The temperature is : ")
uart.write(str(35))


This is a test in which we send a fake message to the phone. First the word "hello"is send and next "The temperature is 35" As you can see the last line converts the number 35 into a string that can be send over Bluetooth.

L1.value(1)
utime.sleep(1)
L1.value(0)
L2.value(1)
utime.sleep(1)
L2.value(0)
L3.value(1)
utime.sleep(1)
L3.value(0)
L4.value(1)
utime.sleep(1)
L4.value(0)


The leds are all set on and then off as a test so we can see they are working and attached to the right I/O pins.

Then comes the most important part of the program. It is where an incoming message from the Phone is read and decoded.

while True:
 b=""
 a=""
 c=""
 while a != "#" :
 a = uart.read(1)
 c = (a.decode('utf-8'))

The program starts with defining some helper variables and making them empty.
Next a test is done wether the variable a comntains "#" if that is the case it is the last character received and it should be ignored.

If it is not "#" the next character is read from the uart and decoded to a normal ascii value.

 a = c
 if (c != "#"):
 b = b + c
 print("received chars: ",len(b)," = ",b)

Again we test wether the received character is "#" and if not the character is added to the variable b. Then we print in the shell the received characters and the length of the total received characters. This is just a test to see if everything send from the phone is indeed received.

 if "on1" in b:
 L1.value(1)
 uart.write("Led 1 On")
 if "off1" in b:
 L1.value(0)
 uart.write("Led 1 Off")

When "#" is received the command is now complete and stored in the variable b. Now we can test which command is received and set the appropriate led on or off.
Then we send "Led 1 On" or "Led 1 Off" back to the phone so the phone can check if the command really has been received and the command is executed.
These lines are repeated for all 4 leds.

In the field

When te App is started firrst press on the field with the text "Pick your Bluetooth device" and make sure Bluetooth is activated on your phone.



A new screen opens showing all Bluetooth devices available and the ones that you have used in the past. The top one has the Bluetooth adress and name (HC-06) we want to connect to. Press on that line and the connection is made.

Now you can use the app and switch the led's on and off.

This clearly demonstrates how to receive data from Bluetooth and how to send data over Bluetooth.

Of course this is just a framework to send and receive all kinds of data from your phone to the Raspberry Pico and the other way round.

For more detailed info on the Raspberry Pi Pico, a short course on MicroPython and using all kinds of sensors with the Pico please buy my book Raspberry Pi Pico Simplified: https://lucstechblog.blogspot.com/2021/08/raspberry-pi-pico-simplified-now.html

Till next time
have fun

Luc Volders








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






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








Subscribe to: Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /