CAN Bus 03: Get Vehicle Speed

This sketch shows how to get the Vehicle Speed using the standard OBD-II PIDs codes. 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_03] CAN Bus Get Vehicle Speed -------- 
 *  
 *  This sketch shows how to get the Vehicle Speed using the standard
 *  OBD-II PIDs codes. 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 .  
 */
 
// Include always these libraries before using the CAN Bus functions
#include <WaspCAN.h> 

void setup()
{ 	
  // Initializes 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()
{		
  // Read the value of vehicle speed	
  int vehicleSpeed = CAN.getVehicleSpeed();      

  // Print received data in the serial monitor
  USB.print("Vehicle Speed: ");
  USB.print(vehicleSpeed);
  USB.println(" Km/h");

  delay(1000); 

}

Output

H#
Initializing CAN Bus...
CAN Bus initialized at 500 KBits/s
Vehicle Speed : 175 Km/h
Vehicle Speed : 175 Km/h
Vehicle Speed : 175 Km/h
Vehicle Speed : 79 Km/h
Vehicle Speed : 0 Km/h
Vehicle Speed : 119 Km/h
Vehicle Speed : 199 Km/h
Vehicle Speed : 215 Km/h
Vehicle Speed : 215 Km/h
Vehicle Speed : 215 Km/h

Last updated