SPECIAL OFFER - Buy (1) Apeiros Robot & Get (1) 8x2 Character LCD Free!

Lesson 9

Lesson 9: Bluetooth Serial Port Profile (SPP)

In this lesson you will learn how to control Apeiros by using a Bluetooth Mate 4.0 module. You will learn how to send and receive serial data across a wireless Bluetooth connection by using the Serial Port Profile (SPP).

Prerequisites

Before starting this lesson be sure to follow the Initial Setup & Configuration. Also, we strongly recommend that you take some time to read "Getting Started with Arduino and Genuino products". This lesson assumes that you have purchased and installed an optional Bluetooth Mate 4.0. In this lesson, we intend to focus on using an Android Smartphone to connect with a Bluetooth Mate module that has been installed on Apeiros. You will need to download and install the BlueTerm application on your Android Smartphone.

You also need to compile and upload the "Apeiros_Bluetooth_Serial" sketch, which can be found under the Apeiros Examples. Once loaded, this sketch will receive and send serial data using the connected Bluetooth Mate module.

First, solder a 1x6 female header on the Apeiros printed circuit board assembly in location J16 as shown below

Next, solder a 1x6 right-angle male header to the Bluetooth Mate 4.0 module and install it as shown below. WARNING: Improper installation can result in damage to Apeiros and/or the Bluetooth Mate module! Please take note of the connections shown in the image below.

Introduction

Apeiros is built around a custom-designed printed circuit board assembly (PCBA). During the PCBA design process, we worked hard to include a core set of functionality, but did not lose sight of the need for expansion. The ATmega32U4 includes a built-in Universal Synchronous and Asynchronous serial Receiver and Transmitter (USART). We expose the serial transmit (Tx) and receive (Rx) pins at location JP16 on the Apeiros PCBA. You will also find a connection to +5 volts and GND.

Setup BlueTerm Application

After downloading and installing the BlueTerm application, you will need to connect with the Bluetooth Mate module. Be sure to turn on Apeiros, so that the Bluetooth Mate is powered on. The red power LED should be on and the green connection status LED should be blinking. On your Android Smartphone, go to Settings-> Connected Devices -> Bluetooth.

Make sure that Bluetooth is turned on and scan for the Bluetooth Mate module. The Bluetooth Mate module will show up in the Available Devices list with the name "HMSoft". Select the "HMSoft" device and begin the pairing process.

A popup screen will be presented as part of the pairing process. Be sure to enter the following pairing code: "1234". Once paired, the "HMSoft" device will show up in your "Paired devices" list.

Now we are ready to go ahead and launch the BlueTerm application.

Once BlueTerm connects with the Bluetooth Mate module the green connection status LED will stop blinking and remain on. Now we can press the lowercase letter "a" and get the analogue sensor readings.

Serial BT Command Character List

If we examine the "Apeiros_Bluetooth_Serial" sketch, then we can see the list of BT command characters within the main loop as shown below. Go ahead and add new switch cases so that Apeiros responds to new character codes!

void loop() {
  // put your main code here, to run repeatedly:
 
  ///////////////////////////////////////////////
  /* Simple Serial Communication via Bluetooth */
  ///////////////////////////////////////////////

  int incomingByte = 0;
  int motorPWM = 200;
  
  if (Serial1.available() > 0)
  {

    incomingByte = Serial1.read();
    switch (incomingByte){
    {

      case97:  // "a"
        // Analogue Sensors //
        Serial1.print("Battery=");
        Serial1.print(sensors.getBatteryMillivolts());
        Serial1.print("LeftLight=");
        Serial1.print(sensors.getLeftLight());
        Serial1.print("CenterLight=");
        Serial1.print(sensors.getCenterLight());
        Serial1.print("RightLight=");
        Serial1.print(sensors.getRightLight());
        break;

      case98:  // "b"
        // Move Backwards //
        motors.setSpeeds(-motorPWM, -motorPWM);
        break;

      case99:  // "c"
        // Close Gripper //
        myGripper.closeGripper();
        break;

      case100:  // "d"
        // Digital Sensors //
        Serial1.print("FLIR=");
        Serial1.print(io.getFrontLeftIR());
        Serial1.print("FCIR=");
        Serial1.print(io.getFrontCenterIR());
        Serial1.print("FRIR=");
        Serial1.print(io.getFrontRightIR());
        Serial1.print("RLIR=");
        Serial1.print(io.getRearLeftIR);
        Serial1.print("RRIR=");
        Serial1.print(io.getRearRightIR());
        break;

      case101:  // "e"
        // Print Encoders //
        Serial1.print("Left = ");
        Serial1.print(myEncoders.getCountsLeft());
        Serial1.print("Right = ");
        Serial1.print(myEncoders.getCountsRight());
        break;

      case102:  // "f"
        // Move Forward //
        motors.setSpeeds(motorPWM, motorPWM);
        break;

      case104:  // "h"
        // Halt //
        motors.setSpeeds(0, 0);
        break;

      case108:  // "l"
        // Turn Left //
        motors.setSpeeds(-motorPWM, motorPWM);
        break;

      case109:  // "m"
        // Buzzer Sounds //
        buzzer.playTone(330);
        delay(100);
        buzzer.playTone(123);
        delay(100);
        buzzer.playTone(1175);
        delay(100);
        buzzer.playTone(622);
        delay(100);
        buzzer.noSound();
        break;

      case111:  // "o"
        // Open Gripper //
        myGripper.openGripper();
        break;

      case114:  // "r"
        // Turn Right //
        motors.setSpeeds(motorPWM, -motorPWM);
        break;

      default;
        motors.setSpeeds(0, 0);
        break;

    }
    io.lcdClear();
  io.print("RxChar =");
  io.lcdGotoXY(0,1)
  io.print(incomingByte);
  }

  ///////////////////////////////////////////////
  /*  End Serial Communication via Bluetooth   */
  ///////////////////////////////////////////////

  delay(100);  // Delay for a specified amount of time in milliseconds
}