From 3c439db2ab80d754eadd2d2634b95120b697d87a Mon Sep 17 00:00:00 2001 From: SemvdH <45453592+SemvdH@users.noreply.github.com> Date: Thu, 18 Jan 2024 21:50:28 +0100 Subject: [PATCH 1/4] Update readme.md add interfacing hc05 with arduino --- readme.md | 1 + 1 file changed, 1 insertion(+) 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) From 4c6e8b30fb58b10af89c8b467b8bdc08c8c8d601 Mon Sep 17 00:00:00 2001 From: SemvdH <45453592+SemvdH@users.noreply.github.com> Date: Thu, 18 Jan 2024 22:01:20 +0100 Subject: [PATCH 2/4] Create test_elm327.ino --- test_elm327.ino | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 test_elm327.ino diff --git a/test_elm327.ino b/test_elm327.ino new file mode 100644 index 0000000..2ff3326 --- /dev/null +++ b/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(100); + } + 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(); +} From d0884f66aae4b3740b369ff737dd964d72cb5db2 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 18 Jan 2024 22:02:53 +0100 Subject: [PATCH 3/4] add test comments --- due_obd2/obd2_bt.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/due_obd2/obd2_bt.h b/due_obd2/obd2_bt.h index e5a335a..3c1e19c 100644 --- a/due_obd2/obd2_bt.h +++ b/due_obd2/obd2_bt.h @@ -6,6 +6,14 @@ BT module settings (AT mode): +BAUD=115200 +PIN=1189 +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) @@ -14,7 +22,7 @@ AT Commands to connect permanently to the OBD2 module: AT+BIND=dc0d,30,000fa9 AT+INIT (Need to connect) AT+PAIR=dc0d,30,000fa9,30 (,30 means 30 second timeout) - AT+LINK=dc0d,30,000fa9 + Be sure to disconnect the TX and RX pins from the BT module when programming the arduino. */ From 6ffb01b535bbc1f0203bdd62dafa4769515e0f4b Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 18 Jan 2024 23:29:16 +0100 Subject: [PATCH 4/4] tested ELM327 connection and refactored bt driver to include elm327 --- due_obd2/due_obd2.ino | 8 +++--- due_obd2/{obd2_bt.c => obd2_elm327.cpp} | 27 ++++++++++--------- due_obd2/{obd2_bt.h => obd2_elm327.h} | 25 +++++++++-------- .../test_elm327.ino | 16 +++++------ 4 files changed, 41 insertions(+), 35 deletions(-) rename due_obd2/{obd2_bt.c => obd2_elm327.cpp} (58%) rename due_obd2/{obd2_bt.h => obd2_elm327.h} (80%) rename test_elm327.ino => test_elm327/test_elm327.ino (78%) 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 80% rename from due_obd2/obd2_bt.h rename to due_obd2/obd2_elm327.h index 3c1e19c..4f4b957 100644 --- a/due_obd2/obd2_bt.h +++ b/due_obd2/obd2_elm327.h @@ -26,8 +26,8 @@ AT Commands to connect permanently to the OBD2 module: 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" { @@ -43,9 +43,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, @@ -59,36 +62,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/test_elm327.ino b/test_elm327/test_elm327.ino similarity index 78% rename from test_elm327.ino rename to test_elm327/test_elm327.ino index 2ff3326..8f5ba3e 100644 --- a/test_elm327.ino +++ b/test_elm327/test_elm327.ino @@ -21,14 +21,14 @@ void setup() 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(100); - } + // 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...");