Table of Contents
📋 Requirements
Development Environment
- macOS Ventura or later
- Xcode 14.0+
- Apple Developer Account (for HomeKit)
- CocoaPods (optional)
Target Device
- iOS 16.1+ (Matter support)
- iPhone 15 series (or Thread border router)
- Physical device (Simulator won't work)
- Bluetooth enabled
Important: HomeKit capabilities require an Apple Developer account and cannot be tested in the iOS Simulator.
✨ App Features
🎛️ Basic Controls
- Enable/disable sensor
- Adjust intensity (0-254)
- View connection status
- Real-time stats display
🏋️ Workout Testing
- 10 pre-built scenarios
- Real-time progress
- Pause/resume/skip
- Quick test suite
🌐 Matter Integration
- HomeKit pairing
- Thread networking
- Siri commands
- Home automations
🛠️ Project Setup
Step 1: Create New Xcode Project
- Open Xcode
- File → New → Project
- Choose iOS → App
- Configure project:
- Product Name:
FitnessSensorController - Interface: SwiftUI
- Language: Swift
- Minimum iOS: 16.1
- Product Name:
Step 2: Add Source Files
Copy these files to your project:
| File | Description |
|---|---|
FitnessSensorControllerApp.swift |
Main app file with HomeKit integration |
WorkoutScenarios.swift |
Workout definitions and controller |
WorkoutScenarioView.swift |
UI components for workouts |
Info.plist |
App configuration and permissions |
Entitlements.entitlements |
HomeKit capabilities |
Step 3: Project Structure
FitnessSensorController/
├── FitnessSensorControllerApp.swift
├── Views/
│ ├── ContentView.swift
│ ├── DeviceControlView.swift
│ ├── WorkoutScenarioListView.swift
│ ├── WorkoutScenarioView.swift
│ └── WorkoutDetailView.swift
├── Models/
│ ├── HomeManager.swift
│ ├── WorkoutScenarios.swift
│ └── WorkoutController.swift
├── Info.plist
├── Entitlements.entitlements
└── Assets.xcassets/
⚙️ Configuration
A. Signing & Capabilities
- Select your project in Xcode
- Select target → Signing & Capabilities
- Enable Automatic Signing (or configure manual)
- Select your Team
- Add capabilities by clicking + Capability:
- ✅ HomeKit
- ✅ Background Modes (optional)
Note: HomeKit capability requires a paid Apple Developer account ($99/year).
B. Info.plist Configuration
Add these privacy descriptions:
<key>NSHomeKitUsageDescription</key>
<string>Control your fitness sensor via Matter</string>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Required for Matter device commissioning</string>
<key>NSLocalNetworkUsageDescription</key>
<string>Communicate with Matter devices on local network</string>
<key>NSBonjourServices</key>
<array>
<string>_matter._tcp</string>
<string>_matterc._udp</string>
</array>
C. Entitlements Configuration
Ensure Entitlements.entitlements contains:
<key>com.apple.developer.homekit</key>
<true/>
<key>com.apple.developer.matter.allow-setup-payload</key>
<true/>
🔨 Building & Running
Build the App
- Connect your iPhone via USB
- Select your device as the build target
- Press ⌘R or click Run
- Accept HomeKit permission when prompted
Common Build Errors
| Error | Solution |
|---|---|
| "HomeKit entitlement missing" | Add HomeKit capability in Signing & Capabilities |
| "Provisioning profile error" | Use Automatic Signing or create profile with HomeKit |
| "Code signing failed" | Check Developer account is valid and team selected |
| "Cannot run on Simulator" | Must use physical device for HomeKit testing |
🔗 Pairing Your Sensor
Prerequisites
- ✅ nRF52840 device flashed and powered on
- ✅ Thread border router active (HomePod mini/Apple TV)
- ✅ iPhone and device on same network
- ✅ Bluetooth enabled on iPhone
Option 1: Via App (Recommended)
- Open the app
- Tap "Get Started" (first launch)
- Tap "+" button in top-right
- Tap "Open Home Settings"
- In Settings → Home, tap "Add Accessory"
- Scan QR code OR enter code:
20202021-3840 - Follow on-screen instructions
- Name device: "Bike Sensor"
- Return to app - sensor should appear
Option 2: Via Home App
- Open Home app
- Tap "+" → "Add Accessory"
- Scan QR code or enter setup code
- Complete pairing process
- Open FitnessSensorController app
- Sensor appears automatically
Option 3: Via Settings
- Open Settings app
- Tap Home
- Select your home
- Tap "Add Accessory"
- Scan or enter code
Pairing Success Signs:
- Green checkmark in Home app
- Device appears in control app
- Status shows "Connected"
📲 Using the App
Main Control Screen
Status Indicator
- 🟢 Green heart = Active and broadcasting
- ⚪ Gray heart = Paused
- 🔴 "Sensor Offline" = Disconnected
Enable/Disable Sensor
Toggle the "Enable Sensor" switch:
- ON: Sensor broadcasts to Zwift/apps
- OFF: BLE broadcasting stops
Adjust Intensity
- Move the intensity slider (0-254)
- Values update immediately
- View estimated power and HR below
Intensity Mapping:
- 0 = 50W, 60 BPM
- 127 = 225W, 130 BPM (default)
- 254 = 400W, 200 BPM (max)
Test Workouts
- Tap "Test Workouts" button
- Browse 10 pre-built scenarios
- Tap any workout to view details
- Review segment breakdown
- Tap "Start Workout"
- Monitor real-time progress
- Use controls:
- Pause - Temporarily stop
- Stop - End workout early
- Skip - Jump to next segment
Quick Test Suite
- Tap "Test Workouts"
- Tap "Run Quick Test Suite"
- Automatically tests all scenarios (2 min)
- View pass/fail results
Siri Commands
Once paired, use voice commands:
"Hey Siri, turn on my fitness sensor"
"Hey Siri, turn off bike sensor"
"Hey Siri, set fitness sensor to 50%"
"Hey Siri, what's the status of my bike sensor?"
Home Automations
Create automations in Home app:
"Morning Workout" Automation
When: 6:00 AM (Weekdays)
Actions:
- Turn on Bike Sensor
- Set intensity to 100
- Turn on HomeKit lights
- Play workout playlist
"Start Cycling" Shortcut
1. Turn on Bike Sensor
2. Set intensity to 127
3. Wait 5 seconds
4. Open Zwift app
🐛 Troubleshooting
App Issues
| Problem | Solution |
|---|---|
| "Home Not Set Up" | Open Home app, create a home if needed, restart app |
| Can't find sensor | Check device powered on, Bluetooth enabled, HomeKit permissions granted |
| Sensor appears offline | Check Thread border router active, power cycle device, move iPhone closer |
| Controls don't work | Verify device shows "Connected", check toggle responds, review nRF52840 logs |
| Workout doesn't start | Ensure sensor connected and enabled, restart app if needed |
| Values not changing | Enable sensor, verify BLE connection, check intensity being set |
Pairing Issues
Can't Scan QR Code
- Enter manual code:
20202021-3840 - Ensure good lighting
- Clean camera lens
- Try different angle
Pairing Fails
- Reset device (power cycle)
- Remove from Home app first
- Check Thread network active
- Move closer to border router
Debug Mode
Enable verbose logging in Xcode:
// In HomeManager class
func setOnOff(for accessory: HMAccessory, enabled: Bool, completion: @escaping (Bool) -> Void) {
print("DEBUG: Setting power to \(enabled)")
print("DEBUG: Accessory: \(accessory.uniqueIdentifier)")
// ... existing code
}
View console output:
- Window → Devices and Simulators
- Select your device
- Click Open Console
🎨 Customization
Change Theme Colors
// In DeviceControlView
.tint(.green) // Change to .blue, .purple, etc.
Update Accent Color
- Open Assets.xcassets
- Select AccentColor
- Change color in Attributes Inspector
Add Custom Workouts
// In WorkoutScenarios.swift
static func customWorkout() -> WorkoutScenario {
let segments = [
WorkoutSegment(duration: 300, intensityLevel: 100, name: "Warm-up"),
WorkoutSegment(duration: 600, intensityLevel: 200, name: "Main Set"),
WorkoutSegment(duration: 300, intensityLevel: 70, name: "Cool-down")
]
return WorkoutScenario(type: .custom, segments: segments)
}
🚀 Advanced Features
HealthKit Integration (Future)
import HealthKit
func saveWorkout() {
let workout = HKWorkout(
activityType: .cycling,
start: startDate,
end: endDate
)
// Save to Health app
}
Apple Watch Companion (Future)
- Quick controls on wrist
- Real-time workout metrics
- Start/stop workouts
- View sensor status
📊 App Architecture
┌─────────────────────────────────────┐
│ SwiftUI Views │
│ (ContentView, DeviceControlView) │
└──────────────┬──────────────────────┘
│
┌──────────────┴──────────────────────┐
│ HomeManager │
│ (ObservableObject State Manager) │
└──────────────┬──────────────────────┘
│
┌──────────────┴──────────────────────┐
│ HMHomeManager │
│ (Apple HomeKit Framework) │
└──────────────┬──────────────────────┘
│
┌──────────────┴──────────────────────┐
│ Matter/Thread Protocol │
└──────────────┬──────────────────────┘
│
nRF52840 Device
✅ Development Checklist
- Xcode 14+ installed
- iOS 16.1+ device
- Apple Developer account
- Project created with correct settings
- Info.plist configured
- Entitlements added
- HomeKit capability enabled
- nRF52840 firmware flashed
- Device commissioned to HomeKit
- App builds successfully
- Sensor paired and visible
- Controls working
Ready to control your workout! 💪🚴♂️
Continue to Testing Guide to run automated test scenarios.