This library provides an interface to the STN1110 Multiprotocol OBD-II to UART Interpreter.
Two classes are implemented for interacting with the STN1110 over UART.
- STN1110 is a low-level wrapper for executing commands over UART and reading back results from the STN1110.
- VehicleInterface is a high-level interface for accessing common vehicle data like speed, RPM, temperatures or other arbitrary OBD-II PIDs.
An example utilizing VehicleInterface and the Plotly library is provided. The Plotly example logs several vehicle parameters over time and generates a plotly graph of their values.
Short examples for each class are provided below.
car <- VehicleInterface(hardware.uart57);
// logs vehicle speed once per second
car.subscribe(car.VEHICLE_SPEED, function(result) {
if ("err" in result) {
server.error("Error getting vehicle speed");
return;
}
server.log("Current speed: " + result["msg"] + "km/h")
}, 1);
stn1110 <- STN1110(hardware.uart57)
stn1110.execute("AT@1", 1, function(result) { // AT@1 command returns device description string
if ("err" in result) {
server.error("Error executing command")
return
}
server.log("Device description: " + result["msg"])
})
To instantiate the class, pass in the imp UART that the STN1110 is connected to. The UART will be reconfigured by the constructor for communication with the STN1110. This is a blocking call that will return when the STN1110 interface is ready to use. This method may throw an exception if initializing the device fails or times out. The optional baud parameter can be used for initial connection if the STN1110 has a baud rate other than the default 9600 stored in its EEPROM.
Reads a PID once and executes callback with the resulting data. If the PID is in the list of supported PIDs, the callback will be called with a single value in the correct units. If the PID is not supported the callback will be called with a byte array containing the raw result of the request.
Reads a PID every 'period' seconds and executes callback with the resulting data. If the PID is in the list of supported PIDs, the callback will be called with a single value in the correct units. If the PID is not supported the callback will be called with a byte array containing the raw result of the request.
Unsubscribes the callback, if any, for PID pid and stops requesting the PID.
- VehicleInterface.ENGINE_RPM — The engine's RPM in units RPM.
- VehicleInterface.VEHICLE_SPEED — Get the vehicle speed in units km/h.
- VehicleInterface.THROTTLE_POSITION — The throttle position as a percentage.
- VehicleInterface.COOLANT_TEMPERATURE — The engine coolant temperature in degrees celsius.
- VehicleInterface.FUEL_PRESSURE — The fuel pressure in kPa.
- VehicleInterface.INTAKE_AIR_TEMPERATURE — The intake air temperature in degrees celsius.
- VehicleInterface.ENGINE_RUNTIME — The runtime since engine start in minutes.
To instantiate the class, pass in the imp UART that the STN1110 is connected to. The UART will be reconfigured by the constructor for communication with the STN1110. This is a blocking call that will return when the STN1110 interface is ready to use. This method may throw an exception if initializing the device fails or times out. The optional baud parameter can be used for initial connection if the STN1110 has a baud rate other than the default 9600 stored in it's EEPROM.
Executes the command string command with timeout timeout seconds and calls callback with the result. Callback is called with one parameter that is a table containing either an err key or a msg key. If the err key is present in the table an error occured during command execution and the corresponding value describes the error. If the err key is not present in the table the msg value will contain the output of the command.
Performs a soft reset of the STN1110. This is a blocking call that will return when the STN1110 interface is ready to use. This method may throw an exception if initializing the device fails or times out.
Sets the baud rate to baud. This is a blocking call, when it returns the STN1110 is now operating at the new baud rate, unless an exception is thrown. Exceptions will be thrown if commands are currently executing, if the baud rate is deemed invalid by the STN1110 or if another error occurs while setting the baud rate.
Returns the baud rate the uart interface is currently operating at.
Returns the version string of the ELM emulator provided by the STN1110 on reset.
Pass a callback function to be called if an error occurs after initialization and no PID callbacks are registered to receive the error.
The STN1110 library is licensed under the MIT License.