Send a text via Android smartphone

Description


In this example is shown how to send a text on a Serial Monitor or a LCD, managing the Arduino Primo via Android Smartphone.
The sketch uses an application for Android smartphone that allows to communicate with the board via the Bluetooth.
You can download it here or you can use the Nordic's *nRF Toolbox* and Adafruit's *Bluefruit LE* iOS/Android apps.

Hardware


  • Arduino Primo
  • Android Smartphone with bluetooth enabled
  •  LCD (you can use also the Serial Monitor)


Software



Circuit

If you want to use the Serial Monior of IDE then it doesn't need a circuit.

ArduinoPrimo board

Code

Upload the below code.

Warning:
Use the Arduino IDE 1.8.x and check that you have already downloaded the corresponding platform (Arduino NRF52 Boards) from the Board Manager

/* Copyright (c) Sandeep Mistry. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
* Modified by Chiara Ruggeri <chiara@arduino.org>;

* Serial Port over BLE
* Create UART service compatible with Nordic's *nRF Toolbox* and Adafruit's *Bluefruit LE* iOS/Android apps.
* You can also use another board with serial_client example in File->Examples->BLE->Central menu to set up
* a BLE serial connection between two different boards.
*
* BLESerial class implements same protocols as Arduino's built-in Serial class and can be used as it's wireless
* replacement. Data transfers are routed through a BLE service with TX and RX characteristics. To make the
* service discoverable all UUIDs are NUS (Nordic UART Service) compatible.
*
* Please note that TX and RX characteristics use Notify and WriteWithoutResponse, so there's no guarantee
* that the data will make it to the other end. However, under normal circumstances and reasonable signal
* strengths everything works well.
*
* In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is advertising.
* It will be on when the board is connected to a central. It will be off when the board is disconnected.
*/


#include <BLEPeripheral.h>
#include <BLESerial.h>

// create ble serial instance
BLESerial bleSerial = BLESerial();


void setup() {
// custom services and characteristics can be added as well
bleSerial.setLocalName("Primo_Serial");

//initialize BLE led
pinMode(BLE_LED, OUTPUT);

Serial.begin(9600);
bleSerial.begin();
}

void loop() {
bleSerial.poll();

forward();
// loopback();
// spam();

// handle the BLE led. Blink when advertising
if(bleSerial.status() == ADVERTISING){
digitalWrite(BLE_LED, LOW);
delay(200);
digitalWrite(BLE_LED, HIGH);
delay(200);
}
else // if we are not advertising, we are connected
digitalWrite(BLE_LED, HIGH);
}


// forward received from Serial to BLESerial and vice versa
void forward() {
if (bleSerial && Serial) {
int byte;
while ((byte = bleSerial.read()) > 0) Serial.write((char)byte);
while ((byte = Serial.read()) > 0) bleSerial.write((char)byte);
}
}

// echo all received data back
void loopback() {
if (bleSerial) {
int byte;
while ((byte = bleSerial.read()) > 0) bleSerial.write(byte);
}
}

// periodically sent time stamps
void spam() {
if (bleSerial) {
bleSerial.print(millis());
bleSerial.println(" tick-tacks!");
delay(1000);
}
}

Output

Install the apk in your Android Smartphone.
Launch the application.

Arduino Primo apk

CLick on “Try to send text to LCD”

Arduino Primo application

Search the Primo board (in the sketch the BLE_Board name is set as Primo_Serial)


Select the device

Write a message and click on Send button

write a message

Send a message

Verify the message using the Serial Monitor, setting the baud-rate to 9600, or a LCD.

Serial Monitor