Home TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 32



Scripting Tutorial - Lesson 32: BLE - Measuring Temperature with Vernier Go Wireless Temp

(UPDATED to include Vernier Go-Link support)


Lesson 30: Welcome to Bluetooth (BLE)

Supplement: Working with Scripts on the iPad

Lesson 31: BLE - Create your own TI-Nspire Remote

Lesson 32: BLE - Measuring Temperature with the Vernier Go Wireless Temp (UPDATED to include Vernier Go-Link support)

Lesson 33: BLE - Measuring Heart Rate

Lesson 34: BLE - Measuring Temperature with the TI Sensor Tag

Lesson 35: BLE - Build Your Own Weather Station with the TI Sensor Tag

Lesson 36: BLE - Exploring Movement and Position with the TI Sensor Tag

Lesson 37: Lua, LaunchPads and BLE: Making Music via BLE

Lesson 38: Lua, LaunchPads and BLE: Real world data at your fingertips: Light, Ultrasonic Motion and more...

Lesson 39: Lua, LaunchPads and BLE: Build your own BLE Robot for under $USD40


Download supporting files for this tutorial

Download this page in PDF format

Texas Instruments TI-Nspire Scripting Support Page


In the first lessons of this sequence, you were introduced to the fundamentals of BLE scripting using Ti-Nspire/Lua. You learned how to scan for BLE devices, and to connect to certain of these.

In this lesson, you will connect to specific peripherals and read data (in this case, temperature data) from these. We will begin with the Vernier Go Wireless Temp probe, since this is one of the easiest BLE connections, and then extend to the TI Sensor Tag - a little more involved, but offering the flexibility to then go on to a host of other sensors. Even if you do not have the Vernier probe, it is worth following this first section, as the principles underly all that follows.


Communicating with BLE Devices

Top of Page

With the growing array of BLE-capable devices, there will be a great variety of different ways in which they may be configured to operate. The principles we describe here will not work for all BLE devices. They will work for the specific devices described, particularly for those which operate as sensors of some sort.

Such devices collect data and make it available in quite specific forms. Part of your scripting will be taking this raw data and converting into the expected form.

Most of the common BLE probes, when activated, perform several functions:

  1. They broadcast identifying information, called UUID (universally unique ID) numbers. They may broadcast a range of these, designed to identify different functionality (as in the different sensors available with the TI Sensor Tag), but may also serve other purposes (such as giving useful information about how best to collect the data that is being offered).

  2. They will generally collect real world data of some sort - temperature, humidity, heart rate, location and many more.

  3. They will probably listen for the correct signals which will activate different aspects of their functionality (For example, the device may be configured to read data when the program wants the temperature, or may ask the thermometer to notify the program whenever a new sensor reading is available).

Two important BLE terms should be introduced before proceeding: services and characteristics. Think of services as the general organizing categories for a BLE device. For the Vernier temperature probe, think of temperature as a service. For the TI Sensor Tag, services include temperature, humidity, barometric pressure, acceleration, magnetometer and gyroscope categories.

Then the characteristics may be thought of as the data associated with these categories.

For example, the Vernier Go Wireless Temp probe may use the following UUID values (Note: the names used here are arbitrary but informative):

  • VST_FIREFLY_TEMPERATURE_DATA_UUID = '1A97C2F8-DA04-11E2-B53A-00264AA53EFC' - This UUID is used to carry with it the temperature data being collected.

  • VST_FIREFLY_TEMPERATURE_SAMPLING_PERIOD_UUID = '1A97C2FA-DA04-11E2-B53A-00264AA53EFC' - This UUID carries instructions regarding the sampling period.

  • RENAME_SERVICE_UUID = '180A' - This UUID offers an option of rename services, while

  • RENAME_CHARACTERISTIC_UUID = '2A00' - This UUID supports renaming of characteristics.

For our purposes here, we will require and use only the first of these UUIDs. Ignoring the others will simply assume the default settings for the device.


Interactive Script Exploration

This page has been enhanced using JavaScript so that we can focus easily upon the various elements of our script.

  • Click the 'init' button to see the initial variable definitions in the script window below for our Vernier Go Wireless Temp script.
  • To see the on.resize and on.paint functions, click 'Layout'.
  • For menu, keyboard and mouse controls, click 'Menu'.

    Reading and Displaying BLE Data: Vernier Temperature Probe

    We now focus upon the critical BLE functions for our script, first looking at General BLE Functions. Here we trace the path of the BLE functionality throughout the script.

    These first steps review the previous tutorial.

    • require 'bleCentral': This occurs immediately that the script is run and tests the validity of the platform and device.

    • ble.addStateListener(listenerCallback): This is placed in on.resize, setting up the BLE listener. It calls...

    • listenerCallback(state, scriptError): Checks the state (BLE.ON, BLE.OFF, etc) and may act upon errors.

    • peripheralOn(): If all conditions are met, then the user may initiate this command. As described previously, this calls...

    • bleCentral.startScanning(callbackScan): Scanning begins, calling...

    • callbackScan(peripheral): Carrying the name of a found peripheral, if this name is found then...

    • peripheral:connect(callbackConnect): This brings us up to the stage that we reached in the previous tutorial.

    The next steps are new.

    • callbackConnect(peripheral, event): Carrying the found peripheral and the event associated with this, we are ready to connect, and...

    • peripheral:discoverServices(callbackServices): All conditions being met, it is time to discover which services are available.

    • callbackServices(peripheral): Services are composed of characteristics. This function runs through found services looking for these.

    • service:discoverCharacteristics(callbackCharacteristics): Multiple characteristics will often be discovered, and each of these will lead to a callback test.


    Note again that these are general BLE functions - they are not specific to the Vernier probe and so almost everything that has gone before (apart from a few initial variables) may be reused for other BLE devices.


    platform.apilevel = '2.5'

    screen = platform.window
    w = screen:width()
    h = screen:height()

    pcall(function () require 'bleCentral' end)

    require "color"
    local bleState = ''
    local bleStatus = 'Stand by'
    local peripheralName = ''
    local myPeripheral = nil
    local characteristicsFound = 0

    -- Vernier GoTemp Init Variables

    local VST_FIREFLY_TEMPERATURE_DATA_UUID = '1A97C2F8-DA04-11E2-B53A-00264AA53EFC'

    local vTemp = nil

    local vTempF = nil

    local units = '°C'

    local unitsF = '°F'

    local nameCheckList = {'Go'}

    local nameList = {'Vernier GoTemp'}




    BLE Functions Specific to the Vernier Temperature Probe

    Time to see where the real action happens!

    • callbackCharacteristics(service): You will have seen from the final command above that each found service generates a callback. These are now checked against known UUID values, and also use characteristic:setNotify(true) to send a message to the peripheral to confirm connection. characteristic:setValueUpdateListener(callbackCharacteristic) then initiates...

    • callbackCharacteristic(characteristic) : Checking again that you are talking to the right device, the data from that device may now be configured to draw from it the required information - in this case, ambient temperature data. The Vernier probe issues 16-bit unsigned data, and so the command ble.unpack('u16', value) to take this apart and perform necessary calculations.


    If you have a Vernier probe and Nspire iPad App, you may test this entire script by copying and pasting from this web page into the TI-Nspire Lua Script Editor.


    We are now also able to offer support for the extensive range of sensors supported by the Vernier Go-Link, thanks to the great work of Fred Fotsch.

    This amazing device takes dozens of Vernier probes and broadcasts, not just a single value like temperature (or, more correctly, voltage) but an array of information, identifying the type of probe connected, along with the necessary data to convert the raw data into meaningful sensor information.

    This script would be a good extension to come back and study, perhaps after you have completed the next few BLE tutorials. However, if you happen to have a GoLink and a few Vernier probes, go right ahead and play with the TNS file attached.


    Back to Top


    Home TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 32