Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 Wifi: getting weird responses #76

Closed
hrtkpf opened this issue Apr 7, 2021 · 5 comments
Closed

ESP32 Wifi: getting weird responses #76

hrtkpf opened this issue Apr 7, 2021 · 5 comments
Assignees
Labels
question Further information is requested

Comments

@hrtkpf
Copy link

hrtkpf commented Apr 7, 2021

Hello,

I was testing ELMduino with a OBD2 Wifi adapter and an ESP32. I read #52 and I know there may be problems with this configuration. I'm trying to read the coolant temperature of a BMW from 2014. Sometimes, I get correct numbers, but very often I just get weird responses.
After some debugging, I found out that these "weird responses" might actually contain the correct value but some weird symbols as well, which could be misinterpreted by my code.

I just tested with two different sketches, one by me and the other one being your "ESP32_test.ino", adapted to work with Wifi.
I will only post my sketch and the output for now because I don't want the initial issue to be too long, but I can also post the debug sketch and its output later on if desired.
I also added some comments starting with "NOTE:". Unrelated parts of the sketch, like printing to a display, are omitted.

My sketch:

#include <WiFi.h>
#include <ELMduino.h>

// ELM dongle IP address
IPAddress ELMserver(192, 168, 0, 10);
WiFiClient client;
ELM327 elm;

int lastUpdatedCoolant, updateAgeCoolant = 0;
int coolantTemp = 0;


void setup(){
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.begin("V-LINK");

  Serial.println("Connecting to WiFi...");

  while (WiFi.status() != WL_CONNECTED){
    Serial.print(".");
    delay(500);
  }

  // could improve wireless connectivity with ESP (maybe)
  client.setNoDelay(true); // NOTE: due to the error in the output, I don't know if this has any influence at all

  if(client.connect(ELMserver, 35000)){
    Serial.println("ELM connected");
  }else{
    Serial.println("ELM connection failed");
    delay(5000);
    ESP.restart();
  }

  elm.begin(client, true);
}


void loop(){
  // Coolant
  float readCoolantTemp = elm.engineCoolantTemp();

  Serial.println(readCoolantTemp);

  if(elm.status == ELM_SUCCESS && readCoolantTemp < 200 && readCoolantTemp >= 0){
    coolantTemp = readCoolantTemp;
    lastUpdatedCoolant = millis();
  }else{
    // NOTE: this fails because the value is too high, but elm.status is actually ELM_SUCCESS
    Serial.println("Could not read Coolant");
    elm.printError();
  }

  if(elm.status == ELM_TIMEOUT){
    ESP.restart();
  }

  updateAgeCoolant = (millis() - lastUpdatedCoolant) / 1000;

  String textCoolant = (String)coolantTemp + " C, " + updateAgeCoolant + "s";
  Serial.println(textCoolant);

  delay(5000);
}

The corresponding output:

Connecting to WiFi...
..[E][WiFiClient.cpp:309] setOption(): fail on fd -1, errno: 9, "Bad file number"
ELM connected
Clearing input serial buffer
Sending the following command/query: AT D
All chars received:
Timeout detected with overflow of 0ms
Clearing input serial buffer
Sending the following command/query: AT Z
Received char: A
Received char: T
Received char:
Received char: Z
Received char: \r
All chars received: ATZ
Timeout detected with overflow of 0ms
Clearing input serial buffer
Sending the following command/query: AT E0
Received char: A
Received char: T
Received char:
Received char: E
Received char: 0
Received char: \r
Received char: O
Received char: K
Received char: \r
Received char: \r
Received char: >
Delimiter found
All chars received: ATE0OK
Clearing input serial buffer
Sending the following command/query: AT S0
Received char: O
Received char: K
Received char: \r
Received char: \r
Received char: >
Delimiter found
All chars received: OK
Clearing input serial buffer
Sending the following command/query: AT AL
Received char: O
Received char: K
Received char: \r
Received char: \r
Received char: >
Delimiter found
All chars received: OK
Clearing input serial buffer
Sending the following command/query: AT TP A0
Received char: O
Received char: K
Received char: \r
Received char: \r
Received char: >
Delimiter found
All chars received: OK
Service: 1
PID: 5
Normal length query detected
Query string: 0105
Clearing input serial buffer
Sending the following command/query: 0105
Received char: S
Received char: E
Received char: A
Received char: R
Received char: C
Received char: H
Received char: I
Received char: N
Received char: G
Received char: .
Received char: .
Received char: .
Received char: \r
Received char: �
Received char: 4
Received char: 1
Received char: 0
Received char: 5
Received char: 2
Received char: 8
Received char: \r
Received char: �
Received char: \r
Received char: >
Delimiter found
All chars received: SEARCHING...�410528�
Expected response header: 4105
Single response detected
64-bit response: responseByte_0: 200
responseByte_1: 2
responseByte_2: 0
responseByte_3: 0
responseByte_4: 0
responseByte_5: 0
responseByte_6: 0
responseByte_7: 0
672.00 // NOTE: even though the ELM response seems to be correct, the interpreted value is way too high
Could not read Coolant
Received: SEARCHING...�410528� // NOTE: First response includes "SEARCHING...", the numbers seem to be correct but also contain the "weird symbols"
ELM_SUCCESS
0 C, 10s // NOTE: coolant temp is 0 here because coolantTemp is initialized with 0
Service: 1
PID: 5
Normal length query detected
Query string: 0105
Clearing input serial buffer
Sending the following command/query: 0105
Received char: 4
Received char: 1
Received char: 0
Received char: 5
Received char: 2
Received char: 8
Received char: \r
Received char: �
Received char: \r
Received char: >
Delimiter found
All chars received: 410528�
Expected response header: 4105
Single response detected
64-bit response: responseByte_0: 200
responseByte_1: 2
responseByte_2: 0
responseByte_3: 0
responseByte_4: 0
responseByte_5: 0
responseByte_6: 0
responseByte_7: 0
672.00
Could not read Coolant
Received: 410528�
ELM_SUCCESS
0 C, 16s
Service: 1
PID: 5
Normal length query detected
Query string: 0105
Clearing input serial buffer
Sending the following command/query: 0105
Received char: 4
Received char: 1
Received char: 0
Received char: 5
Received char: 2
Received char: 8
Received char: \r
Received char: �
Received char: \r
Received char: >
Delimiter found
All chars received: 410528�
Expected response header: 4105
Single response detected
64-bit response: responseByte_0: 200
responseByte_1: 2
responseByte_2: 0
responseByte_3: 0
responseByte_4: 0
responseByte_5: 0
responseByte_6: 0
responseByte_7: 0
672.00
Could not read Coolant
Received: 410528�
ELM_SUCCESS
0 C, 22s
Service: 1
PID: 5
Normal length query detected
Query string: 0105
Clearing input serial buffer
Sending the following command/query: 0105
Received char: 4
Received char: 1
Received char: 0
Received char: 5
Received char: 2
Received char: 8
Received char: \r
Received char: �
Received char: \r
Received char: >
Delimiter found
All chars received: 410528�
Expected response header: 4105
Single response detected
64-bit response: responseByte_0: 200
responseByte_1: 2
responseByte_2: 0
responseByte_3: 0
responseByte_4: 0
responseByte_5: 0
responseByte_6: 0
responseByte_7: 0
672.00
Could not read Coolant
Received: 410528�
ELM_SUCCESS
0 C, 28s
Service: 1
PID: 5
Normal length query detected
Query string: 0105
Clearing input serial buffer
Sending the following command/query: 0105
Received char: 4
Received char: 1
Received char: 0
Received char: 5
Received char: 2
Received char: 8
Received char: \r
Received char: �
Received char: \r
Received char: >
Delimiter found
All chars received: 410528�
Expected response header: 4105
Single response detected
64-bit response: responseByte_0: 200
responseByte_1: 2
responseByte_2: 0
responseByte_3: 0
responseByte_4: 0
responseByte_5: 0
responseByte_6: 0
responseByte_7: 0
672.00
Could not read Coolant
Received: 410528�
ELM_SUCCESS
0 C, 33s
Service: 1
PID: 5
Normal length query detected
Query string: 0105
Clearing input serial buffer
Sending the following command/query: 0105
Received char: 4
Received char: 1
Received char: 0
Received char: 5
Received char: 2
Received char: 8
Received char: \r
Received char: �
Received char: \r
Received char: >
Delimiter found
All chars received: 410528�
Expected response header: 4105
Single response detected
64-bit response: responseByte_0: 200
responseByte_1: 2
responseByte_2: 0
responseByte_3: 0
responseByte_4: 0
responseByte_5: 0
responseByte_6: 0
responseByte_7: 0
672.00
Could not read Coolant
Received: 410528�
ELM_SUCCESS
0 C, 39s
Service: 1
PID: 5
Normal length query detected
Query string: 0105
Clearing input serial buffer
Sending the following command/query: 0105
Received char: 4
Received char: 1
Received char: 0
Received char: 5
Received char: 2
Received char: 8
Received char: \r
Received char: �
Received char: \r
Received char: >
Delimiter found
All chars received: 410528�
Expected response header: 4105
Single response detected
64-bit response: responseByte_0: 200
responseByte_1: 2
responseByte_2: 0
responseByte_3: 0
responseByte_4: 0
responseByte_5: 0
responseByte_6: 0
responseByte_7: 0
672.00
Could not read Coolant
Received: 410528�
ELM_SUCCESS
0 C, 44s

Sometimes, I get the correct number (without the weird symbols) which is then correctly interpreted by the code, but while logging this output, that didn't work.
Thanks to this page, I was able to manually decode the response values (after removing weird symbols) and they correctly decode to 0 °C in this case.

Do you think those weird symbols originate from hardware issues like described in #52? Like I said, the raw numbers seem to be correct.
Also, thank you for maintaining this very useful library! :)

@hrtkpf hrtkpf added the question Further information is requested label Apr 7, 2021
@PowerBroker2
Copy link
Owner

I don't know why you're getting the char, but I'll add a filter in the parsing function to throw out any non-alphanumeric ascii chars. This should fix your issue

Also, thank you for maintaining this very useful library! :)

Happy to help and really appreciate people find it useful!

@PowerBroker2
Copy link
Owner

PowerBroker2 commented Apr 7, 2021

Try testing the main branch. If it works I'll tag out a new version. If not, please post the latest debug printout

@hrtkpf
Copy link
Author

hrtkpf commented Apr 7, 2021

Thank you for your fast response! I will test the code tomorrow again and report back ASAP.

But doesn’t it need to be !isalnum(recChar) because we want to continue on non-alphanumerical chars? Or am I overseeing something?

PowerBroker2 added a commit that referenced this issue Apr 7, 2021
@hrtkpf
Copy link
Author

hrtkpf commented Apr 8, 2021

So I just tested the updated code and it works flawlessly now! Apparently no hardware issues, just some timeouts here and there, but that's probably normal.

Thank you!

@PowerBroker2
Copy link
Owner

Fixed in release 2.5.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants