Command Arduino Primo UARTS via Android smartphone

Description


In this example is shown how to command via Android Smartphone the Uarts of Arduino Primo, managing, for example, the turn on/off of some Leds connected to board.
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
  • 12Leds
  • 12 Resistors (220 Ohm)
  • Wires


Software



Circuit

Connect the Leds to digital pins (D2-D13) of board as shown in the below image:

Circuit

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

/*
* Serial Port over BLE
* Create UART service compatible with Nordic's *nRF Toolbox* and Adafruit's *Bluefruit LE* iOS/Android apps.
*
* 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.
*/


// Import libraries (BLEPeripheral depends on SPI)
#include <SPI.h>
#include <BLEPeripheral.h>
#include "BLESerial.h"

// define pins (varies per shield/board)
#define BLE_REQ 10
#define BLE_RDY 2
#define BLE_RST 9

// create ble serial instance, see pinouts above
BLESerial BLESerial(BLE_REQ, BLE_RDY, BLE_RST);

int states[14];
String inputString = ""; // a string to hold incoming data

void setup() {
// custom services and characteristics can be added as well
BLESerial.setLocalName("Primo_LEDs");
BLESerial.begin();

//Declare all output
for(int p=2; p<=13;p++){
pinMode(p, OUTPUT);
states[p]=LOW;
digitalWrite(p, states[p]);
}
Serial.begin(115200);

inputString.reserve(200);
}

void loop() {
BLESerial.poll();

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


// forward received from Serial to BLESerial and vice versa
void forward() {
if (BLESerial && Serial) {
int byte;
while ((byte = BLESerial.read()) > 0){
char inChar=(char)byte;
inputString += inChar;
}
//Parse data
if(inputString.length()>0){
Serial.println("Cmd:" + inputString);
if(inputString.startsWith("d", 0)){
int pin=0;
if(inputString.length()>=2)
pin=(int)(inputString.charAt(1)-48);
if(inputString.length()==3)
pin=pin*10+((int)(inputString.charAt(2)-48));

int newState=!states[pin]; //Get previous state
digitalWrite(pin, newState);
Serial.print(pin);Serial.print(":"); Serial.println(newState);

states[pin]=newState;
}
inputString="";
}
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

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

Search the device

Now push on a button for turn on/off the corresponding Led connected.