17
17
Flags
18
18
Change this depending on hardware/version setup
19
19
*/
20
- // #define STN_CHIP_CONNECTED // comment this out if you're not connected to the STN chip
20
+ #define STN_CHIP_CONNECTED // comment this out if you're not connected to the STN chip
21
21
22
22
/* *
23
23
Libraries
@@ -102,7 +102,7 @@ int iso9141Delay = 100; // imperically defined
102
102
String str1 = " " ;
103
103
unsigned long programStarted = 0 ;
104
104
const long interval = 500 ;
105
-
105
+ boolean readerConnected = false ;
106
106
107
107
/* *
108
108
Setup
@@ -117,8 +117,7 @@ void setup() {
117
117
118
118
setupReader ();
119
119
setupBluetooth ();
120
- setupWiFi ();
121
- setupESPNow ();
120
+
122
121
123
122
while (!Serial2) {
124
123
Serial.println (" Connect to the reader" );
@@ -127,6 +126,13 @@ void setup() {
127
126
waitForBluetoothConnection ();
128
127
initialiseReaderConnection ();
129
128
129
+
130
+ while (!readerConnected) {
131
+ ;
132
+ }
133
+ setupWiFi ();
134
+ setupESPNow ();
135
+
130
136
delay (1000 );
131
137
// connectSensorModules();
132
138
Serial.println (" Ready" );
@@ -139,12 +145,11 @@ void setup() {
139
145
void loop () {
140
146
unsigned long currentMillis = millis ();
141
147
142
- if (currentMillis - programStarted >= interval) {
143
- receivedFromApp ();
144
- programStarted = currentMillis;
145
-
146
- // generateSampleIMUMsg
147
- }
148
+ if (currentMillis - programStarted >= interval) {
149
+ receivedFromApp ();
150
+ programStarted = currentMillis;
151
+ // generateSampleIMUMsg();
152
+ }
148
153
}
149
154
150
155
/* *
@@ -213,16 +218,13 @@ void setupReader(void) {
213
218
// }
214
219
215
220
void waitForBluetoothConnection (void ) {
216
- digitalWrite (ESP32_BLUETOOTH_LED_PIN, LOW);
217
-
218
221
while (!SerialBT.available ()) {
219
222
digitalWrite (ESP32_BLUETOOTH_LED_PIN, HIGH);
220
223
delay (500 );
221
224
digitalWrite (ESP32_BLUETOOTH_LED_PIN, LOW);
222
225
delay (500 );
223
226
Serial.println (" pair up the BT" );
224
227
}
225
-
226
228
digitalWrite (ESP32_BLUETOOTH_LED_PIN, HIGH);
227
229
}
228
230
@@ -233,8 +235,6 @@ void initialiseReaderConnection(void) {
233
235
#endif
234
236
235
237
while (!protocolFlag) {
236
- digitalWrite (ESP32_OBD_ACT_LED_PIN, HIGH);
237
-
238
238
findCorrectProtocol ();
239
239
flushBuffers ();
240
240
if (str1 == " 22" ) {
@@ -288,9 +288,8 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingD, int len) {
288
288
// Serial.println();
289
289
290
290
String comma = " ," ;
291
-
292
- Serial.print (" DEBUG: Printing IMUMsg" );
293
- Serial.println (msgData.accX );
291
+ // Serial.print("DEBUG: Printing IMUMsg");
292
+ // Serial.println(msgData.accX);
294
293
295
294
SerialBT.print (msgData.msgID );
296
295
SerialBT.print (" :" );
@@ -315,9 +314,10 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingD, int len) {
315
314
}
316
315
317
316
void findCorrectProtocol () {
318
- digitalWrite (ESP32_OBD_ACT_LED_PIN, HIGH);
317
+
319
318
while (SerialBT.available ()) {
320
319
char c = SerialBT.read ();
320
+ digitalWrite (ESP32_OBD_ACT_LED_PIN, HIGH);
321
321
if (c != ' >' ) {
322
322
str1 = str1 + c;
323
323
} else {
@@ -336,13 +336,22 @@ void findCorrectProtocol() {
336
336
protocolFlag = true ;
337
337
Serial.print (" found protocol:" );
338
338
Serial.println (str1);
339
- break ;
339
+ SerialBT.println (" Protocol Found!255255" );
340
+ readerConnected = true ;
341
+ flushBuffers ();
342
+ digitalWrite (ESP32_OBD_ACT_LED_PIN, LOW);
343
+ return ;
340
344
}
345
+
341
346
}
347
+ digitalWrite (ESP32_OBD_ACT_LED_PIN, LOW);
342
348
str1 = " " ;
343
349
}
344
350
}
345
-
351
+ flushBuffers ();
352
+ if (!SerialBT.available ()) {
353
+ SerialBT.println (" NOT FOUND!255255" );
354
+ }
346
355
digitalWrite (ESP32_OBD_ACT_LED_PIN, LOW);
347
356
}
348
357
@@ -386,6 +395,7 @@ void FromOBD() {
386
395
String subString = " " ;
387
396
388
397
while (Serial2.available () > 0 ) {
398
+ digitalWrite (ESP32_OBD_ACT_LED_PIN, HIGH);
389
399
char c = Serial2.read ();
390
400
str = str + c;
391
401
}
@@ -395,19 +405,41 @@ void FromOBD() {
395
405
Serial.println (charArray);
396
406
397
407
if (StrContains (charArray, c43)) {
398
- str = str.substring (6 , str.length () - 1 );
399
- getDTC (str);
408
+ Serial.println (str);
409
+ // str = removeEnterKey(charArray);
410
+ Serial.println (str);
411
+ if (str1 == " stp 22" ) {
412
+ str = str.substring (6 , str.length () - 1 );
413
+ Serial.println (str);
414
+ getDTC (str);
415
+ } else {
416
+ str = str.substring (9 , str.length () - 1 ); // "03 43 02 01 08 01 11"
417
+ Serial.println (str);
418
+ getDTC (str);
419
+ }
400
420
} else {
401
421
str = str.substring (6 , str.length () - 1 ); // "01 0D 41 0D D9 >" getting rid of "01 0D " and ">"
402
422
str = " 7E8 03 " + str + " 255255" ;
403
423
SerialBT.println (str);
404
424
}
405
425
delay (iso9141Delay);
426
+
427
+ digitalWrite (ESP32_OBD_ACT_LED_PIN, LOW);
406
428
}
407
429
408
- digitalWrite (ESP32_OBD_ACT_LED_PIN, LOW);
409
430
}
410
431
432
+
433
+ String removeEnterKey (char * c) {
434
+ String str = " " ;
435
+ char * iterator;
436
+ for (iterator = c; *iterator != ' \0 ' ; iterator++ ) {
437
+ if (*iterator != ' \n ' ) {
438
+ str = str + *iterator;
439
+ }
440
+ }
441
+ return str;
442
+ }
411
443
/* *
412
444
function for decoding DTC message from obd
413
445
*/
@@ -470,13 +502,61 @@ String decodeDTC(String string) {
470
502
return str;
471
503
}
472
504
473
- void getDTC (String str) {
474
- String string;
475
- str.replace (" " , " " );
476
- Serial.println (str);
477
- for (int i = 0 ; i < str.length ();) {
478
- SerialBT.println (decodeDTC (str.substring (i, i + 4 )));
479
- i = i + 4 ;
505
+
506
+ void getDTC (String string) {
507
+ Serial.print (" length is:" );
508
+ Serial.println (string.length ());
509
+ Serial.print (" str is:" );
510
+ Serial.println (string);
511
+ string.replace (" " , " " );
512
+ Serial.print (" length is:" );
513
+ Serial.println (string.length ());
514
+ Serial.println (" str is:" );
515
+ for (size_t i = 0 ; i < string.length (); i++) {
516
+ Serial.println (string[i]);
517
+ }
518
+ Serial.println (string);
519
+ Serial.println (str1);
520
+ if (str1 == " stp 22" ) {
521
+ for (size_t i = 0 ; i < string.length ();) {
522
+ Serial.println (" iso9141" );
523
+ Serial.println (string.substring (i, i + 4 ));
524
+ SerialBT.println (decodeDTC (string.substring (i, i + 4 )));
525
+ i = i + 4 ;
526
+ }
527
+ } else if (string.length () < 12 ) {
528
+ for (size_t i = 0 ; i < string.length ();) {
529
+ Serial.println (" less than 9" );
530
+ Serial.println (string.substring (i, i + 4 ));
531
+ SerialBT.println (decodeDTC (string.substring (i, i + 4 )));
532
+ i = i + 4 ;
533
+ }
534
+ } else {
535
+ Serial.println (" loop" );
536
+ for (size_t i = 0 ; i < string.length ();) {
537
+ if (i < 5 ){
538
+ i = string.indexOf (" :" , 0 );
539
+ i = i + 5 ; // 0:4303 0121 0122 getting rid of 43 and 03
540
+ SerialBT.println (decodeDTC (string.substring (i, i + 4 )));
541
+ i = i + 4 ;
542
+ SerialBT.println (decodeDTC (string.substring (i, i + 4 )));
543
+ Serial.println (string.substring (i, i + 4 ));
544
+ i = i + 4 ;
545
+ }else {
546
+ i = string.indexOf (" :" , i);
547
+ i++;
548
+ SerialBT.println (decodeDTC (string.substring (i, i + 4 )));
549
+ Serial.println (string.substring (i, i + 4 ));
550
+ i = i + 4 ;
551
+ SerialBT.println (decodeDTC (string.substring (i, i + 4 )));
552
+ Serial.println (string.substring (i, i + 4 ));
553
+ i = i + 4 ;
554
+ SerialBT.println (decodeDTC (string.substring (i, i + 4 )));
555
+ Serial.println (string.substring (i, i + 4 ));
556
+ i = i + 4 ;
557
+ break ;
558
+ }
559
+ }
480
560
}
481
561
}
482
562
@@ -549,4 +629,4 @@ void generateSampleIMUMsg(void) {
549
629
// void disconnectWiFi(void) {
550
630
// Serial.println("Turning off WiFi");
551
631
// WiFi.softAPdisconnect(false);
552
- // }
632
+ // }
0 commit comments