Bluetooth HC-06. Arduino. Send. Receive. Send text file. Multitouch. Image

18.- Poor man’s circuit.

  • In the example above, the poor man has no LCD, no I2C module, and no KeyPad.
  • Remove the wire from terminal 0 of the Arduino.

bt_48

  • In Design, DelimiterByte = 35 (it is #)
    bt_46
// Juan A. Villalpando
// http://kio4.com/appinventor/9BA_bluetooth_teclado_LCD.htm

char key = 0;
String clave = "";

void setup() {
Serial.begin(9600);
}

void loop() {
if (Serial.available() > 0) {
  key = Serial.read();
  clave = clave + key;
      if (key == '#') {
        Serial.print(clave);
        // Example sends: 123#
        clave = ""; 
      }
  }
}
  • Now in Serial Monitor write number, 123#

bt_47

2 Likes

19.- Arduino Interrupts sends data to App.

p9A01_bluetooth_interrupt.aia (2.3 KB)


// Juan A. Villalpando
// http://kio4.com/appinventor/9A0_Resumen_Bluetooth.htm

#define LED13 13

void setup()
{
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), on_13, RISING);
  attachInterrupt(digitalPinToInterrupt(3), off_13, RISING);
  pinMode(LED13, OUTPUT);
}

void loop()
{
  // 
}

 // Interruptions ISR.
void on_13() // When pin2 RISING...
{
   digitalWrite(LED13, HIGH);
   Serial.print(1);
}

void off_13()  // When pin3 RISING...
{
   digitalWrite(LED13, LOW);
   Serial.print(0);
}
1 Like

20.- Get message when Bluetooth has disconnection failure.

p9A01_bluetooth_faliure.aia (4.0 KB)

  • Arduino sends random numbers to App:
// Juan A. Villalpando
// http://kio4.com/appinventor/9A0_Resumen_Bluetooth.htm

int aleatorio;

void setup() { 
  Serial.begin(9600);
}

void loop() { 
    aleatorio = random(0,100);
    Serial.println(aleatorio);
    delay(400); // Clock.Interval < delay Arduino
}
  • App receives those numbers and shows.

  • Disconnect the power cable from the Arduino.

  • You will get the approximate time of that disconnection.
    bt_52

  • In the previous blocks observe SendText = 3, this is dumb number, when that number is sent and the Bluetooth does not receive it, we get error 516: Unable to write: Broken pipe.

1 Like

21.- Arduino sends three random numbers to App.

p9A0i_bluetooth_aleatorios.aia (3.9 KB)

// Juan A. Villalpando
// http://kio4.com/appinventor/9A0_Resumen_Bluetooth.htm

int x;
int y;
int z;
String datos;

void setup() {
 Serial.begin(9600);
}

void loop() {
x = random(0, 10);
y = random(10, 100);
z = random(100,1000);
datos = (String) x + "," + (String) y + "," + (String) z;
Serial.println("Números aleatorios:");
Serial.println("Random numbers:");
Serial.println(datos);

delay(800);
}

Note that it only shows up when a list of 3 items arrives. Not shown:
Serial.println(“Números aleatorios:”);
Serial.println(“Random numbers:”);

22.- JoyStick in Arduino sends values. Move a ball.

p9A0_bluetooth_laberinto.aia (115.7 KB)

JoyStick Arduino.

// Juan A. Villalpando
// http://kio4.com/appinventor/9A0_Resumen_Bluetooth.htm

int x;
int y;
int c;
String datos;

void setup() {
 Serial.begin(9600);
 pinMode(2,INPUT_PULLUP);
}

void loop() {
  x = analogRead(A0);
  y = analogRead(A1);
  if (digitalRead(2) == HIGH){c=0;} else {c=1;}
  datos = (String) x + "," + (String) y + "," + (String) c;
  Serial.println(datos);
  delay(100);
}

Variables x,y: Ball.MoveTo and DrawLine.
Variable c: Canvas.Clear (when press JoyStick)

23.- Arduino sends random numbers every second and every two seconds.

p9A0i_bluetooth_aleatorio2.aia (3.4 KB)

// Juan A. Villalpando
// http://kio4.com/appinventor/9A0_Resumen_Bluetooth.htm

int alea_1;
int alea_2;
unsigned long tiempo_actual_1 = 0;
unsigned long tiempo_actual_2 = 0;

void setup() { 
  Serial.begin(9600);
}

void loop() {
  if((millis()-tiempo_actual_1)>= 1000){
          int alea_1 = random(0,100);
          tiempo_actual_1 = millis();
          Serial.println(String(alea_1));
  }
  if((millis()-tiempo_actual_2)>= 2000){
          int alea_2 = random(200,300);
          tiempo_actual_2 = millis();
          Serial.println(String(alea_2));
  }
} 

3 Likes

24.- Press a button continuously and send a character.

In this topic @Povilas_Kasparas, @Taifun and @ABG discuss how to send a character continuously while pressing a button.

char caracter;

 void setup(){  
  Serial.begin(9600);  
 } 
  
 void loop() { 
 if(Serial.available()) {
  caracter = Serial.read();
  Serial.println(caracter);
  }
}
1 Like

Thank you a lot saved me :slight_smile:

25.- Simulation of an arithmia.

p9BC_Bluetooth_ECGi.aia (12.4 KB)

Arduino sends a random level (90...100), with a random delay (400...1600)
appinventor_ECG

void setup(){
  Serial.begin(9600);
}

void loop(){
      int nivel = random(90,100);
      int ritmo = random(400,1600);
      delay(ritmo);
      String mensaje = (String) nivel + "," + (String) ritmo;
      Serial.println(mensaje);
}

App Receives Text (98,977), gets the level (98) and draws it on a Canvas.

For a more realistic process, use the module AD8232

Here in Spanish:
http://kio4.com/appinventor/9BC_bluetooth_corazon.htm

26.- App sends text. readStringUntil. readString.

p9A1_bluetooth_textoi.aia (2.2 KB)

bluetooth_enviartexto1

1.- readStringUntil

char end_char = '\n';
String texto;

void setup() { 
Serial.begin(9600);
}

void loop() { 
  if(Serial.available()) {
    texto = Serial.readStringUntil(end_char);
    Serial.println(texto);
  }
}

ooooooooooooooOOOOOoooooooooooooo

2.- readString

String texto;

void setup() { 
Serial.begin(9600);
Serial.setTimeout(100);
}

void loop() { 
  if(Serial.available()) {
    texto = Serial.readString();
    Serial.println(texto);
  }
}

26B.- Example with readString. Return.
[Module Bluetooth in pin 2 and 3 of Arduino]

p9A1_bluetooth_textoi_2.aia (3.0 KB)

#include <SoftwareSerial.h>
SoftwareSerial Bluetooth(2,3);
String texto;

void setup() { 
Serial.begin(9600);
Bluetooth.begin(9600);
Bluetooth.setTimeout(100);
}

void loop() { 
  if(Bluetooth.available()) {
    // texto = Bluetooth.readStringUntil('\n');
    texto = Bluetooth.readString();
    Serial.println(texto);
    Bluetooth.println(texto);
  }
}

27.- App sends two values ​​separated by commas. Arduino adds them up and returns the sum.

p40i_bluetooth_deslizador.aia (3.5 KB)

bluetooth_deslizador12

  • App sends two comma separated values ​​created by two Sliders.

70.3,60.4

  • Arduino receives those values, separates them by comma, and adds them up.
    Using Serial.println(addition) returns the sum

130.70

String texto;
String a ;
String b;
float suma = 0.0;

void setup() { 
 Serial.begin(9600);
 Serial.setTimeout(200);
}

void loop() { 
  if(Serial.available()) {
    // texto = Serial.readString();
    // Serial.println(texto);
    a =  Serial.readStringUntil(',');
    b =  Serial.readStringUntil('\n');
    suma = a.toFloat() + b.toFloat();
   // Serial.println(a);
   // Serial.println(b);
    Serial.println(suma);
  }
} 
1 Like

28. Bluetooth in Screen1 and Screen2.

p9H4i_pantalla_real_BT.aia (8.7 KB)

When we change from Screen1 to Screen2, we lose the Bluetooth connection.
The proper method to avoid this is to use "virtual screens" with VerticalArrangement as we saw in the post: #12

Now let's see how we can go from Screen1 to Screen2 using the disconnection and connection in the new Screen.

To go from one Screen to another, we disconnect the current connection, when we start the visible Screen we reconnect.

I've put a little delay (for each 1 ... 10000) to make the connection easier.

The app is similar to the previous example, two Sliders send two numbers to the Arduino, it adds them and returns the sum to the app.

Screen1

Screen2

Arduino code.

  • The same from the previous post.
1 Like

Excellent collection of tutorials!!
Could you please post how to use textboxes in example 27, instead of using 2 sliders?
For example, how do we enter two decimal numbers in two separate textboxes and that would be sent to the Arduino? But only one or both textboxes can be used at any time to change the corresponding values of the variables in Arduino, with a single "Send" button in app for both textboxes.
Thank you for all your amazing work!

1 Like

29.- Send text from two TextBox.

p9A0i_bluetooth_TextBox2.aia (3.6 KB)

1 Like

Hi, your tutorials are very useful. I was able to finish a lot of projects thanks to this!

Now I'm stuck with my current project. I'm trying to analogRead from my ESP32, send that data via Bluetooth, and graph that signal in mitAppInventor. I need a sample frequency of around 500 samples per second. Is this possible with ESP32?

I checked your real time graph example, but I don't think it meets my time requirements. Maybe I should save the data to a .csv and then graph from there?

Once again, thank you so much for all your tutorials. They are super helpful!

I don't think App Inventor can take that data rate and draw it in real time.

ESP32 works with Bluetooth classic (uses the Bluetooth client component) and BLE (uses the BLE extension).

Try sending a Base 64 image and see how long it takes.


u have used -1 every where in this project what it means

http://ai2.appinventor.mit.edu/reference/components/connectivity.html#BluetoothClient

ReceiveText(numberOfBytes)
Reads a number of bytes from the input stream and converts them to text.
If numberOfBytes is negative, read until a delimiter byte value is read.

In Design. Properties BluetoothClient. DelimiterByte = 10
ASCII 10 is \n, New Line, NL.

In Arduino:
Serial.println("Hello");
ln sends \n
(really \r\n)

So Arduino sends Hello\n
Bluetooth receive Hello\n
\n here means end message because DelimiterByte is 10 and numberOfBytes is -1.

Thank you.
Yeah, I think I'll try to buffer that data in the ESP32, and then send it to the app and make a graph with that.
It definitely can't handle the "real time" part.

Hey Juan, thanks for this potentiometer app. I am trying to modify it such that the Y axis should vary between -90 to 90 and the variable will fluctuate between +ve and -ve along the X-axis. This would be similar to your p91C_grafico_amortiguacion app. But how to do this? I am kinda lost with the complexity of the graphing app. Also, how to add labels to the axes?