Home TI-Nspire Scripting HQ TI-Nspire STEM HQ Getting Started with TI LaunchPads TI LaunchPad Lesson 5

     
     

Lesson 5: From Serial to BLE

Energia Reference page

Texas Instruments TI-Nspire Scripting Support Page

Download the TI-Nspire LaunchPad document for this lesson

     
           
     

Connecting to the World Beyond - Wirelessly!

Now that we know how to use the serial port on our LaunchPad, the great news is that we also know how to work with BLE - simple BLE modules, such as the Grove BLE used here actually function as an additional serial port. While the main Serial port transfers data via the USB port, the LaunchPad is able to define additional serial ports, and so we define "Serial1" to be our BLE port.

So connect your BLE module as described below and just change all "Serial" references in previous sketches to "Serial1" and you should be good to go! Of course, you will need a way to read and write these BLE communications. For that we will use the TI-Nspire iPad App(s) with a Lua document described below.

   

Connecting your BLE Module

You may want to go back to the previous lesson and review the pin layout of the MSP432 board. Of interest here are the pins labelled RX (receive) and TX (transfer). For our Serial1 port, these will be pins 3 (P3_2) and 4 (P3_3).

The Grove BLE modules (and similar units) use 4 wires - GND, voltage, RX and TX. For the MSP432 (and several other LaunchPads) this will mean connecting to pins 1 and 22 (or any of the equivalent 3V3 and GND pins on the board), and pins 3 and 4 for RX and TX - note that RX on the module connects to TX on the board, and vice-versa).

If you have the Grove Base BoosterPack then things are even easier. At the top of the BoosterPack board there are three connectors - the middle one of these serves as the Serial2 port (it is wired directly to pins 3 and 4 as we described manually above).

  
  

LaunchPad Sketch
(Copy and paste into Energia)

int buttonState1 = 0;
int buttonState2 = 0;

void setup()
{

pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(PUSH1, INPUT_PULLUP);
pinMode(PUSH2, INPUT_PULLUP);
Serial1.begin(9600); }

void loop()
{
buttonState1 = digitalRead(PUSH1);
buttonState2 = digitalRead(PUSH2);
while(Serial1.available()){

char recvChar = Serial1.read();

switch(recvChar) {

case 'r':

digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, LOW);
break;

case 'g':

digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(BLUE_LED, LOW);
break;

case 'b':

digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, HIGH);
break;

case '1':

buttonState1 = LOW;
break;

case '2':

buttonState2 = LOW;
break;

case '0':

digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, LOW);
break;

}
}

if (buttonState1 == LOW) {
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, LOW);
Serial1.println("P1");
}
if (buttonState2 == LOW) {
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(BLUE_LED, LOW);
Serial1.println("P2");
}
}

Lua Script (copy and paste full script into TI-Nspire Script Editor)

 
 

platform.apilevel = '2.5'

screen = platform.window
w = screen:width()
h = screen:height()
local date = "110216"

pcall(function () require 'bleCentral' end)

require "color"
local nameList = {'HMSoft'}
local bleState = ''
local bleStatus = 'Stand by'
local peripheralName = ''
local myPeripheral = nil

local groveBLE = 'FFE1'
local keyPress = 0
local alert = nil
local myChar = nil

local textBox = D2Editor.newRichText()
local boxX, boxY, boxWidth, boxHeight
local fontSize = 12
local button1 = "Scan for BLE"
local button2 = "Reset"

 
 

 
 

Understanding the LaunchPad Sketch

Study the LaunchPad sketch first. It should, by now, be starting to look familiar and understandable. Nothing new has been introduced here - other than swapping all "Serial" references for "Serial1". Note that you could still include Serial references as well, and have comments appear in the Serial Monitor in Energia, but this is probably not necessary since the joy of BLE is that you are now free from the wired link to the computer, and able to work with a power source and an iPad, anywhere!

This sketch, then, enables two types of interaction with the board. Writing particular letters and numbers to the board will activate LEDs, and simulate pressing the two buttons (in the same way that we previously wrote from the Serial Monitor to the board to cause such reactions).

Of perhaps greater interest is the power to write from the board to the iPad: pressing PUSH1 or PUSH2 will send a message to the Lua script, which then registers this with a simple screen message. This will form the basis, shortly, for important interactions such as connecting the LaunchPad to a sensor, reading the data from that device - and sending this data to the iPad ready for visualisation and analysis!

  
  

Understanding the Lua Script

The Lua script builds upon the basics covered in the BLE Lua tutorials - refer to Build your own BLE Remote as a starting point. In fact, the two buttons of the LaunchPad can be readily used in the same way as the SensorTag buttons to control TI-Nspire documents are variables. If you have been using a SensorTag in this way, then you should definitely try the same things with your LaunchPad! This will be covered in a lesson or two.

In fact, the LaunchPad is a much simpler introduction to BLE than the SensorTag. The Tag has an enormous amount of data to transmit and to be interpreted, and so is necessarily more complex. The LaunchPad is quite capable of doing the same, but for our purposes we will be starting with simple examples, as you have seen, initially just sending and receiving single characters.

Focus for a moment on the section of the Lua script. The UUID for the Grove BLE module ("HMSoft") is 'FFE1' - coincidentally exactly the same as the simple keys UUID for the SensorTag. Potentially, this makes it even easier to write scripts that can accommodate both LaunchPad and SensorTag. However, this UUID controls only the keypresses on the SensorTag; the red and green LEDs and buzzer are controlled by another UUID. A sample page that will work with both LaunchPad/Grove BLE and SensorTag 2.0 (CC2650) is included in Problem 2.

For an even simpler example, what if we only wanted to write to the LaunchPad, and did not need the ability to read from it?

Then our final section of the script (BLE Specific Functions) could be very simple indeed: as shown here, it could consist of just the function shown - no need for the "callbackCharacteristic(characteristic)" function at all, since all that is needed is the characteristic (myChar) for the connected device.

-- BLE Specific Functions (2) ---------

function callbackCharacteristics(service)

local characteristicsList = service:getCharacteristics()
for _,characteristic in ipairs(characteristicsList) do

if characteristic:getUUID() == groveBLE then
myChar = characteristic
end
end

end

  
  

Back to Top

  

Home TI-Nspire Scripting HQ TI-Nspire STEM HQ Getting Started with TI LaunchPads TI LaunchPad Lesson 5