SlideShare une entreprise Scribd logo
1  sur  34
Aspirus
About Me
• iOS developer
• Runs Azukisoft Pte Ltd
• Author of
http://www.amazon.com/Objecti
ve-C-Memory-Management-
Essentials-Gibson-
ebook/dp/B00V8BMX7I/
What is the Aspirus Standing Desk?
• Standing Desk
• But Smarter
• https://www.indiegogo.com/p
rojects/aspirus-world-s-first-
smart-standing-workstation#/
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 (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
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 TCP only again. Damned!!)
• http://libmodbus.org/documentation/ (Again,
it supports TCP only. Double damned!!!!)
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
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
• 91E0 – CRC check
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
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
Bluetooth
• Then we do a connection
• - (void)connectPeripheral:(CBPeripheral
*)peripheral options:(NSDictionary<NSString
*, id> *)options
• Great. Now the easy part is done
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; 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;
• }
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 writeValue:data
forCharacteristic:write_characteristic
type:
CBCharacteristicWriteWithResponse];
Modbus
• Then in the callback function.
We await the response from
the device
• - (void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacte
ristic *)characteristic error:(NSError *)error
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
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
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
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
Aspirus
Aspirus
Aspirus

Contenu connexe

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

What's new in Xamarin.iOS, by Miguel de Icaza
What's new in Xamarin.iOS, by Miguel de IcazaWhat's new in Xamarin.iOS, by Miguel de Icaza
What's new in Xamarin.iOS, by Miguel de Icaza
Xamarin
 
Cs1123 11 pointers
Cs1123 11 pointersCs1123 11 pointers
Cs1123 11 pointers
TAlha MAlik
 
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsFI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS Basics
Petr Dvorak
 

Similaire à Modbus using Coreblue Bluetooth for iOS to connect to a hardware sensor (20)

DevOpsCon 2015 - DevOps in Mobile Games
DevOpsCon 2015 - DevOps in Mobile GamesDevOpsCon 2015 - DevOps in Mobile Games
DevOpsCon 2015 - DevOps in Mobile Games
 
From a student to an apache committer practice of apache io tdb
From a student to an apache committer  practice of apache io tdbFrom a student to an apache committer  practice of apache io tdb
From a student to an apache committer practice of apache io tdb
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
05 integrate redis
05 integrate redis05 integrate redis
05 integrate redis
 
¡El mejor lenguaje para automatizar pruebas!
¡El mejor lenguaje para automatizar pruebas!¡El mejor lenguaje para automatizar pruebas!
¡El mejor lenguaje para automatizar pruebas!
 
Toward Hybrid Cloud Serverless Transparency with Lithops Framework
Toward Hybrid Cloud Serverless Transparency with Lithops FrameworkToward Hybrid Cloud Serverless Transparency with Lithops Framework
Toward Hybrid Cloud Serverless Transparency with Lithops Framework
 
Dive Into Azure Data Lake - PASS 2017
Dive Into Azure Data Lake - PASS 2017Dive Into Azure Data Lake - PASS 2017
Dive Into Azure Data Lake - PASS 2017
 
Automate using Python
Automate using PythonAutomate using Python
Automate using Python
 
Iot meets Serverless
Iot meets ServerlessIot meets Serverless
Iot meets Serverless
 
Getting started with Appcelerator Titanium
Getting started with Appcelerator TitaniumGetting started with Appcelerator Titanium
Getting started with Appcelerator Titanium
 
What's new in Xamarin.iOS, by Miguel de Icaza
What's new in Xamarin.iOS, by Miguel de IcazaWhat's new in Xamarin.iOS, by Miguel de Icaza
What's new in Xamarin.iOS, by Miguel de Icaza
 
Getting started with titanium
Getting started with titaniumGetting started with titanium
Getting started with titanium
 
Cs1123 11 pointers
Cs1123 11 pointersCs1123 11 pointers
Cs1123 11 pointers
 
Null mumbai-iot-workshop
Null mumbai-iot-workshopNull mumbai-iot-workshop
Null mumbai-iot-workshop
 
Microsoft IoT & Data OpenHack Zürich
Microsoft IoT & Data OpenHack ZürichMicrosoft IoT & Data OpenHack Zürich
Microsoft IoT & Data OpenHack Zürich
 
Dino2 - the Amazing Evolution of the VA Smalltalk Virtual Machine
Dino2 - the Amazing Evolution of the VA Smalltalk Virtual MachineDino2 - the Amazing Evolution of the VA Smalltalk Virtual Machine
Dino2 - the Amazing Evolution of the VA Smalltalk Virtual Machine
 
One Shellcode to Rule Them All: Cross-Platform Exploitation
One Shellcode to Rule Them All: Cross-Platform ExploitationOne Shellcode to Rule Them All: Cross-Platform Exploitation
One Shellcode to Rule Them All: Cross-Platform Exploitation
 
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsFI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS Basics
 
Code and memory optimization tricks
Code and memory optimization tricksCode and memory optimization tricks
Code and memory optimization tricks
 
Code and Memory Optimisation Tricks
Code and Memory Optimisation Tricks Code and Memory Optimisation Tricks
Code and Memory Optimisation Tricks
 

Plus de Azukisoft Pte Ltd (8)

From Idea to App Store
From Idea to App StoreFrom Idea to App Store
From Idea to App Store
 
Ohoh
OhohOhoh
Ohoh
 
Intro to Laravel
Intro to LaravelIntro to Laravel
Intro to Laravel
 
Storyboard prototyping
Storyboard prototypingStoryboard prototyping
Storyboard prototyping
 
The good, bad and ugly of Digital Ocean vs AWS
The good, bad and ugly of Digital Ocean vs AWSThe good, bad and ugly of Digital Ocean vs AWS
The good, bad and ugly of Digital Ocean vs AWS
 
Presentation
PresentationPresentation
Presentation
 
Cocos2d for beginners
Cocos2d for beginnersCocos2d for beginners
Cocos2d for beginners
 
Mobile Marketing Dec2009
Mobile Marketing Dec2009Mobile Marketing Dec2009
Mobile Marketing Dec2009
 

Dernier

Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Cara Menggugurkan Kandungan 087776558899
 

Dernier (7)

Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
 
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
 
Bromazolam CAS 71368-80-4 high quality opiates, Safe transportation, 99% pure
Bromazolam CAS 71368-80-4 high quality opiates, Safe transportation, 99% pureBromazolam CAS 71368-80-4 high quality opiates, Safe transportation, 99% pure
Bromazolam CAS 71368-80-4 high quality opiates, Safe transportation, 99% pure
 
Android Application Components with Implementation & Examples
Android Application Components with Implementation & ExamplesAndroid Application Components with Implementation & Examples
Android Application Components with Implementation & Examples
 
Mobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s ToolsMobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s Tools
 
Mobile Application Development-Components and Layouts
Mobile Application Development-Components and LayoutsMobile Application Development-Components and Layouts
Mobile Application Development-Components and Layouts
 
Mobile App Penetration Testing Bsides312
Mobile App Penetration Testing Bsides312Mobile App Penetration Testing Bsides312
Mobile App Penetration Testing Bsides312
 

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

  • 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. What is the Aspirus Standing Desk? • Standing Desk • But Smarter • https://www.indiegogo.com/p rojects/aspirus-world-s-first- smart-standing-workstation#/
  • 4.
  • 5. Functionality • 8-in-1 SMART WORKSTATION with one-touch elevation, smart app, sit-stand reminder, tracking and more
  • 7. Technology Stack • Objective-C • Cocoapods • AFNetworking • Core Bluetooth
  • 8. Core Bluetooth • Desk uses a TI (Texas Instrument Controller) • With a BT sensor • And registers
  • 9. iPhone •What type of communication protocol to use? •JSON? XML? SOAP?
  • 10. Protocol • We settled on Modbus after discussion with the hardware engineer • WTF is Modbus?
  • 11. 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
  • 12. Modbus • Royalty free too • Which means someone wrote a objective-c library • Which we can use?
  • 15. 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!!!!)
  • 16. 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
  • 17. Modbus • 30 0300 2500 01 91E0
  • 18. Modbus • 30 0300 2500 01 91E0 • 30 – Random Tag • 0300 – Address of register • 2500 – Function to use • 01 – Data to send • 91E0 – CRC check
  • 19. 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
  • 20. 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
  • 21. Bluetooth • Then we do a connection • - (void)connectPeripheral:(CBPeripheral *)peripheral options:(NSDictionary<NSString *, id> *)options • Great. Now the easy part is done
  • 22. Modbus • So how we do send the data over? • Binary? • Text? • ???
  • 23. Modbus • Only way is using byte array • So we convert our NSString • Contain Hex values • To a byte array
  • 24. 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; • }
  • 25. Modbus • Then get the write characteristic of the BT device • And send the data to it
  • 26. Modbus • CBCharacteristic *write_characteristic = self.discoveredSevice.characteristics[1]; • [self.selectPeripheral writeValue:data forCharacteristic:write_characteristic type: CBCharacteristicWriteWithResponse];
  • 27. Modbus • Then in the callback function. We await the response from the device • - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacte ristic *)characteristic error:(NSError *)error
  • 28. 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
  • 29. 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
  • 30. 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
  • 31. 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