diff --git a/due_obd2/due_obd2.ino b/due_obd2/due_obd2.ino index ec50807..0f612a1 100644 --- a/due_obd2/due_obd2.ino +++ b/due_obd2/due_obd2.ino @@ -14,7 +14,7 @@ Program to create a car monitor display using: #include "obd2_display.h" #include "statemachine.h" #include "obd2_util.h" -#include "obd2_bt.h" +#include "obd2_elm327.h" #define DEBUG 1 // #define RTT_MR 0x400E1A30U @@ -86,7 +86,7 @@ char device_label_i = -1; /* counter in the current device label */ char bt_state = BT_INITIALISING; -obd2_bt_t bt; +obd2_elm327_t elm327; void on_init_enter(); void on_init_run(); @@ -191,7 +191,7 @@ void on_init_run() display.print("Bluetooth ", (display.getDisplayXSize() / 2) - (11 * display.getFontXsize()), initialization_y + 50); char current_bt_state[BT_STATE_LENGTH]; - obd2_bt_get_state(&bt, (char *)current_bt_state); + obd2_elm327_get_state(&elm327, (char *)current_bt_state); display.print(current_bt_state, (display.getDisplayXSize() / 2) + display.getFontXsize(), initialization_y + 50); display.setBackColor(VGA_BLACK); @@ -437,7 +437,7 @@ void setup() /* Serial1 is bluetooth module, pin 18 and 19. */ Serial1.begin(BT_BAUD); - obd2_bt_init(&bt); + obd2_elm327_init(&elm327); } void loop() diff --git a/due_obd2/obd2_bt.c b/due_obd2/obd2_elm327.cpp similarity index 58% rename from due_obd2/obd2_bt.c rename to due_obd2/obd2_elm327.cpp index 5cd0184..51ac0e1 100644 --- a/due_obd2/obd2_bt.c +++ b/due_obd2/obd2_elm327.cpp @@ -1,13 +1,16 @@ #include +#include "ELMduino.h" -#include "obd2_bt.h" +#include "obd2_elm327.h" -void obd2_bt_init(obd2_bt_t *bt) +ELM327 myELM327; + +void obd2_elm327_init(obd2_elm327_t *elm327) { - bt->state = BT_INITIALISING; + elm327->bt_state = BT_INITIALISING; } -void obd2_bt_get_state(obd2_bt_t *bt, char *state) +void obd2_elm327_get_state(obd2_elm327_t *elm327, char *state) { // char bt_states[3][13] = {"Initializing", "Connecting ", "Connected "}; // char bt_states; @@ -18,7 +21,7 @@ void obd2_bt_get_state(obd2_bt_t *bt, char *state) /* result string must be of size 13 */ return; } - switch (bt->state) + switch (elm327->bt_state) { case BT_INITIALISING: strcpy(state, "Initializing"); @@ -35,25 +38,25 @@ void obd2_bt_get_state(obd2_bt_t *bt, char *state) } } -void obd2_bt_process(obd2_bt_t *bt) +void obd2_elm327_process(obd2_elm327_t *elm327) { - if (bt->state != BT_CONNECTED) + if (elm327->bt_state != BT_CONNECTED) { __UINT32_TYPE__ bt_state = digitalRead(BT_STATE_PIN); - if (bt_state != bt->state) + if (bt_state != elm327->bt_state) { if (bt_state) { - bt->state = BT_CONNECTED; + elm327->bt_state = BT_CONNECTED; } else { - bt->state = BT_INITIALISING; + elm327->bt_state = BT_INITIALISING; } - bt->on_state_change(); + elm327->on_state_change(); } } - /* TODO read from Serial1 to read OBD2 data */ + /* TODO read from Serial2 to read OBD2 data */ } \ No newline at end of file diff --git a/due_obd2/obd2_bt.h b/due_obd2/obd2_elm327.h similarity index 60% rename from due_obd2/obd2_bt.h rename to due_obd2/obd2_elm327.h index 778b1ed..fc243bb 100644 --- a/due_obd2/obd2_bt.h +++ b/due_obd2/obd2_elm327.h @@ -10,20 +10,28 @@ 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=dc0d,30,000fa9 + AT+BIND=0010,CC,4F3603 AT+INIT (Need to connect) - AT+PAIR=dc0d,30,000fa9,30 (,30 means 30 second timeout) - AT+LINK=dc0d,30,000fa9 + 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_BT_H -#define OBD2_BT_H +#ifndef OBD2_ELM327_H +#define OBD2_ELM327_H #ifdef __cplusplus extern "C" { @@ -39,9 +47,12 @@ Be sure to disconnect the TX and RX pins from the BT module when programming the #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 + enum BluetoothState { BT_INITIALISING = 0, @@ -55,36 +66,36 @@ enum BluetoothState * @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_bt_tag +typedef struct obt2_elm327_tag { - enum BluetoothState state; + enum BluetoothState bt_state; void (*on_state_change)(); -} obd2_bt_t; +} obd2_elm327_t; /** * @brief initializes the bluetooth module driver * - * @param bt pointer to struct holding bluetooth driver data + * @param elm327 pointer to struct holding bluetooth driver data */ -void obd2_bt_init(obd2_bt_t *bt); +void obd2_elm327_init(obd2_elm327_t *elm327); /** * @brief processes the bluetooth module communication with the OBD2 reader * * @param bt pointer to struct holding bluetooth driver data */ -void obd2_bt_process(obd2_bt_t *bt); +void obd2_elm327_process(obd2_elm327_t *elm327); /** * @brief copies the current state of the bluetooth module to the state string * - * @param bt pointer to struct holding bluetooth driver data + * @param elm327 pointer to struct holding bluetooth driver data * @param state resulting string to hold the text representation of the state */ -void obd2_bt_get_state(obd2_bt_t *bt, char *state); +void obd2_elm327_get_state(obd2_elm327_t *elm327, char *state); #ifdef __cplusplus } #endif -#endif // OBD2_BT_H \ No newline at end of file +#endif // OBD2_ELM327_H \ No newline at end of file diff --git a/readme.md b/readme.md index 167ca61..f3c3739 100644 --- a/readme.md +++ b/readme.md @@ -24,6 +24,7 @@ - [Datasheet](https://components101.com/sites/default/files/component_datasheet/HC-05%20Datasheet.pdf) - [How to configure](https://lastminuteengineers.com/hc05-at-commands-tutorial/) - [User Instruction manual](https://www.rcscomponents.kiev.ua/datasheets/hc_hc-05-user-instructions-bluetooth.pdf) +- [Interfacing HC05 with Arduino](https://lastminuteengineers.com/hc05-bluetooth-arduino-tutorial/) ### 3.5 inch TFT display - [Product link](https://www.tinytronics.nl/shop/en/displays/tft/3.5-inch-tft-display-320*480-pixels-mega-compatible-ili9486) diff --git a/test_elm327/test_elm327.ino b/test_elm327/test_elm327.ino new file mode 100644 index 0000000..8f5ba3e --- /dev/null +++ b/test_elm327/test_elm327.ino @@ -0,0 +1,57 @@ +#include "ELMduino.h" + + +#define ELM_PORT Serial2 + + +ELM327 myELM327; + + +uint32_t rpm = 0; + + +void setup() +{ +#if LED_BUILTIN + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, LOW); +#endif + + Serial.begin(115200); + ELM_PORT.begin(115200); + + + // int bt_state = digitalRead(11); + // while (bt_state == 0) + // { + // bt_state = digitalRead(11); + // Serial.println(bt_state); + // Serial.println("bt not connected"); + // delay(500); + // } + Serial.println("bt connected"); + + Serial.println("Attempting to connect to ELM327..."); + + if (!myELM327.begin(ELM_PORT, true, 20000)) + { + Serial.println("Couldn't connect to OBD scanner"); + while (1); + } + + Serial.println("Connected to ELM327"); +} + + +void loop() +{ + float tempRPM = myELM327.rpm(); + + if (myELM327.nb_rx_state == ELM_SUCCESS) + { + rpm = (uint32_t)tempRPM; + Serial.print("RPM: "); Serial.println(rpm); + } + else if (myELM327.nb_rx_state != ELM_GETTING_MSG) + myELM327.printError(); +}