Introduction: Android and Arduino With Bluetooth Using Porta App!
Goal
In this tutorial I'll be showing how is super simple to exchange data between Android and Arduino using the app Porta.
As you will see, the BIG usage difference of Porta is widgets!
Now you really can use your Arduino project everyday without pain!
If you don't know it, please check it out!
Step 1: Arduino:
Connection
To keep things simple, connect the bluetooth module directly to the Arduino's main Serial pins (0 and 1). If you are already using the default Serial pins, you can implement a SoftwareSerial like this one.
Coding
Porta interprets messages using line-break character. You can set the line-break character in the Settings window. The important thing is to use the same character in both the Arduino and the app.
In this example, I'll be implementing some Serial read handling and Acknowledging income readings with a serial print. My line-break character will be \n.
Serial read handling
void loop() { if(Serial.available()){ char serialBuffer[8]; // IMPORTART: setting the Arduino to read until a break-line is detected char breakline = '\n'; \\ Here is the break-line character I talked about int finalSize = Serial.readBytesUntil(breakline, serialBuffer, 8); serialBuffer[finalSize] = '\0'; handler(serialBuffer); } }
Acknowledging income readings
void handler(String in){ if(in == "toggle"){ Serial.print("toggle"); // IMPORTART: always send the line-break character after send your message Serial.print("\n"); // ... do stuff related to 'toggle' action } }
Closure
Now we are able to listen and to send response when the Serial receives the message toggle.
In the next step we are going to setup the Android connection.
Attachments
Step 2: Android
Download Porta app
You can download it from PlayStore here.
Pair your device
In your bluetooth configuration, find and pair your device (hc-06, hc-05, other).
Setting Porta app
In Porta, you only have to set your configurations once!
- Open Porta, go to Settings button, select your device from the list;
- Change UUID if you need;
- Write down what break-line character you want: any alphanumeric character, \n, \t or \r. Default is \n;
- The Timeout option represents the time that your smartphone will wait for receive the message (after sending);
- Choose what notifications do you want to see relative to the communication;
Set actions
In Porta, an Action is a protocol of sending and receiving a response.
Set the Action 1 to send the word toggle and to receive the word toggle.
PS: the receiving message can also be a regular expression, so instead of receiving toggle you can set \w+, \w{6,6}, ^\w+$, etc..
Implement widgets
Create an Action 1 widget in the Android home screen.
Have fun!
With a simple click, Porta will send and listen for the receiving message. Not complicated at all!
Step 3: Contribute
The goal of Porta is to be able to use DIY bluetooth projects in a daily basis.
If that is what you were looking for, feel free to contribute by purchasing the Ads Free version ofPorta (you can find it here).
Your feedback means the world
Tell me what functionalities you will love to see in Porta!
by a tech lover, Raphael Brandão