Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Manual OBDII query
--Implements a simple OBDII engine in lua scripting, with a configurable delay between PIDs.
-- IMPORTANT: Ensure built-in OBDII is turned off in Setup
--
--500K baud. set your baud rate here.
initCAN(0, 500000)
--standard 11 bit PID request 0x7DF
--extended 29 bit PID request 0x18DB33F1
--standard 11 bit PID response 0x7E8
--extended 29 bit PID response 0x18DAF110
standard = true
--delay between PID queries, in mS
delay = 1000
--this function checks the PID response
function checkPidResponse()
local pid = nil
local id
local ext
local data
id, ext, data = rxCAN(0, 1000)
if ext == 1 and id == 0x18DAF110 then
pid = data[3]
end
if ext == 0 and id == 0x7E8 then
pid = data[3]
end
if pid ~= nil then
local mapper = mappers[pid]
if mapper ~= nil then
local value = mapper(data)
local chId = pids[pid]
setChannel(chId, value)
return true
end
end
end
function request_pid(pid)
local r
if standard then
r = txCAN(0, 0x7DF,0 , {2, 1, pid})
else
r = txCAN(0, 0x18DB33F1,1 , {2, 1, pid})
end
-- println('request pid ' ..pid ..' result ' ..r)
end
function onTick()
sleep(delay)
request_pid(query[queryIndex])
checkPidResponse()
queryIndex = queryIndex + 1
if queryIndex > queryLen then queryIndex = 1 end
end
function rpmMapper(data)
a = data[4]
b = data[5]
return (a * 256 + b) / 4
end
function tpsMapper(data)
return data[4] / 2.55
end
function tempMapper(data)
local degC = data[4] - 40
local degF = degC * 1.8 + 32
return degF
--return degC
end
rpmId = addChannel("RPM", 25, 0, 0, 10000)
tpsId = addChannel("TPS", 25, 0, 0, 100)
tempId = addChannel("EngineTemp", 1, 0, 0, 300)
setTickRate(50)
pids = {}
pids[12] = rpmId
pids[17] = tpsId
pids[5] = tempId
query = {12,17, 12,17, 12,17, 12,17, 12,17, 5}
queryLen = #query
queryIndex = 1
mappers = {}
mappers[12] = rpmMapper
mappers[17] = tpsMapper
mappers[5] = tempMapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.