Skip to main content

STN1110

Latest Version: 1.0.0

This library provides an interface to the STN1110 Multiprotocol OBD-II to UART Interpreter.

You can view the library’s source code on GitHub. Click here to see information on other versions of this library.

To add this library to your project, add #require "STN1110.class.nut:1.0.0" to the top of your device code

Classes

Two classes are implemented for interacting with the STN1110 over UART.

  1. STN1110 — a low-level wrapper for executing commands over UART and reading back results from the STN1110
  2. VehicleInterface — a high-level interface for accessing common vehicle data like speed, RPM, temperatures or other arbitrary OBD-II PIDs.

Examples

An example utilizing VehicleInterface and the Plotly library is provided. It logs several vehicle parameters over time and generates a plotly graph of their values.

Short examples for each class are provided below.

VehicleInterface

#require "STN1110.class.nut:1.0.0"

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 <- 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"]);
});

VehicleInterface Class Usage

Constructor: VehicleInterface(impUart, [baud])

To instantiate the VehicleInterface class, pass in the imp UART bus 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.

VehicleInterface Class Methods

read(pid, callback)

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.

subscribe(pid, callback, period)

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.

unsubscribe(pid)

Unsubscribes the callback, if any, for PID pid and stops requesting the PID.

VehicleInterface Supported PIDs

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.

STN1110 Class Usage

Constructor: STN110(uart, [baud])

To instantiate the class, pass in the imp UART bus 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.

STN1110 Class Methods

execute(command, timeout, callback)

Executes the command string command with the timeout timeout in seconds and calls callback with the result. The callback is called with one parameter: 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.

reset()

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.

setBaudRate(baud)

Sets the baud rate to baud. This is a blocking call. When it returns, the STN1110 is now operating at the specified baud rate or an exception will have been 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.

getBaudRate()

Returns the baud rate at which the UART interface is currently operating.

getElmVersion()

Returns the version string of the ELM emulator provided by the STN1110 on reset.

onError(callback)

Pass a callback function, callback, to be executed if an error occurs after initialization and no PID callbacks are registered to receive the error.

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release

License

The STN1110 library is licensed under the MIT License.