fbpixel
Tags: , , , , ,

Learn how to create an application for Android very simply using App Inventor 2.

In the fields of robotics and connected objects, one of the most interesting features to implement is the remote control of an electronic project. Whether to display measurements from his home or to drive a drone, a possible solution is to develop a smartphone application and communicate with a microcontroller using a Bluetooth connection. This App can be quickly created using MIT’s online software, App Inventor 2 (AI2).

Material

  • Computer
  • Android smartphone with a QR code reader app
  • Arduino UNO
  • Bluetooth HC-06
  • Dupont F / M Cable

Creating AI2 application

In this example, we create an application to enable a function (ON / OFF) and retrieve a sensor measurement from an Arduino board. This tutorial can be applied for many other applications.

Register on the App Inventor 2 website .

Create a new project named myApp

start-new-project Create an app with App inventor 2

The designer screen of the project appears

new-project-designer-screen Create an app with App inventor 2

AI2 Application Configuration

To create the design of our application, we use the objects available under the tabs in the drop-down menu on the left of the screen and modify their parameters that appear on the right of the screen.

In User interface, add a label object and rename it titleLbl. It will contain the title of the application.

project-title-label Create an app with App inventor 2

In layout, add a VerticalArrangement object that allows you to align objects vertically.

project-bluetooth-canvas Create an app with App inventor 2

In VerticalArrangement, place a label btStatusLbl and then a ListPicker btBtn that will be used to manage the Bluetooth connection.

project-bluetooth-lbl-btn Create an app with App inventor 2

Then add a label named sensorLbl that will contain the value received from the Arduino board.

project-sensor-lbl Create an app with App inventor 2

Add a HorizontalArrangement that allows you to align objects horizontally and that will contain the buttons to activate the Arduino function.

project-command-canvas Create an app with App inventor 2

In the HorizontalArrangement, add two buttons named onBtn and offBtn.

project-on-off-btn Create an app with App inventor 2

Finally, in Connectivity, add a BluetoothClient object that will allow us to interact with the Bluetooth device.

project-bluetooth-client Create an app with App inventor 2

And in Sensor, add two Clock objects which will help receive and send data at a regular time interval.

project-clock Create an app with App inventor 2

You can customize your application as you like by using different colours, fonts or images.

project-designer-end Create an app with App inventor 2

AI2 Application Programming

AI2 uses Scratch to program application which makes it possible to create a program in the form of block diagram. It is very easy to use but requires some programming logic.

All functions are available by clicking on the objects in the drop-down menu on the left of the screen. Browse the functions of the objects added to the app designer as well as those of the objects Variables, Text, Math and Control that we will use.

ai2-blocks-btn-menu Create an app with App inventor 2

Create a list of available Bluetooth modules on the device.

ai2-blocks-create-list Create an app with App inventor 2

Manage establishment of the connection and change the label according to the connection status.

ai2-blocks-handle-bt-connexion Create an app with App inventor 2

A global variable “command” is defined in order to keep in memory the value given by the last button pressed (ON or OFF).

ai2-blocks-handle-command Create an app with App inventor 2

The value of command is sent via Bluetooth, if it is connected, at a frequency defined by the object Clock1.

ai2-blocks-send-command Create an app with App inventor 2

To recover the value of the sensor sent from Arduino, we check if data is available in the buffer with a frequency defined by Clock2. We then display this value in sensorLbl.

ai2-blocks-recieve-text Create an app with App inventor 2

Complete Code

Once the blocks are placed and configure properly, the code should look loke this

ai2-myapp-complete-code Create an app with App inventor 2

Compile and share your Application

Once this program in Scratch is correctly configured and displays no errors, install a QR code reader application and compile the program using the menu Build> App program (provide QR code for .apk)

ai2-app-build-qrcode Create an app with App inventor 2

Once the QR code has been generated, you can download and install the application on your Smartphone.

ai2-app-QRcode Create an app with App inventor 2

You can share the installer .APK so that other can install your App on an Android phone. You can share your project as a AIA file so that other can use and modify your application.

Export to create the AIA file on your computer

ai2-app-export-project-aia Create an app with App inventor 2

Import to open a AIA file fro your computer to the online software

ai2-app-import-project-aia Create an app with App inventor 2

Arduino Code

Here is the code to upload to the Arduino board to receive and send data to the serial port.

#include <SoftwareSerial.h>

SoftwareSerial hc06(2,3);

String cmd="";
float sensor_val=0;

void setup(){
  //Initialize Serial Monitor
  Serial.begin(9600);
  //Initialize Bluetooth Serial Port
  hc06.begin(9600);
}

void loop(){
  //Read data from HC06
  while(hc06.available()>0){
    cmd+=(char)hc06.read();
  }

  //Select function with cmd
  if(cmd!=""){
    Serial.print("Command recieved : ");
    Serial.println(cmd);
    // We expect ON or OFF from bluetooth
    if(cmd=="ON"){
        Serial.println("Function is on");
    }else if(cmd=="OFF"){
        Serial.println("Function is off");
    }else{
        Serial.println("Function is off by default");
    }
    cmd=""; //reset cmd
  }
  // Simulate sensor measurement
  sensor_val=(float)random(256); // random number between 0 and 255
    
  //Write sensor data to HC06
  hc06.print(sensor_val);
  delay(100);
}


Result

We can activate and deactivate the function in the Arduino code and receive data from a sensor.

myApp-not-connected Create an app with App inventor 2
myApp-connected Create an app with App inventor 2
ai2-app-arduino-onoff Create an app with App inventor 2

Do not hesitate to leave a comment or to send us a message if you need more information on how to create a Smartphone Application.

Applications

Sources