|     Inicio    |   |         |  |   FOROS      |  |      |      
   Elastix - VoIP B4A (Basic4Android) App inventor 2 PHP - MySQL
  Estación meteorológica B4J (Basic4Java) ADB Shell - Android Arduino
  Raspberry Pi Visual Basic Script (VBS) FireBase (BD autoactualizable) NodeMCU como Arduino
  AutoIt (Programación) Visual Basic Cosas de Windows Webs interesantes
Translate:
Búsqueda en este sitio:


.

Tutorial del Internet de las Cosas y Bluetooth con el ESP32
Juan Antonio Villalpando

Volver al índice del tutorial

____________________________

150.- Wemos D1 R32 ESP32. Bluetooth clásico. App Inventor. LCD.

_________________________________
11.- Bluetooth clásico (SPP).

https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/examples/SerialToSerialBT/SerialToSerialBT.ino

- Para conectar con el ESP32 por Bluetooth necesitamos esta librería: BluetoothSerial.zip

- Puede enviar hasta 260 caracteres.

______________________________________________________
1.- App Inventor envía un mensaje al ESP32 y se muestra en el Monitor Serie.

- En App Inventor creamos una aplicación con tres Botones, una Etiquetas y un Cliente Bluetooth

_____________________________________________
- Código para el ESP32.

AppInventor_a_ESP32.ino

// Juan A. Villalpando
// kio4.com

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth no activado! Activa la conexion Bluetooth.
#endif

BluetoothSerial SerialBT;

char caracter;
String palabra;

void setup(){
    SerialBT.begin("ESP32test");
    Serial.begin(115200);
}

void loop(){
  if(SerialBT.available()) {
  caracter = SerialBT.read();
  palabra = palabra + caracter;

  if(caracter == '*') {    
    palabra = palabra.substring(0, palabra.length() - 1); // Quita último caracter * 
    Serial.println(palabra);
    // SerialBT.write(palabra); // Error. No funciona con String.
    palabra = "";
  }
    delay(100); 

} // =>Fin del available
}

_____________________________________________
- Comentarios.

- Solamente con el Serial, no conecta el Bluetooth, necesitamos la librería "BluetoothSerial.h".

- Con la librería BluetoothSerial no podemos visualizar String en el Monitor Serie, por eso visualizamos con Serial.println(palabra).

- Con la librería BluetoothSerial no funciona BluetoothSerial.print, funciona BluetoothSerial.write pero solo con (Serial.read())

- En el código de App Inventor observamos que el mensaje debe terminar en *, para indicarle al código del ESP32 que ha terminado el mensaje.

- El Serial.begin(115200); // Se suele poner estos baudios.

- El SerialBT.begin("ESP32test"); // No se le pone baudios.

 

______________________________________________________
______________________________________________________
______________________________________________________
______________________________________________________
______________________________________________________
2.- Al pulsar "Enviar" el Monitor Serie del ESP32 envía un mensaje a App Inventor.

- En App Inventor creamos una aplicación con tres Botones, dos Etiquetas, un Reloj y un Cliente Bluetooth.

- Importante el Intervalo del Reloj debe ser más rápido que el envío del ESP32, en este ejemplo no se analiza eso.

- El Cliente Bluetooth debe tener un ByteDelitimador = 10, esto significa que cuando le llegue el "\n", (que es el 10), sepa que a terminado el mensaje.

_____________________________________________
- Código para el ESP32.

ESP32_a_AppInventor.ino

// Juan A. Villalpando
// kio4.com

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth no activado! Activa la conexion Bluetooth.
#endif

BluetoothSerial SerialBT;

char caracter;
int aleatorio;
String alea;

void setup(){
    SerialBT.begin("ESP32test");
    Serial.begin(115200);
}

void loop() {
  if (Serial.available()) {
    caracter = Serial.read(); // Al Pulsar "Enviar"
    aleatorio = random(0, 100);
    alea = (String) aleatorio + "\n";
    Serial.print(alea);   // Puede ser int o String.
    SerialBT.print(alea); // Debe ser String y con \n.
  }

  delay(20);
}

_____________________________________________
- Comentarios.

- En este ejemplo cada vez que pulsamos el botón "Enviar" en el Monitor Serie, se envía un número aleatorio a App Inventor.

- El número se muestra en el Monitor Serie mediante Serial.print(alea);

- Y se envía por Bluetooth mediante SerialBT.print(alea);

- Es necesario que el mensaje termine en "\n", para indicarle al App Inventor su finalización.


- Otros ejemplos.

- El primer if, espera un caracter desde el Monitor Serie y lo envía por Bluetooth.

- El segundo if, espera que llegue un caracter por Bluetooth y lo muestra en el Monitor Serie.

- Desde App Inventor enviamos información por Bluetooth a la tarjeta.

- Podemos probarlo desde esta aplicación de App Inventor:

9W.- Motor paso a paso desde Bluetooth.

bluetooth.ino

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

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

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

- Modificando un poco el código anterior, podemos escribir una serie de caracteres en el Monitor Serie, pulsar el Botón Enviar y enviar esos caracteres y que además se muestren en el mismo Monitor Serie.

_________________________________
- Código.

bluetooth_MonitorSerie.ino

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#include "BluetoothSerial.h"
char rx_byte = 0;
String rx_str = "";

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

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  if (Serial.available()) {
    rx_byte = Serial.read(); // Toma el caracter
    rx_str += rx_byte;
    Serial.println(rx_str);
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

_______________________________

- Mi correo:
juana1991@yahoo.com
- KIO4.COM - Política de cookies. Textos e imágenes propiedad del autor:
© Juan A. Villalpando
No se permite la copia de información ni imágenes.
Usamos cookies propias y de terceros que entre otras cosas recogen datos sobre sus hábitos de navegación y realizan análisis de uso de nuestro sitio.
Si continúa navegando consideramos que acepta su uso. Acepto    Más información