Skip to content

Commit

Permalink
feat: allow to set PIN for ELM327 dongle
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerre committed Feb 24, 2025
1 parent 2178d45 commit 120d6e2
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ void WiFiAPStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {

if (wifiAPStaConnected == 0) {
DEBUG_PORT.println("WiFi AP all clients disconnected. Start all other task.");
OBD.begin(Settings.getOBD2Name(OBD_ADP_NAME), Settings.getOBD2MAC(), Settings.getOBD2Protocol(),
Settings.getOBD2CheckPIDSupport());
OBD.begin(Settings.getOBD2Name(OBD_ADP_NAME), Settings.getOBD2MAC(), Settings.getOBD2Pin(),
Settings.getOBD2Protocol(), Settings.getOBD2CheckPIDSupport());
OBD.connect(true);
wifiAPInUse = false;
}
Expand Down Expand Up @@ -837,8 +837,8 @@ void setup() {

OBD.onConnected(onOBDConnected);
OBD.onConnectError(onOBDConnectError);
OBD.begin(Settings.getOBD2Name(OBD_ADP_NAME), Settings.getOBD2MAC(), Settings.getOBD2Protocol(),
Settings.getOBD2CheckPIDSupport());
OBD.begin(Settings.getOBD2Name(OBD_ADP_NAME), Settings.getOBD2MAC(), Settings.getOBD2Pin(),
Settings.getOBD2Protocol(), Settings.getOBD2CheckPIDSupport());
OBD.onDevicesDiscovered(onBTDevicesDiscovered);
OBD.connect();

Expand Down
11 changes: 10 additions & 1 deletion src/obd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,11 @@ BTScanResults *OBDClass::discoverBtDevices() {
return nullptr;
}

void OBDClass::begin(const String &devName, const String &devMac, const char protocol,
void OBDClass::begin(const String &devName, const String &devMac, const String &devPin, const char protocol,
const bool checkPidSupport) {
this->devName = devName;
this->devMac = devMac;
this->devPin = devPin;
this->protocol = protocol;
this->checkPidSupport = checkPidSupport;
stopConnect = false;
Expand Down Expand Up @@ -451,6 +452,10 @@ void OBDClass::connect(bool reconnect) {
}

if (!stopConnect && addr) {
if (devPin != nullptr && devPin.length() != 0) {
serialBt.setPin(devPin.c_str());
}

Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel);
if (serialBt.connect(addr, channel, ESP_SPP_SEC_NONE, ESP_SPP_ROLE_SLAVE)) {
connectedBTAddress = addr.toString().c_str();
Expand All @@ -474,6 +479,10 @@ void OBDClass::connect(bool reconnect) {
}

if (!stopConnect && addr) {
if (devPin != nullptr && devPin.length() != 0) {
serialBt.setPin(devPin.c_str());
}

Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel);
if (serialBt.connect(addr, channel, ESP_SPP_SEC_NONE, ESP_SPP_ROLE_SLAVE)) {
connectedBTAddress = addr.toString().c_str();
Expand Down
4 changes: 3 additions & 1 deletion src/obd.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class OBDClass : public OBDStates {

String devName;
String devMac;
String devPin;
char protocol;
bool checkPidSupport = false;

Expand Down Expand Up @@ -114,7 +115,8 @@ class OBDClass : public OBDStates {

bool writeStates(FS &fs);

void begin(const String &devName, const String &devMac, char protocol = AUTOMATIC, bool checkPidSupport = false);
void begin(const String &devName, const String &devMac, const String &devPin, char protocol = AUTOMATIC,
bool checkPidSupport = false);

void end();

Expand Down
10 changes: 10 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void SettingsClass::readJson(JsonDocument &doc) {

strlcpy(obd2.name, doc["obd2"]["name"] | "", sizeof(obd2.name));
strlcpy(obd2.mac, doc["obd2"]["mac"] | "", sizeof(obd2.mac));
strlcpy(obd2.pin, doc["obd2"]["pin"] | "", sizeof(obd2.pin));
obd2.checkPIDSupport = doc["obd2"]["checkPIDSupport"] | false;
obd2.protocol = doc["obd2"]["protocol"] | '0';

Expand Down Expand Up @@ -66,6 +67,7 @@ void SettingsClass::writeJson(JsonDocument &doc) {

doc["obd2"]["name"] = obd2.name;
doc["obd2"]["mac"] = obd2.mac;
doc["obd2"]["pin"] = obd2.pin;
doc["obd2"]["checkPIDSupport"] = obd2.checkPIDSupport;
doc["obd2"]["protocol"] = obd2.protocol;

Expand Down Expand Up @@ -230,6 +232,14 @@ void SettingsClass::setOBD2MAC(const char *mac) {
strlcpy(obd2.mac, mac, sizeof(obd2.mac));
}

String SettingsClass::getOBD2Pin() const {
return obd2.pin;
}

void SettingsClass::setOBD2Pin(const char *pin) {
strlcpy(obd2.pin, pin, sizeof(obd2.pin));
}

bool SettingsClass::getOBD2CheckPIDSupport() const {
return obd2.checkPIDSupport;
}
Expand Down
5 changes: 5 additions & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct MobileSettings {
struct OBD2Settings {
char name[65];
char mac[19];
char pin[7];
bool checkPIDSupport;
char protocol;
};
Expand Down Expand Up @@ -147,6 +148,10 @@ class SettingsClass {

void setOBD2MAC(const char *mac);

String getOBD2Pin() const;

void setOBD2Pin(const char *pin);

bool getOBD2CheckPIDSupport() const;

void setOBD2CheckPIDSupport(bool checkPIDSupport);
Expand Down
10 changes: 10 additions & 0 deletions ui/src/app/components/settings.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@
>
</div>
</div>
<div class="row mb-2">
<label for="obd2Pin" class="col-sm-2 control-label">PIN</label>
<div class="col-sm-10">
<input formControlName="pin" type="text" id="obd2Pin" autocapitalize="off" autocorrect="off"
placeholder="PIN"
class="form-control"
[ngClass]="{'is-invalid': obd2.controls.pin.errors}"
>
</div>
</div>
<div class="row mb-2">
<label for="protocol" class="col-sm-2 col-form-label">Protocol</label>
<div class="col-sm-10">
Expand Down
1 change: 1 addition & 0 deletions ui/src/app/components/settings.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class SettingsComponent implements OnInit {
this.obd2 = new FormGroup({
name: new FormControl("", Validators.maxLength(64)),
mac: new FormControl("", Validators.pattern(/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/)),
pin: new FormControl("", [Validators.minLength(4), Validators.maxLength(6), Validators.pattern("^[0-9]+")]),
checkPIDSupport: new FormControl(false),
protocol: new FormControl(OBD2Protocol.AUTOMATIC),
});
Expand Down
1 change: 1 addition & 0 deletions ui/src/app/definitions/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export enum OBD2Protocol {
export interface OBD2Settings {
name?: string;
mac?: string;
pin?: string;
checkPIDSupport?: boolean;
protocol?: OBD2Protocol;
}
Expand Down

0 comments on commit 120d6e2

Please sign in to comment.