Skip to content

Commit 6b8d039

Browse files
author
eyazici
committed
Merge remote-tracking branch 'origin/master'
2 parents d925472 + 9c044c4 commit 6b8d039

File tree

2 files changed

+124
-44
lines changed

2 files changed

+124
-44
lines changed

esp32_sensor_module/esp32_sensor_module.ino

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
b/**
22
ESP32 Sensor Module
33
44
ESP32 Datasheet: https://www.espressif.com/sites/default/files/documentation/esp32-wroom-32_datasheet_en.pdf
@@ -19,7 +19,7 @@
1919
/**
2020
Flags
2121
*/
22-
#define DEMO // Comment this out for production
22+
//#define DEMO // Comment this out for production
2323

2424
/**
2525
Libraries
@@ -118,14 +118,14 @@ esp_now_peer_info_t readerInfo;
118118
void setup() {
119119
Serial.begin(SERIAL_BAUD_RATE);
120120
Serial.println("Wait for user to be ready");
121-
while (true) {
122-
123-
if (Serial.available() > 0) {
124-
if (Serial.read() == 'G') {
125-
break;
126-
}
127-
}
128-
}
121+
// while (true) {
122+
//
123+
// if (Serial.available() > 0) {
124+
// if (Serial.read() == 'G') {
125+
// break;
126+
// }
127+
// }
128+
// }
129129

130130
imuSetup();
131131
wifiSetup();
@@ -422,5 +422,5 @@ void loop() {
422422
waitForConnectionToReader();
423423
}
424424

425-
delay(100);
426-
}
425+
delay(200);
426+
}

reader/reader.ino

Lines changed: 112 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Flags
1818
Change this depending on hardware/version setup
1919
*/
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
2121

2222
/**
2323
Libraries
@@ -102,7 +102,7 @@ int iso9141Delay = 100; // imperically defined
102102
String str1 = "";
103103
unsigned long programStarted = 0;
104104
const long interval = 500;
105-
105+
boolean readerConnected = false;
106106

107107
/**
108108
Setup
@@ -117,8 +117,7 @@ void setup() {
117117

118118
setupReader();
119119
setupBluetooth();
120-
setupWiFi();
121-
setupESPNow();
120+
122121

123122
while (!Serial2) {
124123
Serial.println("Connect to the reader");
@@ -127,6 +126,13 @@ void setup() {
127126
waitForBluetoothConnection();
128127
initialiseReaderConnection();
129128

129+
130+
while (!readerConnected) {
131+
;
132+
}
133+
setupWiFi();
134+
setupESPNow();
135+
130136
delay(1000);
131137
// connectSensorModules();
132138
Serial.println("Ready");
@@ -139,12 +145,11 @@ void setup() {
139145
void loop() {
140146
unsigned long currentMillis = millis();
141147

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+
}
148153
}
149154

150155
/**
@@ -213,16 +218,13 @@ void setupReader(void) {
213218
//}
214219

215220
void waitForBluetoothConnection(void) {
216-
digitalWrite(ESP32_BLUETOOTH_LED_PIN, LOW);
217-
218221
while (!SerialBT.available()) {
219222
digitalWrite(ESP32_BLUETOOTH_LED_PIN, HIGH);
220223
delay(500);
221224
digitalWrite(ESP32_BLUETOOTH_LED_PIN, LOW);
222225
delay(500);
223226
Serial.println("pair up the BT");
224227
}
225-
226228
digitalWrite(ESP32_BLUETOOTH_LED_PIN, HIGH);
227229
}
228230

@@ -233,8 +235,6 @@ void initialiseReaderConnection(void) {
233235
#endif
234236

235237
while (!protocolFlag) {
236-
digitalWrite(ESP32_OBD_ACT_LED_PIN, HIGH);
237-
238238
findCorrectProtocol();
239239
flushBuffers();
240240
if (str1 == "22") {
@@ -288,9 +288,8 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingD, int len) {
288288
// Serial.println();
289289

290290
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);
294293

295294
SerialBT.print(msgData.msgID);
296295
SerialBT.print(":");
@@ -315,9 +314,10 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingD, int len) {
315314
}
316315

317316
void findCorrectProtocol() {
318-
digitalWrite(ESP32_OBD_ACT_LED_PIN, HIGH);
317+
319318
while (SerialBT.available()) {
320319
char c = SerialBT.read();
320+
digitalWrite(ESP32_OBD_ACT_LED_PIN, HIGH);
321321
if (c != '>') {
322322
str1 = str1 + c;
323323
} else {
@@ -336,13 +336,22 @@ void findCorrectProtocol() {
336336
protocolFlag = true;
337337
Serial.print("found protocol:");
338338
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;
340344
}
345+
341346
}
347+
digitalWrite(ESP32_OBD_ACT_LED_PIN, LOW);
342348
str1 = "";
343349
}
344350
}
345-
351+
flushBuffers();
352+
if (!SerialBT.available()) {
353+
SerialBT.println("NOT FOUND!255255");
354+
}
346355
digitalWrite(ESP32_OBD_ACT_LED_PIN, LOW);
347356
}
348357

@@ -386,6 +395,7 @@ void FromOBD() {
386395
String subString = "";
387396

388397
while (Serial2.available() > 0) {
398+
digitalWrite(ESP32_OBD_ACT_LED_PIN, HIGH);
389399
char c = Serial2.read();
390400
str = str + c;
391401
}
@@ -395,19 +405,41 @@ void FromOBD() {
395405
Serial.println(charArray);
396406

397407
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+
}
400420
} else {
401421
str = str.substring(6, str.length() - 1); //"01 0D 41 0D D9 >" getting rid of "01 0D " and ">"
402422
str = "7E8 03 " + str + "255255";
403423
SerialBT.println(str);
404424
}
405425
delay(iso9141Delay);
426+
427+
digitalWrite(ESP32_OBD_ACT_LED_PIN, LOW);
406428
}
407429

408-
digitalWrite(ESP32_OBD_ACT_LED_PIN, LOW);
409430
}
410431

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+
}
411443
/**
412444
function for decoding DTC message from obd
413445
*/
@@ -470,13 +502,61 @@ String decodeDTC(String string) {
470502
return str;
471503
}
472504

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+
}
480560
}
481561
}
482562

@@ -549,4 +629,4 @@ void generateSampleIMUMsg(void) {
549629
//void disconnectWiFi(void) {
550630
// Serial.println("Turning off WiFi");
551631
// WiFi.softAPdisconnect(false);
552-
//}
632+
//}

0 commit comments

Comments
 (0)