Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Aspirus
About Me
• iOS developer
• Runs Azukisoft Pte Ltd
• Author of
http://www.amazon.com/Objecti
ve-C-Memory-Management-
Essent...
What is the Aspirus Standing Desk?
• Standing Desk
• But Smarter
• https://www.indiegogo.com/p
rojects/aspirus-world-s-fir...
Functionality
• 8-in-1 SMART WORKSTATION
with one-touch elevation,
smart app, sit-stand reminder,
tracking and more
• https://www.youtube.com/watch?v
=H7b8GMvqi_E
Technology Stack
• Objective-C
• Cocoapods
• AFNetworking
• Core Bluetooth
Core Bluetooth
• Desk uses a TI (Texas
Instrument Controller)
• With a BT sensor
• And registers
iPhone
•What type of
communication protocol
to use?
•JSON? XML? SOAP?
Protocol
• We settled on Modbus after discussion with
the hardware engineer
• WTF is Modbus?
Modbus
• https://en.wikipedia.org/wiki/Modbus
• Modbus is a serial communications protocol
originally published by Modicon...
Modbus
• Royalty free too
• Which means someone wrote a objective-c
library
• Which we can use?
Modbus
WRONG!!!
Modbus
Modbus
• https://github.com/iUtvikler/ObjectiveLibMod
bus (Only support TCP)
• http://www.modbusapps.com/mbk.php
(Supports...
Modbus
• So we had no choice, but to write our own
implementation
• Look for online documentation
• Start hacking away
• h...
Modbus
• 30 0300 2500 01 91E0
Modbus
• 30 0300 2500 01 91E0
• 30 – Random Tag
• 0300 – Address of register
• 2500 – Function to use
• 01 – Data to send
...
Bluetooth
• CRC calculated using some arcane formula
involving bit shifting, blood of your first born
and some magical inc...
Bluetooth
• After calculation, how do we send it to the
controller on the desk?
• 1st, we scan for a peripherals and then ...
Bluetooth
• Then we do a connection
• - (void)connectPeripheral:(CBPeripheral
*)peripheral options:(NSDictionary<NSString
...
Modbus
• So how we do send the data
over?
• Binary?
• Text?
• ???
Modbus
• Only way is using byte array
• So we convert our NSString
• Contain Hex values
• To a byte array
Modbus
• - (NSData *) convert_to_byte_array:(NSString *) str {
• const char *chars = [str UTF8String];
• int i = 0; NSUInt...
Modbus
• Then get the write
characteristic of the BT device
• And send the data to it
Modbus
• CBCharacteristic *write_characteristic =
self.discoveredSevice.characteristics[1];
• [self.selectPeripheral write...
Modbus
• Then in the callback function.
We await the response from
the device
• - (void)peripheral:(CBPeripheral *)periphe...
Modbus
• But wait, there’s more!!!
• Remember that random tag?
• 30030025000191E0
• 30 – Random Tag
• 0300 – Address of re...
Modbus
• In our
didUpdateValueForCharacteri
stic function, we have to keep
writing the same data and
check the random tag ...
Modbus
1. Write random tag and value to
characteristic
2. Await for response
3. Get the random tag from response
4. If tag...
Modbus
1. So once all these are done
2. We reused the code so that we
are able to get various sensor
data from the device
...
Aspirus
Aspirus
Aspirus
Modbus using Coreblue Bluetooth for iOS to connect to a hardware sensor
Upcoming SlideShare
Loading in …5
×

Modbus using Coreblue Bluetooth for iOS to connect to a hardware sensor

899 views

Published on

Using core bluetooth to connect to a hardware sensor on a standing desk

Published in: Mobile
  • Be the first to comment

Modbus using Coreblue Bluetooth for iOS to connect to a hardware sensor

  1. 1. Aspirus
  2. 2. About Me • iOS developer • Runs Azukisoft Pte Ltd • Author of http://www.amazon.com/Objecti ve-C-Memory-Management- Essentials-Gibson- ebook/dp/B00V8BMX7I/
  3. 3. What is the Aspirus Standing Desk? • Standing Desk • But Smarter • https://www.indiegogo.com/p rojects/aspirus-world-s-first- smart-standing-workstation#/
  4. 4. Functionality • 8-in-1 SMART WORKSTATION with one-touch elevation, smart app, sit-stand reminder, tracking and more
  5. 5. • https://www.youtube.com/watch?v =H7b8GMvqi_E
  6. 6. Technology Stack • Objective-C • Cocoapods • AFNetworking • Core Bluetooth
  7. 7. Core Bluetooth • Desk uses a TI (Texas Instrument Controller) • With a BT sensor • And registers
  8. 8. iPhone •What type of communication protocol to use? •JSON? XML? SOAP?
  9. 9. Protocol • We settled on Modbus after discussion with the hardware engineer • WTF is Modbus?
  10. 10. Modbus • https://en.wikipedia.org/wiki/Modbus • Modbus is a serial communications protocol originally published by Modicon (now Schneider Electric) in 1979 for use with its programmable logic controllers • Simple and robust, it has since become a de facto standard communication protocol, and it is now a commonly available means of connecting industrial electronic devices
  11. 11. Modbus • Royalty free too • Which means someone wrote a objective-c library • Which we can use?
  12. 12. Modbus WRONG!!!
  13. 13. Modbus
  14. 14. Modbus • https://github.com/iUtvikler/ObjectiveLibMod bus (Only support TCP) • http://www.modbusapps.com/mbk.php (Supports TCP only again. Damned!!) • http://libmodbus.org/documentation/ (Again, it supports TCP only. Double damned!!!!)
  15. 15. Modbus • So we had no choice, but to write our own implementation • Look for online documentation • Start hacking away • http://www.modbustools.com/modbus.html
  16. 16. Modbus • 30 0300 2500 01 91E0
  17. 17. Modbus • 30 0300 2500 01 91E0 • 30 – Random Tag • 0300 – Address of register • 2500 – Function to use • 01 – Data to send • 91E0 – CRC check
  18. 18. Bluetooth • CRC calculated using some arcane formula involving bit shifting, blood of your first born and some magical incantations • http://www.ccontrolsys.com/w/How_to_Com pute_the_Modbus_RTU_Message_CRC
  19. 19. Bluetooth • After calculation, how do we send it to the controller on the desk? • 1st, we scan for a peripherals and then get an array of peripherals • retrieveConnectedPeripheralsWithServices returns a NSArray of peripherals
  20. 20. Bluetooth • Then we do a connection • - (void)connectPeripheral:(CBPeripheral *)peripheral options:(NSDictionary<NSString *, id> *)options • Great. Now the easy part is done
  21. 21. Modbus • So how we do send the data over? • Binary? • Text? • ???
  22. 22. Modbus • Only way is using byte array • So we convert our NSString • Contain Hex values • To a byte array
  23. 23. Modbus • - (NSData *) convert_to_byte_array:(NSString *) str { • const char *chars = [str UTF8String]; • int i = 0; NSUInteger len = str.length; • • NSMutableData *data = [NSMutableData dataWithCapacity:len / 2]; • char byteChars[3] = {'0','0','0'}; • unsigned long wholeByte; • • while (i < len) { • byteChars[0] = chars[i++]; • byteChars[1] = chars[i++]; • wholeByte = strtoul(byteChars, NULL, 16); • [data appendBytes:&wholeByte length:1]; • } • return data; • }
  24. 24. Modbus • Then get the write characteristic of the BT device • And send the data to it
  25. 25. Modbus • CBCharacteristic *write_characteristic = self.discoveredSevice.characteristics[1]; • [self.selectPeripheral writeValue:data forCharacteristic:write_characteristic type: CBCharacteristicWriteWithResponse];
  26. 26. Modbus • Then in the callback function. We await the response from the device • - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacte ristic *)characteristic error:(NSError *)error
  27. 27. Modbus • But wait, there’s more!!! • Remember that random tag? • 30030025000191E0 • 30 – Random Tag • 0300 – Address of register • 2500 – Function to use • 01 – Data to send • 91E0 – CRC check
  28. 28. Modbus • In our didUpdateValueForCharacteri stic function, we have to keep writing the same data and check the random tag matches what was sent to the device
  29. 29. Modbus 1. Write random tag and value to characteristic 2. Await for response 3. Get the random tag from response 4. If tag does not match what was sent in step #1 5. Rinse and repeat step #1 until it does
  30. 30. Modbus 1. So once all these are done 2. We reused the code so that we are able to get various sensor data from the device 3. Desk up/down position, adjust desk light, detection of user presense etc
  31. 31. Aspirus
  32. 32. Aspirus
  33. 33. Aspirus

×