OBD II see: https://os.mbed.com/users/okini3939/notebook/obd2-can-bus/

Dependencies:   OBD2 mbed

Committer:
okini3939
Date:
Wed Dec 20 07:21:57 2017 +0000
Revision:
0:c6f40de7ab86
1st build;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:c6f40de7ab86 1 #include "mbed.h"
okini3939 0:c6f40de7ab86 2 #include "OBD2.h"
okini3939 0:c6f40de7ab86 3
okini3939 0:c6f40de7ab86 4 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
okini3939 0:c6f40de7ab86 5 Serial pc(USBTX, USBRX);
okini3939 0:c6f40de7ab86 6 OBD2 obd(p30, p29);
okini3939 0:c6f40de7ab86 7
okini3939 0:c6f40de7ab86 8 float readObd2 (int cmd) {
okini3939 0:c6f40de7ab86 9
okini3939 0:c6f40de7ab86 10 if (obd.request(cmd)) {
okini3939 0:c6f40de7ab86 11 return -1;
okini3939 0:c6f40de7ab86 12 }
okini3939 0:c6f40de7ab86 13 return obd.read();
okini3939 0:c6f40de7ab86 14 }
okini3939 0:c6f40de7ab86 15
okini3939 0:c6f40de7ab86 16 int main() {
okini3939 0:c6f40de7ab86 17 int i;
okini3939 0:c6f40de7ab86 18 float f;
okini3939 0:c6f40de7ab86 19
okini3939 0:c6f40de7ab86 20 pc.baud(115200);
okini3939 0:c6f40de7ab86 21 pc.printf("--- OBD2\n\r");
okini3939 0:c6f40de7ab86 22 led1 = 1;
okini3939 0:c6f40de7ab86 23
okini3939 0:c6f40de7ab86 24 for (;;) {
okini3939 0:c6f40de7ab86 25 if (pc.readable()) {
okini3939 0:c6f40de7ab86 26 i = 0;
okini3939 0:c6f40de7ab86 27 switch (pc.getc()) {
okini3939 0:c6f40de7ab86 28 case '1': i = ENGINE_LOAD; break;
okini3939 0:c6f40de7ab86 29 case '2': i = THROTTLE; break;
okini3939 0:c6f40de7ab86 30 case '3': i = FUEL_LEVEL; break;
okini3939 0:c6f40de7ab86 31 case '4': i = ENGINE_COOLANT_TEMP; break;
okini3939 0:c6f40de7ab86 32 case '5': i = INTAKE_TEMP; break;
okini3939 0:c6f40de7ab86 33 case '6': i = AMB_AIR_TEMP; break;
okini3939 0:c6f40de7ab86 34 case '7': i = INTAKE_PRESSURE; break;
okini3939 0:c6f40de7ab86 35 case '8': i = ENGINE_RPM; break;
okini3939 0:c6f40de7ab86 36 case '9': i = VEHICLE_SPEED; break;
okini3939 0:c6f40de7ab86 37 }
okini3939 0:c6f40de7ab86 38
okini3939 0:c6f40de7ab86 39 if (i) {
okini3939 0:c6f40de7ab86 40 f = readObd2(i);
okini3939 0:c6f40de7ab86 41 pc.printf("PID=%02x, Result=%f\r\n", i, f);
okini3939 0:c6f40de7ab86 42 }
okini3939 0:c6f40de7ab86 43 led2 = !led2;
okini3939 0:c6f40de7ab86 44 }
okini3939 0:c6f40de7ab86 45 }
okini3939 0:c6f40de7ab86 46 }