OBDII Click demo application is developed using the NECTO Studio, ensuring compatibility with mikroSDK's open-source libraries and tools. Designed for plug-and-play implementation and testing, the demo is fully compatible with all development, starter, and mikromedia boards featuring a mikroBUS™ socket.
- Author : Stefan Filipovic
- Date : Jul 2023.
- Type : UART type
This example demonstrates the use of OBDII Click board by reading the engine RPM and vehicle speed and displaying results on the USB UART.
- MikroSDK.Board
- MikroSDK.Log
- Click.OBDII
obdii_cfg_setupConfig Object Initialization function.
void obdii_cfg_setup ( obdii_cfg_t *cfg );obdii_initInitialization function.
err_t obdii_init ( obdii_t *ctx, obdii_cfg_t *cfg );obdii_send_commandThis function sends command string by using UART serial interface.
void obdii_send_command ( obdii_t *ctx, uint8_t *cmd );obdii_generic_readThis function reads a desired number of data bytes by using UART serial interface.
err_t obdii_generic_read ( obdii_t *ctx, uint8_t *data_out, uint16_t len );obdii_reset_deviceThis function resets the device by toggling the RST pin.
void obdii_reset_device ( obdii_t *ctx );Initializes the driver and performs the Click default configuration.
void application_init ( void )
{
log_cfg_t log_cfg; /**< Logger config object. */
obdii_cfg_t obdii_cfg; /**< Click config object. */
/**
* Logger initialization.
* Default baud rate: 115200
* Default log level: LOG_LEVEL_DEBUG
* @note If USB_UART_RX and USB_UART_TX
* are defined as HAL_PIN_NC, you will
* need to define them manually for log to work.
* See @b LOG_MAP_USB_UART macro definition for detailed explanation.
*/
LOG_MAP_USB_UART( log_cfg );
log_init( &logger, &log_cfg );
log_info( &logger, " Application Init " );
// Click initialization.
obdii_cfg_setup( &obdii_cfg );
OBDII_MAP_MIKROBUS( obdii_cfg, MIKROBUS_POSITION_OBDII );
if ( UART_ERROR == obdii_init( &obdii, &obdii_cfg ) )
{
log_error( &logger, " Communication init." );
for ( ; ; );
}
obdii_reset_device ( &obdii );
obdii_process ( &obdii );
obdii_clear_app_buf ( );
log_printf( &logger, "> Reset device\r\n" );
obdii_send_command ( &obdii, OBDII_CMD_RESET_DEVICE );
obdii_rsp_check ( &obdii, OBDII_RSP_PROMPT );
obdii_log_app_buf ( );
Delay_ms ( 1000 );
log_printf( &logger, " Disable echo\r\n" );
obdii_send_command ( &obdii, OBDII_CMD_DISABLE_ECHO );
obdii_rsp_check ( &obdii, OBDII_RSP_PROMPT );
obdii_log_app_buf ( );
log_printf( &logger, " Remove spaces\r\n" );
obdii_send_command ( &obdii, OBDII_CMD_SPACES_OFF );
obdii_rsp_check ( &obdii, OBDII_RSP_PROMPT );
obdii_log_app_buf ( );
}Reads and processes the engine RPM and vehicle speed and displays the results on the USB UART once per second.
void application_task ( void )
{
uint8_t * __generic_ptr start_ptr = NULL;
uint8_t data_buf[ 5 ] = { 0 };
uint16_t rpm = 0;
uint8_t speed = 0;
log_printf( &logger, " Get current RPM\r\n" );
obdii_send_command ( &obdii, OBDII_CMD_GET_CURRENT_RPM );
obdii_rsp_check ( &obdii, OBDII_RSP_PROMPT );
start_ptr = strstr( app_buf, OBDII_RSP_CURRENT_RPM );
if ( start_ptr )
{
memcpy ( data_buf, ( start_ptr + 4 ), 4 );
data_buf[ 4 ] = 0;
rpm = hex_to_uint16( data_buf ) / 4;
log_printf( &logger, "RPM: %u\r\n\n>", rpm );
}
else
{
obdii_log_app_buf ( );
}
log_printf( &logger, " Get current speed\r\n" );
obdii_send_command ( &obdii, OBDII_CMD_GET_CURRENT_SPEED );
obdii_rsp_check ( &obdii, OBDII_RSP_PROMPT );
start_ptr = strstr( app_buf, OBDII_RSP_CURRENT_SPEED );
if ( start_ptr )
{
memcpy ( data_buf, ( start_ptr + 4 ), 2 );
data_buf[ 2 ] = 0;
speed = hex_to_uint8( data_buf );
log_printf( &logger, "Speed: %u km/h\r\n\n>", ( uint16_t ) speed );
}
else
{
obdii_log_app_buf ( );
}
Delay_ms ( 1000 );
}This Click board can be interfaced and monitored in two ways:
- Application Output - Use the "Application Output" window in Debug mode for real-time data monitoring. Set it up properly by following this tutorial.
- UART Terminal - Monitor data via the UART Terminal using a USB to UART converter. For detailed instructions, check out this tutorial.
The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.