Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Downgrade to esp 2.0.17 in board manager
- #include "BluetoothSerial.h"
- //#define CORE_DEBUG_LEVEL ARDUHAL_LOG_LEVEL_INFO // show logs
- //#define USE_NAME // Comment this to use MAC address instead of a slaveName
- const char *pin = "1234";
- #if !defined(CONFIG_BT_SPP_ENABLED)
- #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
- #endif
- BluetoothSerial SerialBT;
- #ifdef USE_NAME
- String slaveName = "EMUCANBT_SPP";
- #else
- String MACadd = "98:DA:20:02:BE:A4"; // This only for printing
- uint8_t address[6] = { 0x98, 0xDA, 0x20, 0x02, 0xBE, 0xA4 }; // Change this to reflect real MAC address of your slave BT device
- #endif
- String myName = "ESP32-BT-Master";
- void setup() {
- bool connected;
- Serial.begin(1000000); // Faster serial. Increase serial monitor speed
- SerialBT.begin(myName, true);
- Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
- #ifndef USE_NAME
- SerialBT.setPin(pin);
- Serial.println("Using PIN");
- #endif
- #ifdef USE_NAME
- connected = SerialBT.connect(slaveName);
- Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
- #else
- connected = SerialBT.connect(address);
- Serial.print("Connecting to slave BT device with MAC ");
- Serial.println(MACadd);
- #endif
- if (connected) {
- Serial.println("Connected Successfully!");
- } else {
- while (!SerialBT.connected(30000)) {
- Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
- }
- }
- // Disconnect() may take up to 10 secs max
- if (SerialBT.disconnect()) {
- Serial.println("Disconnected Successfully!");
- }
- // This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
- SerialBT.connect();
- if (connected) {
- Serial.println("Reconnected Successfully!");
- } else {
- while (!SerialBT.connected(10000)) {
- Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
- }
- }
- }
- void loop() {
- uint8_t frame[5];
- uint8_t channel;
- uint16_t value;
- int chData;
- int rpm;
- int spd;
- float afr;
- float map;
- float boost;
- int tps;
- int clt;
- int ign;
- int inj;
- float bat;
- int cel;
- // Wait until at least 5 bytes are available
- while (SerialBT.available() >= 5) {
- SerialBT.readBytes(frame, 5); // Read exactly 5 bytes
- // Validate IDCHAR (should always be 0xA3)
- /* if (frame[1] != 0xA3) {
- Serial.println("Invalid frame: IDCHAR mismatch!");
- return;
- }*/
- // Calculate checksum
- /* uint8_t checksum = (frame[0] + frame[1] + frame[2] + frame[3]) % 255;
- if (checksum != frame[4]) {
- Serial.println("Checksum error!");
- return;
- }*/
- // Extract values
- channel = frame[0];
- value = (frame[2] << 8) | frame[3]; // Combine High and Low byte
- chData = static_cast<int>(channel);
- if(chData == 1) {
- rpm = static_cast<int>(value);
- Serial.println("RPM: " + String(rpm));
- } else if(chData == 28) {
- spd = (static_cast<int>(value) * 1.609);
- Serial.println("SPD: " + String(spd) + " KM/H");
- } else if(chData == 12) {
- afr = (static_cast<float>(value) / 10);
- Serial.println("AFR: " + String(afr));
- } else if(chData == 2) {
- map = (static_cast<float>(value) / 100);
- boost = (map - 1.0132f);
- Serial.println("MAP: " + String(map) + " BAR");
- Serial.println("BST: " + String(boost) + " BAR");
- } else if(chData == 3) {
- tps = static_cast<int>(value);
- Serial.println("TPS: " + String(tps) + " %");
- } else if(chData == 24) {
- clt = static_cast<int>(value);
- Serial.println("CLT: " + String(clt) + " °C");
- } else if(chData == 6) {
- ign = static_cast<int>(value);
- Serial.println("IGN: " + String(ign) + " °");
- } else if(chData == 19) {
- inj = static_cast<int>(value);
- Serial.println("INJ: " + String(inj) + " %");
- } else if(chData == 5) {
- bat = (static_cast<float>(value) / 37);
- Serial.println("BAT: " + String(bat) + " V");
- } else if(chData == 255) {
- cel = decodeCheckEngine(value);
- Serial.println("CEL: " + String(cel));
- }
- }
- }
- int decodeCheckEngine(uint16_t value) {
- int cel_codes = 0;
- if (value == 0) { return 0; }
- else {
- Serial.print("CEL Codes: ");
- if (value & (1 << 0)) { cel_codes++; Serial.print("CLT "); } // Bit 0
- if (value & (1 << 1)) { cel_codes++; Serial.print("IAT "); } // Bit 1
- if (value & (1 << 2)) { cel_codes++; Serial.print("MAP "); } // Bit 2
- if (value & (1 << 3)) { cel_codes++; Serial.print("WBO "); } // Bit 3
- if (value & (1 << 4)) { cel_codes++; Serial.print("EGT1 "); } // Bit 4
- if (value & (1 << 5)) { cel_codes++; Serial.print("EGT2 "); } // Bit 5
- if (value & (1 << 6)) { cel_codes++; Serial.print("EGT ALARM "); } // Bit 6
- if (value & (1 << 7)) { cel_codes++; Serial.print("KNOCK "); } // Bit 7
- if (value & (1 << 8)) { cel_codes++; Serial.print("FF SENSOR "); } // Bit 8
- if (value & (1 << 9)) { cel_codes++; Serial.print("DBW "); } // Bit 9
- if (value & (1 << 10)) { cel_codes++; Serial.print("FPR "); } // Bit 10
- //Serial.print("Total CEL Codes: " + cel_codes);
- Serial.println();
- return cel_codes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment