-
Notifications
You must be signed in to change notification settings - Fork 12
Home
SwiftOBD2 is a Swift package that simplifies communication with vehicles using an ELM327 OBD2 adapter. It provides a straightforward and powerful interface for interacting with your vehicle's onboard diagnostics system, allowing you to retrieve real-time data and perform diagnostics.
- Connection Management:
- Establishes connections to the OBD2 adapter via Bluetooth or Wi-Fi.
- Handles adapter initialization and vehicle connection process.
- Manages connection states (disconnected, connectedToAdapter, connectedToVehicle).
- Command Interface: Send and receive OBD2 commands for interacting with your vehicle.
- Data Retrieval:
- Supports requests for real-time vehicle data (RPM, speed, etc.) using standard OBD2 PIDs (Parameter IDs).
- Provides functions to continuously poll and retrieve updated measurements.
- Can retrieve a list of supported PIDs from the vehicle.
- Diagnostics:
- Retrieves and clears diagnostic trouble codes (DTCs).
- Gets the overall status of the vehicle's onboard systems.
- Sensor Monitoring: Retrieve and view data from various vehicle sensors in real time.
- Adaptability and Configuration:
- Seamless switching between Bluetooth and Wi-Fi communication.
- Allows for testing and development with a demo mode.
iOS 14.0+ / macOS 11.0+ Xcode 13.0+ Swift 5.0+ Installation To install SwiftOBD2 in your Swift project, use the Swift Package Manager:
In Xcode, go to File > Add Packages... Enter the repository URL: https://github.com/kkonteh97/SwiftOBD2 Select the desired dependency rule (version, branch, or commit).
- Import and Setup:
- Import the necessary modules:
import SwiftUI
import SwiftOBD2
import Combine
```
2. Create a ViewModel:
* Create a ViewModel class conforming to the ObservableObject protocol.
* Inside the ViewModel:
Define a @Published property to store collected data.
Initialize an OBDService instance, setting the desired connection type.
Set the ViewModel as the delegate of OBDService.
Handle Connection State Changes:
Implement the connectionStateChanged method from the OBDServiceDelegate protocol.
Update the UI or handle logic based on connection state changes.
Start and Stop Connection:
Create functions to initiate and terminate the connection process with the OBD-II adapter.
Retrieve Information:
Use OBDService methods to retrieve data from the vehicle, such as:
getTroubleCodes: Retrieve diagnostic trouble codes (DTCs).
getStatus: Retrieve vehicle status since DTCs were cleared.
Continuous Updates:
Use startContinuousUpdates to continuously poll and retrieve updated measurements.
Subscribe to the returned Combine publisher for updates.
Example Code
```
class ViewModel: OBDServiceDelegate, ObservableObject {
// ... (properties and initialization)
func connectionStateChanged(state: ConnectionState) {
// ... (handle connection state changes)
}
func startContinousUpdates() {
obdService.startContinuousUpdates([.mode1(.rpm)]) // Add more PIDs as needed
.sink { completion in
// ... (handle completion)
} receiveValue: { measurements in
// ... (update measurements)
}
.store(in: &cancellables)
}
// ... (other functions for connection and data retrieval)
}
```
## Important Considerations
Compatible Adapter: Ensure you have a compatible ELM327 OBD2 adapter.
Permissions: Request Bluetooth permissions if using Bluetooth communication.
Error Handling: Implement robust error handling for potential communication issues.
Background Updates: Explore iOS background fetch capabilities for background data updates.
Contributing
Contributions are welcome! Open issues for bug reports or feature requests. To contribute code, follow the standard fork, branch, commit, and pull request workflow.
## License
SwiftOBD2 is distributed under the MIT license. See the LICENSE file for details.
Additional Resources GitHub Repository Sample App This wiki entry provides a basic overview of SwiftOBD2. For more detailed information and examples, refer to the README and sample app linked above.