95 lines
2.1 KiB
Arduino
95 lines
2.1 KiB
Arduino
// Import required libraries
|
|||
|
|
#include "WiFi.h"
|
||
|
|
#include <ESPAsyncWebServer.h>
|
||
|
|
// needs esp32 2.0.* in boards manaager // https://forum.arduino.cc/t/libary-problem-with-esp32-web-server/1282583/6
|
||
|
|
|
||
|
|
// Set your access point network credentials
|
||
|
|
const char* ssid = "ESP32-Access-Point";
|
||
|
|
const char* password = "123456789";
|
||
|
|
|
||
|
|
|
||
|
|
#include "BluetoothSerial.h"
|
||
|
|
#include "ELMduino.h"
|
||
|
|
|
||
|
|
// Create AsyncWebServer object on port 80
|
||
|
|
AsyncWebServer server(80);
|
||
|
|
|
||
|
|
BluetoothSerial SerialBT;
|
||
|
|
#define ELM_PORT SerialBT
|
||
|
|
#define DEBUG_PORT Serial
|
||
|
|
|
||
|
|
|
||
|
|
ELM327 myELM327;
|
||
|
|
|
||
|
|
|
||
|
|
uint32_t kph = 0;
|
||
|
|
|
||
|
|
String readSpeed() {
|
||
|
|
if (ELM_PORT.isClosed()) {
|
||
|
|
return "0";
|
||
|
|
}
|
||
|
|
|
||
|
|
float tempkph = myELM327.kph();
|
||
|
|
|
||
|
|
if (myELM327.nb_rx_state == ELM_SUCCESS) {
|
||
|
|
kph = (uint32_t)tempkph;
|
||
|
|
Serial.print("kph: ");
|
||
|
|
Serial.println(kph);
|
||
|
|
} else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
|
||
|
|
myELM327.printError();
|
||
|
|
|
||
|
|
return String(kph);
|
||
|
|
// return "123";
|
||
|
|
}
|
||
|
|
|
||
|
|
void connectElm() {
|
||
|
|
if (!ELM_PORT.connect("OBDII")) {
|
||
|
|
DEBUG_PORT.println("Couldn't connect to OBD scanner - Phase 1");
|
||
|
|
// while (1)
|
||
|
|
// ;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!myELM327.begin(ELM_PORT, true, 2000)) {
|
||
|
|
Serial.println("Couldn't connect to OBD scanner - Phase 2");
|
||
|
|
// while (1)
|
||
|
|
// ;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
Serial.println("Connected to ELM327");
|
||
|
|
}
|
||
|
|
|
||
|
|
void setup() {
|
||
|
|
DEBUG_PORT.begin(115200);
|
||
|
|
SerialBT.setPin("1234");
|
||
|
|
ELM_PORT.begin("ArduHUD", true);
|
||
|
|
|
||
|
|
Serial.println("Starting ELM327 to wifi bridge");
|
||
|
|
|
||
|
|
//connectElm();
|
||
|
|
|
||
|
|
// Setting the ESP as an access point
|
||
|
|
Serial.print("Setting AP (Access Point)…");
|
||
|
|
// Remove the password parameter, if you want the AP (Access Point) to be open
|
||
|
|
WiFi.softAP(ssid, password);
|
||
|
|
|
||
|
|
IPAddress IP = WiFi.softAPIP();
|
||
|
|
Serial.print("AP IP address: ");
|
||
|
|
Serial.println(IP);
|
||
|
|
|
||
|
|
server.on("/speed", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||
|
|
request->send_P(200, "text/plain", readSpeed().c_str());
|
||
|
|
});
|
||
|
|
|
||
|
|
// Start server
|
||
|
|
server.begin();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
void loop() {
|
||
|
|
if (ELM_PORT.isClosed()) {
|
||
|
|
Serial.println("Trying to reconnect to ELM327");
|
||
|
|
connectElm();
|
||
|
|
}
|
||
|
|
}
|