espd

ESP BT working - 20250402 with CEL

Apr 1st, 2025 (edited)
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.39 KB | None | 0 0
  1. // Downgrade to esp 2.0.17 in board manager
  2.  
  3. #include "BluetoothSerial.h"
  4.  
  5. //#define CORE_DEBUG_LEVEL ARDUHAL_LOG_LEVEL_INFO  // show logs
  6. //#define USE_NAME           // Comment this to use MAC address instead of a slaveName
  7. const char *pin = "1234";
  8.  
  9. #if !defined(CONFIG_BT_SPP_ENABLED)
  10. #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
  11. #endif
  12.  
  13. BluetoothSerial SerialBT;
  14.  
  15. #ifdef USE_NAME
  16. String slaveName = "EMUCANBT_SPP";
  17. #else
  18. String MACadd = "98:DA:20:02:BE:A4";                          // This only for printing
  19. uint8_t address[6] = { 0x98, 0xDA, 0x20, 0x02, 0xBE, 0xA4 };  // Change this to reflect real MAC address of your slave BT device
  20. #endif
  21.  
  22. String myName = "ESP32-BT-Master";
  23.  
  24. void setup() {
  25.     bool connected;
  26.     Serial.begin(1000000);  // Faster serial. Increase serial monitor speed
  27.  
  28.     SerialBT.begin(myName, true);
  29.     Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
  30.  
  31.     #ifndef USE_NAME
  32.       SerialBT.setPin(pin);
  33.       Serial.println("Using PIN");
  34.     #endif
  35.  
  36.     #ifdef USE_NAME
  37.       connected = SerialBT.connect(slaveName);
  38.       Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
  39.     #else
  40.       connected = SerialBT.connect(address);
  41.       Serial.print("Connecting to slave BT device with MAC ");
  42.       Serial.println(MACadd);
  43.     #endif
  44.  
  45.     if (connected) {
  46.         Serial.println("Connected Successfully!");
  47.     } else {
  48.         while (!SerialBT.connected(30000)) {
  49.             Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
  50.         }
  51.     }
  52.     // Disconnect() may take up to 10 secs max
  53.     if (SerialBT.disconnect()) {
  54.         Serial.println("Disconnected Successfully!");
  55.     }
  56.     // This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
  57.     SerialBT.connect();
  58.     if (connected) {
  59.         Serial.println("Reconnected Successfully!");
  60.     } else {
  61.         while (!SerialBT.connected(10000)) {
  62.             Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
  63.         }
  64.     }
  65. }
  66.  
  67. void loop() {
  68.     uint8_t frame[5];
  69.     uint8_t channel;
  70.     uint16_t value;
  71.    
  72.     int chData;
  73.     int rpm;
  74.     int spd;
  75.     float afr;
  76.     float map;
  77.     float boost;
  78.     int tps;
  79.     int clt;
  80.     int ign;
  81.     int inj;
  82.     float bat;
  83.     int cel;
  84.    
  85.  
  86.     // Wait until at least 5 bytes are available
  87.     while (SerialBT.available() >= 5) {
  88.         SerialBT.readBytes(frame, 5);  // Read exactly 5 bytes
  89.  
  90.         // Validate IDCHAR (should always be 0xA3)
  91.       /*  if (frame[1] != 0xA3) {
  92.             Serial.println("Invalid frame: IDCHAR mismatch!");
  93.             return;
  94.         }*/
  95.  
  96.         // Calculate checksum
  97.        /* uint8_t checksum = (frame[0] + frame[1] + frame[2] + frame[3]) % 255;
  98.         if (checksum != frame[4]) {
  99.             Serial.println("Checksum error!");
  100.             return;
  101.         }*/
  102.  
  103.         // Extract values
  104.         channel = frame[0];
  105.         value = (frame[2] << 8) | frame[3];  // Combine High and Low byte
  106.        
  107.         chData = static_cast<int>(channel);
  108.         if(chData == 1) {
  109.             rpm = static_cast<int>(value);
  110.             Serial.println("RPM: " + String(rpm));
  111.         } else if(chData == 28) {
  112.             spd = (static_cast<int>(value) * 1.609);
  113.             Serial.println("SPD: " + String(spd) + " KM/H");
  114.         } else if(chData == 12) {
  115.             afr = (static_cast<float>(value) / 10);
  116.             Serial.println("AFR: " + String(afr));
  117.         } else if(chData == 2) {
  118.             map = (static_cast<float>(value) / 100);       
  119.             boost = (map - 1.0132f);
  120.             Serial.println("MAP: " + String(map) + " BAR");
  121.             Serial.println("BST: " + String(boost) + " BAR");
  122.         } else if(chData == 3) {
  123.             tps = static_cast<int>(value);
  124.             Serial.println("TPS: " + String(tps) + " %");
  125.         } else if(chData == 24) {
  126.             clt = static_cast<int>(value);
  127.             Serial.println("CLT: " + String(clt) + " °C");
  128.         } else if(chData == 6) {
  129.             ign = static_cast<int>(value);
  130.             Serial.println("IGN: " + String(ign) + " °");
  131.         } else if(chData == 19) {
  132.             inj =  static_cast<int>(value);
  133.             Serial.println("INJ: " + String(inj) + " %");
  134.         } else if(chData == 5) {
  135.             bat = (static_cast<float>(value) / 37);  
  136.             Serial.println("BAT: " + String(bat) + " V");
  137.         } else if(chData == 255) {
  138.             cel = decodeCheckEngine(value);
  139.             Serial.println("CEL: " + String(cel));
  140.         }
  141.     }
  142. }
  143.  
  144. int decodeCheckEngine(uint16_t value) {
  145.     int cel_codes = 0;
  146.     if (value == 0) { return 0; }
  147.     else {
  148.         Serial.print("CEL Codes: ");
  149.         if (value & (1 << 0)) { cel_codes++; Serial.print("CLT "); }     // Bit 0
  150.         if (value & (1 << 1)) { cel_codes++; Serial.print("IAT "); }       // Bit 1
  151.         if (value & (1 << 2)) { cel_codes++; Serial.print("MAP "); }       // Bit 2
  152.         if (value & (1 << 3)) { cel_codes++; Serial.print("WBO "); }      // Bit 3
  153.         if (value & (1 << 4)) { cel_codes++; Serial.print("EGT1 "); }      // Bit 4
  154.         if (value & (1 << 5)) { cel_codes++; Serial.print("EGT2 "); }      // Bit 5
  155.         if (value & (1 << 6)) { cel_codes++; Serial.print("EGT ALARM "); } // Bit 6
  156.         if (value & (1 << 7)) { cel_codes++; Serial.print("KNOCK "); }     // Bit 7
  157.         if (value & (1 << 8)) { cel_codes++; Serial.print("FF SENSOR "); } // Bit 8
  158.         if (value & (1 << 9)) { cel_codes++; Serial.print("DBW "); }       // Bit 9
  159.         if (value & (1 << 10)) { cel_codes++; Serial.print("FPR "); }     // Bit 10
  160.        
  161.         //Serial.print("Total CEL Codes: " + cel_codes);
  162.         Serial.println();
  163.         return cel_codes;
  164.     }  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment