Add new comment

Bluetooth & AppInventor (1)

English

 

Hello, today we're gonna explain ho to create an app with Appinventor and how to communicate it with Arduino . We connect bluetooth module to Arduino like we told you at  last post with mobile's bluetooth trought a simple app with Appinventor.

First we create a little program for Arduino, only to send an order via Bluetooth and do something easy with Arduino board, for example switch LED on/off .

We will use the next program : 

 

#include 

#define txPin 3 // definimos txpin en que pin de arduino se encuentra
#define rxPin 2 // definimos rxpin en que pin de arduino se encuentra 

SoftwareSerial bluetooth(rxPin, txPin); // el puerto nuevo de comunicacion se llamar bluetooth

char NOMBRE[10]  = "TEST1";  // asignamos un nombre a nuestro modulo bluetooth para cuando lo veamos en linea 
char BPS         = '4';           // 1=1200 , 2=2400, 3=4800, 4=9600, 5=19200, 6=38400, 7=57600, 8=115200
char PASS[10]    = "1234";   // password para realizar la conexion. Sino funciona 0000
 
char INBYTE;
int  LED = 13; // LED on pin 13 // hacemos que el led 13 de Arduino integrado nos sirva de prueba

void setup() {
  Serial.begin(9600); // velocidad de la comunicacion entre arduino y ordenador 
 Serial.println("Estoy preparado"); //mensaje de comprobacion
   pinMode(LED, OUTPUT);
  bluetooth.flush();            // Borramos el buffer del serial para evitar errores
  pinMode(rxPin, INPUT);     // Configuramos los pines del bluetooth 
  pinMode(txPin, OUTPUT);
  bluetooth.begin(9600);  // marcamos la velocidad del puerto bluetooth 

}
 
void loop()
{
  while (!bluetooth.available());   // mientras el puerto este disponible ejecuta el programa
  INBYTE = bluetooth.read();        // lee la comunicacion bluetooth
  if( INBYTE == '2' ) 
    {
      digitalWrite(LED, LOW);  // si pulsas el numero 3 escribe en el led apagado
      bluetooth.println("Has presionado 2"); //muestra en el ordenador que tecla se ha pulsado
    }
  if( INBYTE == '1' ) 
  {
    digitalWrite(LED, HIGH); // si pulsas el numero 4 escribe led encendido
    bluetooth.println("Has presionado 1");
  }
  delay(50);
  
}
 
The line "if INBYTE ==…. then …" told us what happend when you push 1 or 2 on keyboard. If you push 1 on app button then switch ON LED and when you push 2 on app button then switch OFF LED. 

(Download program)

Now e have finished the program, let's go to create the app on Appinventor. We create a new project and we drag and drop two buttons, one dor switch ON and switch OFF.

Now we change the name of the buttons (optional) 

We arrange the buttons horitzontaly

We add a "list pick" to create the "bluetooth connect button". The button will change the color when bluetooth is active (that's it because we need a list pick instead a simple button)

We add bluetooth to screen 

After that we work with Block Appinventor (top right) and we start to program our app.

This is the bluetooth block . We could see that we check the bluetooth connection first and if is correct the bluetooth change to blue color. 

The two last blocks are create to send the character "1" and "2" to switch ON or OFF our LED in the Arduino board. As you can see we can send whatever character you want, then you could add more functions to send at arduino board. 

After these steps we create a QR code  , we download the app to mobile 

When we open th app we push "conexión" and we could see the MAC of bluetooth module. Then we connect (maybe you have to use the password 1234 or 1111 ). The red led on bluetooth module stops blinking and then is connect correctly.

We test the buttons ON and OFF and we could see how it switch ON or OFF LED

 

See you in the next article

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.