This sketch shows how to use the general function CiARequest and the standard OBD-II PIDs codes to get data from the vehicle. This codes are used to request data from a vehicle, used as a diagnostic tool.
Required Materials
1 x Waspmote
1 x Battery
1 x CAN Bus
1 x OBD cable
Notes
- Waspmote must be powered with a battery
- CAN Bus was designed to work in automotive applications, but it can be used in other communication areas.
- The CAN Bus library integrates the most important PID functions, but it does not include all of them.
- This example can be executed in Waspmote v12 and Waspmote v15
Code
/*
* ------ [CAN-Bus_05] General PIDs --------
*
* This sketch shows how to use the general function CiARequest and the
* standard OBD-II PIDs codes to get data from the vehicle. This codes
* are used to request data from a vehicle, used as a diagnostic tool.
*
* Copyright (C) 2014 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Version: 0.1
* Implementation: Luis Antonio Martin & Ahmad Saad
*/
// Include always these libraries before using the CAN Bus functions
#include <WaspCAN.h>
#define myPID 0x00 // <------- Define here your PID
void setup()
{
// Turn on the USB
USB.ON();
delay(100);
// Print initial message
USB.println("Initializing CAN Bus...");
// Configuring the BUS at 500 Kbit/s
CAN.ON(500);
USB.println("CAN Bus initialized at 500 KBits/s");
}
void loop()
{
int data;
// Read the value of RPM if the engine
CAN.CiARequest(myPID);
if (CAN.messageRx.id == ID_RESPONSE)
{
data = uint16_t(CAN.messageRx.data[3]) ; // <-------- Insert the formula here
CAN.printMessage(&CAN.messageRx);
}
// Print received data in the serial monitor
USB.print("Returned data: ");
USB.print(data);
USB.print("\n");
delay(1000);
}
Output
H#
Initializing CAN Bus...
CAN Bus initialized at 500 KBits/s
id: 7E8 rtr: 0 => data: 6,41,0,FF,FF,FF,FF,0
Returned data : 255