Skip to content

ESP32 successful connection using Mac address #265

Description

@MFAISALREHMAN

Hello
ESP32 board does not connect with OBDII string name. Previously i used to connect with HC-05 and Nano but HC-05 is fragile.
Now I tried (some 100 tries) with esp32 and following code worked.

BluetoothSerialSerialBT;
uint8_taddress[6] = { 0x00, 0x10, 0xCC, 0x4F, 0x36, 0x03 };
boolconnected;
#defineBT_DISCOVER_TIME 500000
esp_spp_sec_tsec_mask=ESP_SPP_SEC_NONE; // or ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE to request pincode confirmationesp_spp_role_trole=ESP_SPP_ROLE_MASTER; // or ESP_SPP_ROLE_MASTERvoidsetup() {
Serial.begin(38400);
//SerialBT.setPin(pin);SerialBT.begin("ESP32", true, 10000);
//SerialBT.print()Serial.println("The device started in master mode, make sure remote BT device is on!");
connected=SerialBT.connect(address, sec_mask, role);
if (connected) {
Serial.println("Connected Succesfully!");
} else {
while (!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
SerialBT.print(( String) "AT Z");
SerialBT.print((constchar) '\r');
delay(150);
printBT();
SerialBT.write((constuint8_t*) "AT E0",5);
delay(150);
printBT();
SerialBT.write((constuint8_t*) "AT L0",5);
delay(150);
printBT();
SerialBT.write((constuint8_t*) "AT S0",5);
delay(150);
printBT();
SerialBT.write((constuint8_t*) "AT SP 0",7);
delay(150);
printBT();
Serial.println("0100");
SerialBT.print(( String) "0100");
Serial.println("0100");
Serial.println("delay");
printBT();
delay(6000);
//SerialBT.print((const char) '\r');printBT();
delay(1000);
}
voidloop() {
SerialBT.write((constuint8_t*) "01051",5);
Serial.println("sending 01051");
delay(200);
printBT();
SerialBT.write((constuint8_t*) "0105",4);
Serial.println("sending 0105");
delay(200);
printBT();
SerialBT.write((constuint8_t*) "AT DP",5);
delay(200);
Serial.println("Protocol");
printBT();
}
voidprintBT()
{
while(SerialBT.available())
{
charc=SerialBT.read();
if(c=='>')
Serial.println();
Serial.write(c);
}
}

Consequently the ELMDuino library Initialize funtion was changed as following

{
charcommand[10] = {'\0'};
connected= false;
// sendCommand_Blocking(SET_ALL_TO_DEFAULTS);//delay(100);sendCommand_Blocking(RESET_ALL);
delay(100);
sendCommand_Blocking(ECHO_OFF);
delay(100);
sendCommand_Blocking(LINEFEEDS_OFF);
delay(100);
sendCommand_Blocking(PRINTING_SPACES_OFF);
delay(100);
// // Set data timeout// sprintf(command, SET_TIMEOUT_TO_H_X_4MS, dataTimeout / 4);// sendCommand_Blocking(command);// delay(100);// Automatic searching for protocol requires setting the protocol to AUTO and then// sending an OBD command to initiate the protocol search. The OBD command "0100"// requests a list of supported PIDs 0x00 - 0x20 and is guaranteed to workif ((String)protocol=="0")
{
// Tell the ELM327 to do an auto protocol search. If a valid protocol is found, it will be saved to memory.// Some ELM clones may not have memory enabled and thus will perform the search every time.sprintf(command, SET_PROTOCOL_TO_H_SAVE, protocol);
if (sendCommand_Blocking(command) ==ELM_SUCCESS)
{
if (strstr(payload, RESPONSE_OK) !=NULL)
{
// Protocol search can take a comparatively long time. Temporarily set// the timeout value to 30 seconds, then restore the previous value.uint16_tprevTimeout=timeout_ms;
timeout_ms=6000;
int8_tstate=sendCommand_Blocking("0100");
if (state==ELM_SUCCESS)
{
timeout_ms=prevTimeout;
connected= true;
delay(1000);
returnconnected;
}
elseif (state==ELM_BUFFER_OVERFLOW)
{
while (elm_port->available())
elm_port->read();
}
timeout_ms=prevTimeout;
}
}
}
else
{
// Set protocolsprintf(command, TRY_PROT_H_AUTO_SEARCH, protocol);
if (sendCommand_Blocking(command) ==ELM_SUCCESS)
{
if (strstr(payload, RESPONSE_OK) !=NULL)
{
connected= true;
returnconnected;
}
}
}
if (debugMode)
{
Serial.print(F("Setting protocol via "));
Serial.print(TRY_PROT_H_AUTO_SEARCH);
Serial.print(F(" did not work - trying via "));
Serial.println(SET_PROTOCOL_TO_H_SAVE);
}
// Set protocol and savesprintf(command, SET_PROTOCOL_TO_H_SAVE, protocol);
if (sendCommand_Blocking(command) ==ELM_SUCCESS)
{
if (strstr(payload, RESPONSE_OK) !=NULL)
{
connected= true;
returnconnected;
}
}
if (debugMode)
{
Serial.print(F("Setting protocol via "));
Serial.print(SET_PROTOCOL_TO_H_SAVE);
Serial.println(F(" did not work"));
}
returnconnected;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions