Upload
azukisoft-pte-ltd
View
606
Download
1
Embed Size (px)
Citation preview
OHOH
Aspirus
About MeiOS developerRuns Azukisoft Pte LtdAuthor of http://www.amazon.com/Objective-C-Memory-Management-Essentials-Gibson-ebook/dp/B00V8BMX7I/
What is the Aspirus Standing Desk?Standing DeskBut Smarterhttps://www.indiegogo.com/projects/aspirus-world-s-first-smart-standing-workstation#/
Functionality8-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 StackObjective-CCocoapodsAFNetworkingCore Bluetooth
Core BluetoothDesk uses a TI (Texas Instrument Controller)With a BT sensorAnd registers
iPhone
What type of communication protocol to use?JSON? XML? SOAP?
ProtocolWe settled on Modbus after discussion with the hardware engineer
WTF is Modbus?
Modbushttps://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
ModbusRoyalty free tooWhich means someone wrote a objective-c libraryWhich we can use?
ModbusWRONG!!!
Modbus
Modbushttps://github.com/iUtvikler/ObjectiveLibModbus (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!!!!)
ModbusSo we had no choice, but to write our own implementationLook for online documentationStart hacking awayhttp://www.modbustools.com/modbus.html
Modbus30 0300 2500 01 91E0
Modbus30 0300 2500 01 91E0
30 Random Tag0300 Address of register2500 Function to use01 Data to send91E0 CRC check
BluetoothCRC calculated using some arcane formula involving bit shifting, blood of your first born and some magical incantationshttp://www.ccontrolsys.com/w/How_to_Compute_the_Modbus_RTU_Message_CRC
BluetoothAfter calculation, how do we send it to the controller on the desk?1st, we scan for a peripherals and then get an array of peripheralsretrieveConnectedPeripheralsWithServices returns a NSArray of peripherals
BluetoothThen we do a connection- (void)connectPeripheral:(CBPeripheral *)peripheral options:(NSDictionary *)options
Great. Now the easy part is done
ModbusSo how we do send the data over? Binary?Text????
ModbusOnly way is using byte arraySo we convert our NSStringContain Hex valuesTo 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;}
ModbusThen get the write characteristic of the BT deviceAnd send the data to it
Modbus CBCharacteristic *write_characteristic = self.discoveredSevice.characteristics[1];
[self.selectPeripheral writeValue:data forCharacteristic:write_characteristic type: CBCharacteristicWriteWithResponse];
ModbusThen in the callback function. We await the response from the device- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
ModbusBut wait, theres more!!!Remember that random tag?30030025000191E030 Random Tag0300 Address of register2500 Function to use01 Data to send91E0 CRC check
ModbusIn our didUpdateValueForCharacteristic function, we have to keep writing the same data and check the random tag matches what was sent to the device
ModbusWrite random tag and value to characteristicAwait for responseGet the random tag from responseIf tag does not match what was sent in step #1Rinse and repeat step #1 until it does
ModbusSo once all these are doneWe reused the code so that we are able to get various sensor data from the deviceDesk up/down position, adjust desk light, detection of user presense etc
Aspirus
Aspirus
Aspirus