Files
OBD2-car-display/due_obd2/obd2_elm327.h
Sem van der Hoeven f9cef163b9 progress
2024-07-14 21:44:26 +02:00

139 lines
3.7 KiB
C

/*
BT module settings (AT mode):
+NAME=OBD2_BT
+VER=8.0.2,FSC-BT836B
+MAC=DC0D30000FA9
+BAUD=115200
+PIN=1189
HC-05 bluetooth module:
address: 5856:00:017A39
IAC: 9E8B33
https://forum.arduino.cc/t/trying-to-connect-hc-05-to-bluetooth-obd2-interface-elm-327/643301/13
https://stackoverflow.com/questions/59125470/connecting-to-elm327-bt-with-hc05-stm32-not-as-simply-as-look-like
https://electronics.stackexchange.com/questions/101572/hc-05-bluetooth-module-not-responding-to-certain-commands
OBD2 INQ: 0010:CC:4F3603,0,FF9C,OBDII
AT+PAIR=0010,CC,4F3603,20
AT Commands to connect permanently to the OBD2 module:
AT+RESET
AT+ORGL (Set to original)
AT+ROLE=1 (Set to Master)
AT+CMODE=0 (Set connect to a specific address)
AT+BIND=0010,CC,4F3603
AT+INIT (Need to connect)
AT+PAIR=0010,CC,4F3603,20 (,20 means 30 second timeout)
Be sure to disconnect the TX and RX pins from the BT module when programming the arduino.
*/
#ifndef OBD2_ELM327_H
#define OBD2_ELM327_H
#include "ELMduino.h"
#ifdef __cplusplus
extern "C" {
#endif
// #include <Arduino.h>
// #include "obd2_util.h"
/* pins */
#define PIN_BT_RX 19
#define PIN_BT_TX 18
#define BT_STATE_PIN 3 /* state connection pin from bluetooth module -> level high on connected */
#define BT_BAUD 115200
#define ELM327_BAUD 115200
#define BT_STATE_LENGTH 13 /* length of the string identifier used for the states */
#define ELM327_SERIAL Serial2
#define UPDATE_RPM_POS 0x00
#define UPDATE_THROTTLE_POS 0x01
#define UPDATE_COOLANT_TEMP_POS 0x02
#define UPDATE_INTAKE_AIR_TEMP_POS 0x03
#define UPDATE_AMBIENT_AIR_TEMP_POS 0x04
#define UPDATE_OIL_TEMP_POS 0x05
#define UPDATE_ENGINE_LOAD_POS 0x06
#define UPDATE_BATTERY_VOLTAGE_POS 0x07
#define UPDATE_FUEL_PRESSURE_POS 0x08
#define UPDATE_SPEED_POS 0x09
#define UPDATE_MANIFOLD_PRESSURE_POS 0x0A
#define UPDATE_FUEL_LEVEL_POS 0x0B
enum BluetoothState
{
BT_INITIALISING = 0,
BT_CONNECTED = 1
};
/**
* @brief struct to hold bluetooth module data
*
* @param state the current state of the bluetooth module
* @param on_state_change function pointer to the function to be called when the state changes
*/
typedef struct obt2_elm327_tag
{
ELM327 *elm327;
enum BluetoothState bt_state;
void (*on_state_change)();
uint16_t value_updates;
uint16_t rpm;
uint8_t throttle_percent;
uint8_t engine_coolant_temp;
uint8_t intake_air_temp;
uint8_t ambient_air_temp;
uint16_t engine_oil_temp;
uint8_t engine_load;
float battery_voltage;
uint16_t fuel_pressure;
uint8_t speed;
uint8_t manifold_pressure;
uint16_t fuel_level;
} obd2_elm327_t;
/**
* @brief initializes the bluetooth module driver
*
* @param elm327 pointer to struct holding bluetooth driver data
* @returns 1 if successfully initialized, 0 if not.
*/
char obd2_elm327_init(obd2_elm327_t *elm327);
/**
* @brief processes the bluetooth module communication with the OBD2 reader for variables that change quickly
*
* @param bt pointer to struct holding bluetooth driver data
*/
void obd2_elm327_process_fast(obd2_elm327_t *elm327);
/**
* @brief processes the bluetooth module communication with the OBD2 reader for variables that change slowly
*
* @param bt pointer to struct holding bluetooth driver data
*/
void obd2_elm327_process_slow(obd2_elm327_t *elm327);
void obd2_elm327_check_connection(obd2_elm327_t *elm327);
/**
* @brief copies the current state of the bluetooth module to the state string
*
* @param elm327 pointer to struct holding bluetooth driver data
* @param state resulting string to hold the text representation of the state
*/
void obd2_elm327_get_state(obd2_elm327_t *elm327, char *state);
#ifdef __cplusplus
}
#endif
#endif // OBD2_ELM327_H