Example: LogicMachine as Bluetooth 4.0 Low Energy gateway

Task

Interconnect LogicMachine with Mio Alpha watch and map heart-rate measurement to KNX group address. Upon excessing specific heart-rate measurement, switch on ventilation on group address 2/2/2. In same way any other Bluetooth LE 4.0 sensor with open API or iBeacon can be integrated with any supported standard by LogicMachine.

Some of supported Bluetooth 4.0 USB adapters

We will use Laird BT820 in this demo.

alpha_mio_logic_machine_automationLaird_LogicMachine_Bluetooth4.0

 

Install BLE packages on your LogicMachine

You need to install these packages first, the reboot:

libbluetooth
luable
genohm-scada-ble

You can download them in additional packages section on this page.
 

Add 1byte object 1/1/1 in Objects menu

Mio_alpha_logic_machine_object

Add the following code to Resident script with interval = 0 seconds

Source code    
  1. if proc then
  2. line = proc:read()
  3. parseline(line)
  4. else
  5. mac = 'D7:2D:DA:DF:E4:34' -- MAC of AlphaMio watch
  6.  
  7. -- bring bt interface up
  8. os.execute('hciconfig hci0 up')
  9. os.sleep(2)
  10.  
  11. -- read heart rate data
  12. proc = io.popen('gatttool -b ' .. mac .. ' -t random --char-write-req -a 0x0025 -n 0100 --listen')
  13. count = 0
  14.  
  15. function parseline(line)
  16. local pos, rate
  17.  
  18. -- invalid data
  19. if not line then
  20. return
  21. end
  22.  
  23. -- find value marker
  24. pos = line:find('value: ', 1, true)
  25. if not pos then
  26. return
  27. end
  28.  
  29. -- get current heart rate
  30. rate = tonumber(line:sub(pos + 10, pos + 11), 16)
  31.  
  32. -- send each 5 reads
  33. count = count + 1
  34. if count == 5 then
  35. grp.update('1/1/1', rate)
  36. count = 0
  37. end
  38. end
  39. end

Add event-based script heart-rate object 1/1/1

This script will switch on ventilation if the heart-rate is >80 and switch off if its lower.

Source code    
  1. value = event.getvalue()
  2. if value > 80 then
  3. grp.write('2/2/2', true)
  4. else
  5. grp.write('2/2/2', false)
  6. end

Other Bluetooth watches integration

LM can support the Bluetooth devices which manufacturer is not hiding data encryption, which we can decode. For example:

    • Xiaomi Mi Band: easy to connect, has acceleroemeter on-board which can be used for gesture control, and price $13!! Also, important is that it works in broadcast mode – sends reading to all and the receivers which understand what is received, do decryption

mi-band-1

casio_stb_1000

Pebble_watch_trio

Notes:

  • Taking into account that Bluetooth devices are quite cheap, we expect that in the close future it will be integrated in most home appliance devices – irons, kettles etc. so we can receive status from them and maybe switch off remotely.
  • Problem with watches which are launched by Apple or Android are using OSes which are over-complicated / bloatware, thus consuming much energy. For example, MIO can work 3 months without charging.
  • Watch should be set of sensors, and not replace or compete with smartphone.
  • Plus if we compare Bluetooth Low Energy watches with Apple iBeacon system, e.g. Xiaomi-like devices which are sending broadcast all the time and we can count for example, count of people inside the warehouse; iBeacon – we don’t see its activity, we need to activate them by app first and information is sent by WiFi.