Thanks to visit codestin.com
Credit goes to www.slideshare.net

Swift Hardware Hacking
Sally Shepard

@mostgood
First Local Cat Savings Bank!
Congratulations on joining
@mostgood
Video of coin bank
Savings account v1.0
@mostgood
Savings account v2.0
@mostgood
Ready for deposit
£4.85
How to: Build the smart savings account
@mostgood
Step 0: Setup development environment
@mostgood
"
@mostgood
@mostgood
@mostgood
Install Swift on Raspberry Pi
curl -s https://packagecloud.io/install/
repositories/swift-arm/release/script.deb.sh |
sudo bash
sudo apt install swift4 //if on 3B+
sudo apt install swift4rpi01 //if using a Zero
@mostgood
Create a project
mkdir ~/Documents/<project-name>
cd ~/Documents/<project-name>
swift package init --type executable
Setting up Geany for build/run
Reference: https://www.geany.org/manual/current/index.html
@mostgood
Step 1: Controlling power to the bank
@mostgood
v1
#$
v2
% &
@mostgood
Circuits
@mostgood
Circuits
@mostgood
@mostgood
+ — + —
GPIOHeader
@mostgood
Dependencies ->
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: "1.0.0")]
.target( name: "GeePeeEyeOh",
dependencies: ["SwiftyGPIO"])
Reference: https://github.com/uraimo/SwiftyGPIO
@mostgood
@mostgood
@mostgood
Relay.swift
import SwiftyGPIO
struct Relay {
var state: State {
get {
if let pin = self.gpio {
switch pin.value {
case 1:
return .On
default:
return .Off
}
}
return .Off
}
}
private var gpio: RaspberryGPIO?
init() {
setup()
}
init(connectTo pin: RaspberryGPIO) {
self.gpio = pin
setup()
}
private mutating func setup() {
if self.gpio == nil {
let gpios = SwiftyGPIO.GPIOs(for: .RaspberryPiPlusZero)
self.gpio = gpios[.P4] as? RaspberryGPIO
}
if let gpio = self.gpio {
gpio.direction = .OUT
}
}
public func switchTo(_ state: State) {
if let gpio = self.gpio {
gpio.value = state.rawValue
}
}
}
@mostgood
Digital Signal
0
1
1 = 3.3v 1 = 3.3v
0 = 0v 0 = 0v
@mostgood
Step 2: tell when the lid is open
@mostgood
@mostgood
@mostgood
Analogue Signal
0
128
256
384
512
640
768
896
1024
768
140
@mostgood
@mostgood
@mostgood
Dependencies -> MCP3008.swift
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: “1.0.0”),
.package(url: "https://github.com/uraimo/MCP3008.swift.git", from: “2.0.0")]
.target( name: "GeePeeEyeOh",
dependencies: [“SwiftyGPIO”, “MCP3008”])
Reference: https://github.com/uraimo/MCP3008
@mostgood
import SwiftyGPIO
struct Photoresistor {
private var maxLightThreshold: UInt32 = 0
private var minLightThreshhold: UInt32 = 1024
private var tolerance: UInt32 = 10
var lightReading: UInt32 = 1024
var light: Bool {
get {
let lightRange = 0...(lightReading + tolerance)
return lightRange.contains(lightReading)
}
}
init(light minLightThreshhold: UInt32,
dark maxLightThreshold: UInt32,
tolerance tolerance: UInt32) {
self.minLightThreshhold = minLightThreshhold
self.maxLightThreshold = maxLightThreshold
self.tolerance = tolerance
self.init()
}
}
Photoresistor.
swift
@mostgood
Step 3: figuring out what coin was deposited
@mostgood
coin weight = currentDeposit - previousDeposit
@mostgood
@mostgood
@mostgood
Red E+
Black E-
White A-
Green A+
Ground
DF
SCK
Vcc
Datasheets!
@mostgood
@mostgood
@mostgood
@mostgood
Digital Signal
0
1
1 = 3.3v 1 = 3.3v
0 = 0v 0 = 0v
@mostgood
Analogue Signal
0
128
256
384
512
640
768
896
1024
768
140
@mostgood
Analogue to Digital?
140
768
@mostgood
Analogue to Digital?
140
768
0000 1000 1100
0011 0000 0000
@mostgood
Analogue to Digital?
0000 1000 1100
0011 0000 0000
@mostgood
Analogue to Digital?
0 0 0 0 0 0 1 0 1 0 0 1
word length = 12 bits
@mostgood
@mostgood
@mostgood
@mostgood
@mostgood
two’s complement
import SwiftGPIO
struct HX711 {
var dt: RaspberryGPIO?
var sck: RaspberryGPIO?
init() {
let gpios = SwiftyGPIO.GPIOs(for:.RaspberryPiPlusZero)
dt = gpios[.P5] as? RaspberryGPIO
sck = gpios[.P6] as? RaspberryGPIO
self.setup()
}
init(data dt: RaspberryGPIO, clock ck: RaspberryGPIO) {
self.dt = dt
self.sck = ck
self.setup()
}
private func setup() {
if let dt = self.dt, let sck = self.sck {
dt.direction = .IN
sck.direction = .OUT
}
}
// read from the Data pin
public func readData() -> Int {
if let dt = self.dt {
if dt.value == 0 {
for _ in 0..<24 {
self. sck.value = 1
if let dt = self.dt {
//MAKE BITS DO THINGS HERE
}
self. sck.value = 0
}
} else {
print("Not Ready")
}
}
}
}
HX711.swift
@mostgood
@mostgood
@UIKitten
Step 5: Bluetooth connection to iOS
@mostgood
Bluetooth Low Energy
“Central”
client
“Peripheral”
server
GATT Profile
0x180A
“Device Information”
• Manufacturer Name
• Model Number
• Serial Number
• Hardware/Firmware/Software
Revision
• System ID
• Certification List
• Device ID
GATT Profile
0x180F
“Battery Service”
• Battery Level
• Level Changed
Notify
“Battery Level
Changed”
• Battery Percentage
(optional)
Write
“User Data - First Name”
Read
“Device Information -
Serial Number”
@mostgood
Bluetooth Low Energy
Core
Bluetooth
API
PureSwift/Bluetooth
PureSwift/BluetoothLinux
PureSwift/GATT
@mostgood
Bluetooth Low Energy
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: “1.0.0”),
.package(url: "https://github.com/uraimo/MCP3008.swift.git", from: “2.0.0”),
.package(url: "https://github.com/PureSwift/BluetoothLinux.git", from: “3.0.0"),
.package(url: "https://github.com/PureSwift/GATT.git", from : "1.0.0")]
.target( name: "GeePeeEyeOh",
dependencies: [“SwiftyGPIO”, “MCP3008”, “BluetoothLinux”, “GATT”])
Reference: https://github.com/PureSwift/BluetoothLinux https://github.com/PureSwift/GATT
@mostgood
Core Bluetooth
// Core Bluetooth Manager
var centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
centralManager?.scanForPeripherals(withServices: nil, options: nil)
// Delegate Methods for Bluetooth Central Manager
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData:
[String : Any], rssi RSSI: NSNumber)
// Delegate methods for connected peripherals
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)
// Peripheral methods
peripheral.writeValue(data, for: descriptor)
peripheral.readValue(for: descriptor)
peripheral.setNotifyValue(true, for: characteristic)
@mostgood
Bluetooth Linux
// Instantiate controller & peripheral
let controller = HostController.default
let peripheral = PeripheralManager(controller: controller)
// configure service(s)
let service = BluetoothUUID(rawValue: “180A”), // Hex value of GATT Service
let controllerType = serviceControllers.first(where: { $0.service == service })
let serviceController = try controllerType.init(peripheral: peripheral)
// start the peripheral
try peripheral.start()
Reference: https://github.com/MillerTechnologyPeru/gattserver
@mostgood
Final product
@mostgood
@mostgood
Thank you for listening!
Sally Shepard

@mostgood

Swift hardware hacking @ try! Swift

  • 1.
  • 2.
    First Local CatSavings Bank! Congratulations on joining @mostgood
  • 3.
    Video of coinbank Savings account v1.0 @mostgood
  • 4.
  • 5.
    How to: Buildthe smart savings account @mostgood
  • 6.
    Step 0: Setupdevelopment environment @mostgood
  • 7.
  • 8.
  • 9.
  • 10.
    Install Swift onRaspberry Pi curl -s https://packagecloud.io/install/ repositories/swift-arm/release/script.deb.sh | sudo bash sudo apt install swift4 //if on 3B+ sudo apt install swift4rpi01 //if using a Zero @mostgood Create a project mkdir ~/Documents/<project-name> cd ~/Documents/<project-name> swift package init --type executable
  • 11.
    Setting up Geanyfor build/run Reference: https://www.geany.org/manual/current/index.html @mostgood
  • 12.
    Step 1: Controllingpower to the bank @mostgood
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
    Dependencies -> dependencies: [ //Dependencies declare other packages that this package depends on. .package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: "1.0.0")] .target( name: "GeePeeEyeOh", dependencies: ["SwiftyGPIO"]) Reference: https://github.com/uraimo/SwiftyGPIO @mostgood
  • 19.
  • 20.
  • 21.
    Relay.swift import SwiftyGPIO struct Relay{ var state: State { get { if let pin = self.gpio { switch pin.value { case 1: return .On default: return .Off } } return .Off } } private var gpio: RaspberryGPIO? init() { setup() } init(connectTo pin: RaspberryGPIO) { self.gpio = pin setup() } private mutating func setup() { if self.gpio == nil { let gpios = SwiftyGPIO.GPIOs(for: .RaspberryPiPlusZero) self.gpio = gpios[.P4] as? RaspberryGPIO } if let gpio = self.gpio { gpio.direction = .OUT } } public func switchTo(_ state: State) { if let gpio = self.gpio { gpio.value = state.rawValue } } } @mostgood
  • 22.
    Digital Signal 0 1 1 =3.3v 1 = 3.3v 0 = 0v 0 = 0v @mostgood
  • 23.
    Step 2: tellwhen the lid is open @mostgood
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
    Dependencies -> MCP3008.swift dependencies:[ // Dependencies declare other packages that this package depends on. .package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: “1.0.0”), .package(url: "https://github.com/uraimo/MCP3008.swift.git", from: “2.0.0")] .target( name: "GeePeeEyeOh", dependencies: [“SwiftyGPIO”, “MCP3008”]) Reference: https://github.com/uraimo/MCP3008 @mostgood
  • 30.
    import SwiftyGPIO struct Photoresistor{ private var maxLightThreshold: UInt32 = 0 private var minLightThreshhold: UInt32 = 1024 private var tolerance: UInt32 = 10 var lightReading: UInt32 = 1024 var light: Bool { get { let lightRange = 0...(lightReading + tolerance) return lightRange.contains(lightReading) } } init(light minLightThreshhold: UInt32, dark maxLightThreshold: UInt32, tolerance tolerance: UInt32) { self.minLightThreshhold = minLightThreshhold self.maxLightThreshold = maxLightThreshold self.tolerance = tolerance self.init() } } Photoresistor. swift @mostgood
  • 31.
    Step 3: figuringout what coin was deposited @mostgood
  • 32.
    coin weight =currentDeposit - previousDeposit @mostgood
  • 33.
  • 34.
    @mostgood Red E+ Black E- WhiteA- Green A+ Ground DF SCK Vcc
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
    Digital Signal 0 1 1 =3.3v 1 = 3.3v 0 = 0v 0 = 0v @mostgood
  • 40.
  • 41.
  • 42.
    Analogue to Digital? 140 768 00001000 1100 0011 0000 0000 @mostgood
  • 43.
    Analogue to Digital? 00001000 1100 0011 0000 0000 @mostgood
  • 44.
    Analogue to Digital? 00 0 0 0 0 1 0 1 0 0 1 word length = 12 bits @mostgood
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
    import SwiftGPIO struct HX711{ var dt: RaspberryGPIO? var sck: RaspberryGPIO? init() { let gpios = SwiftyGPIO.GPIOs(for:.RaspberryPiPlusZero) dt = gpios[.P5] as? RaspberryGPIO sck = gpios[.P6] as? RaspberryGPIO self.setup() } init(data dt: RaspberryGPIO, clock ck: RaspberryGPIO) { self.dt = dt self.sck = ck self.setup() } private func setup() { if let dt = self.dt, let sck = self.sck { dt.direction = .IN sck.direction = .OUT } } // read from the Data pin public func readData() -> Int { if let dt = self.dt { if dt.value == 0 { for _ in 0..<24 { self. sck.value = 1 if let dt = self.dt { //MAKE BITS DO THINGS HERE } self. sck.value = 0 } } else { print("Not Ready") } } } } HX711.swift @mostgood
  • 50.
  • 51.
    Step 5: Bluetoothconnection to iOS @mostgood
  • 52.
    Bluetooth Low Energy “Central” client “Peripheral” server GATTProfile 0x180A “Device Information” • Manufacturer Name • Model Number • Serial Number • Hardware/Firmware/Software Revision • System ID • Certification List • Device ID GATT Profile 0x180F “Battery Service” • Battery Level • Level Changed Notify “Battery Level Changed” • Battery Percentage (optional) Write “User Data - First Name” Read “Device Information - Serial Number” @mostgood
  • 53.
  • 54.
    Bluetooth Low Energy dependencies:[ // Dependencies declare other packages that this package depends on. .package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: “1.0.0”), .package(url: "https://github.com/uraimo/MCP3008.swift.git", from: “2.0.0”), .package(url: "https://github.com/PureSwift/BluetoothLinux.git", from: “3.0.0"), .package(url: "https://github.com/PureSwift/GATT.git", from : "1.0.0")] .target( name: "GeePeeEyeOh", dependencies: [“SwiftyGPIO”, “MCP3008”, “BluetoothLinux”, “GATT”]) Reference: https://github.com/PureSwift/BluetoothLinux https://github.com/PureSwift/GATT @mostgood
  • 55.
    Core Bluetooth // CoreBluetooth Manager var centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main) centralManager?.scanForPeripherals(withServices: nil, options: nil) // Delegate Methods for Bluetooth Central Manager func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) // Delegate methods for connected peripherals func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) // Peripheral methods peripheral.writeValue(data, for: descriptor) peripheral.readValue(for: descriptor) peripheral.setNotifyValue(true, for: characteristic) @mostgood
  • 56.
    Bluetooth Linux // Instantiatecontroller & peripheral let controller = HostController.default let peripheral = PeripheralManager(controller: controller) // configure service(s) let service = BluetoothUUID(rawValue: “180A”), // Hex value of GATT Service let controllerType = serviceControllers.first(where: { $0.service == service }) let serviceController = try controllerType.init(peripheral: peripheral) // start the peripheral try peripheral.start() Reference: https://github.com/MillerTechnologyPeru/gattserver @mostgood
  • 57.
  • 58.
  • 59.
    Thank you forlistening! Sally Shepard @mostgood