How to use blueIOT with „techBASIC“

Latest version of techBASIC sample, showing map with current GPS position (iPhone) and recording option for sensor data:

Foto

Step1: Get you copy of techBASIC, it is an awesome tool on your mobile (iPhone, iPad with BLE support) for visualizing and applying math operations on the sensor data received via the blueIOT device. The techBASIC page and Building iPhone and iPad Electronics Projects are good links to start with techBASIC.

Step2: Start with this code sample and learn how to connect blueIOT to techBASIC for visualizing the sensor data received as read,notify:

3d

Simply copy this code to your techBASIC and dig into the world of Bluetooth Low Energy.

System.showConsole
DIM sensorTag AS BLEPeripheral
DIM services(1) AS STRING
DIM s$ AS STRING
DIM A$ AS STRING
DIM B$ AS STRING
DIM C$ AS STRING
DIM A AS INTEGER
DIM B AS INTEGER
DIM C AS INTEGER
services(1) = „06CCE3A0-AF8C-11E3-A5E2-0800200C9A66“
DIM p as Plot, px as PlotPoint, py as PlotPoint, pz as PlotPoint
DIM ax(100, 2), ay(100, 2), az(100, 2)
index = 1
FOR t = 1 TO 100
ax(t,1) = t/10.0
ay(t,1) = t/10.0
az(t,1) = t/10.0
NEXT
! Create the user interface.
setUpGUI
debug = 1
BLE.startBLE
DIM uuid(0) AS STRING
BLE.startScan(uuid)
SUB BLEDiscoveredPeripheral (time AS DOUBLE, _
                             peripheral AS BLEPeripheral, _
                             services() AS STRING, _
                             advertisements(,) AS STRING, _
                             rssi)
! IF peripheral.bleName = „xyz“ THEN
  sensorTag = peripheral
  BLE.connect(sensorTag)
  BLE.stopScan
! END IF
END SUB
SUB BLEPeripheralInfo (time AS DOUBLE, _
                       peripheral AS BLEPeripheral, _
                       kind AS INTEGER, _
                       message AS STRING, _
                       err AS LONG)
IF kind = 1 THEN
  ! The connection was established. Look for available services.
  IF debug THEN PRINT „Connection made.“
  peripheral.discoverServices(uuid)
ELSE IF kind = 2 OR kind = 3 THEN
  IF debug THEN PRINT „Connection lost: „; kind
  BLE.connect(sensorTag)
ELSE IF kind = 4 THEN
  ! Services were found. If it is one of the ones we are interested
  ! in, begin discovery of its characteristics.
  DIM availableServices(1) AS BLEService
  availableServices = peripheral.services
  FOR s = 1 to UBOUND(services, 1)
    FOR a = 1 TO UBOUND(availableServices, 1)
      IF services(s) = availableServices(a).uuid THEN
        IF debug THEN PRINT „Discovering characteristics for „; services(s)
        peripheral.discoverCharacteristics(uuid, availableServices(a))
      END IF
    NEXT
  NEXT
END IF
END SUB
SUB BLEServiceInfo (time AS DOUBLE, _
                    peripheral AS BLEPeripheral, _
                    service AS BLEService, _
                    kind AS INTEGER, _
                    message AS STRING, _
                    err AS LONG)
IF kind = 1 THEN
  ! Get the characteristics.
  DIM characteristics(1) AS BLECharacteristic
  characteristics = service.characteristics
  FOR i = 1 TO UBOUND(characteristics, 1)
    IF service.uuid = services(1) THEN
      ! Found the accelerometer.
      SELECT CASE characteristics(i).uuid
        CASE „06CCE3A2-AF8C-11E3-A5E2-0800200C9A66“
        peripheral.setNotify(characteristics(i), 1)
      END SELECT
    END IF
  NEXT
END IF
END SUB
SUB BLECharacteristicInfo (time AS DOUBLE, _
                           peripheral AS BLEPeripheral, _
                           characteristic AS BLECharacteristic, _
                           kind AS INTEGER, _
                           message AS STRING, _
                           err AS LONG)
IF kind = 2 THEN
  DIM value(1) AS INTEGER
  value = characteristic.value
  SELECT CASE characteristic.uuid
    CASE „06CCE3A2-AF8C-11E3-A5E2-0800200C9A66“
      s$=““
      for i = 1 to ubound(value,1)
      s$=s$ & chr(value(i))
      next

C$ = s$

      A$ = LEFT(C$, POS(C$, „,“) – 1)
      C$ = RIGHT(C$, LEN(C$) – POS(C$, „,“))
      B$ = LEFT(C$, POS(C$, „,“) – 1)
      C$ = RIGHT(C$, LEN(C$) – POS(C$, „,“))
!PRINT VAL(A$), VAL(B$), VAL(C$)
a=val(A$)
b=val(B$)
c=val(C$)
!print a
    ax(index, 2) = a / 1000
    ay(index, 2) = b / 1000
    az(index, 2) = c / 1000
    index = index + 1
    IF index > 100 THEN index = 1
  px.setPoints(ax)
  py.setPoints(ay)
  pz.setPoints(az)
Graphics.repaint
    CASE ELSE
      PRINT „Read from „; characteristic.uuid
  END SELECT
ELSE IF kind = 3 AND err <> 0 THEN
  PRINT „Error writing „; characteristic.uuid; „: („; err; „) „; message
END IF
END SUB
! Set up the user interface.
SUB setUpGUI
p = Graphics.newPlot
p.setTitle(„blueIOT – Acceleration in Gravities“)
p.setXAxisLabel(„Time in Seconds“)
p.setYAxisLabel(„Acceleration: X: Green, Y: Red, Z: Blue“)
p.showGrid(1)
p.setGridColor(0.8, 0.8, 0.8)
p.setAllowedGestures($0042)
px = p.newPlot(ax)
px.setColor(0, 1, 0)
px.setPointColor(0, 1, 0)
py = p.newPlot(ay)
py.setColor(1, 0, 0)
py.setPointColor(1, 0, 0)
pz = p.newPlot(az)
pz.setColor(0, 0, 1)
pz.setPointColor(0, 0, 1)
p.setView(0, -2, 10, 2, 0)
system.showGraphics(1)
! Set the plot size.
p.setRect(0, 40, Graphics.width, Graphics.height – 47)
! Draw the background.
Graphics.setPixelGraphics(0)
Graphics.setColor(0.886, 0.886, 0.886)
Graphics.fillRect(0, 0,Graphics.width, Graphics.height)
END SUB