09-21-2022, 09:15 PM
Okay ... so the message 10 was correct for pulling out the key.
Message 11 is turn off ignition, so the script should be "msg[0:2] == '11' or msg[0:2] == '10')
So i updated the script and made it selectable, to use ignition or pulling out the key to shutdown the pi:
Added the script as attachment too.
Message 11 is turn off ignition, so the script should be "msg[0:2] == '11' or msg[0:2] == '10')
So i updated the script and made it selectable, to use ignition or pulling out the key to shutdown the pi:
from __future__ import print_function
import os
import sys
import binascii
import datetime
import subprocess
import re
import time
import string
import can
from time import strftime, localtime, sleep, gmtime
#############################################
#
# set here, what you want to have active #
# PLEASE ONLY USE 'true' or 'false' #
#
shutdown_by_ignition_off = 'true' # read ignition off message to shutdown the raspberry pi.
shutdown_by_pulling_key = 'true' # read pulling key message to shutdown the raspberry pi.
#
shutdown_delay = '5' # in seconds #
#
#############################################
can_interface = 'can0'
bus = can.interface.Bus(can_interface, bustype='socketcan')
message = bus.recv()
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def shutdown():
try:
for message in bus:
tmstmp = message.timestamp
canid = str(hex(message.arbitration_id).lstrip('0x').upper())
msg = binascii.hexlify(message.data).decode('ascii').upper()
if canid == '271':
if msg[0:2] == '11' and shutdown_by_ignition_off == 'true':
eprint("ignition off message detected - system will shutdown in", shutdown_delay, "seconds")
sleep(int(shutdown_delay)) # one second delay to read the eprint message, if startet from terminal
eprint("system is shutting down now")
os.system('sudo shutdown -h now')
elif msg[0:2] == '10' and shutdown_by_pulling_key == 'true':
eprint("pulling key message detected - system will shutdown in", shutdown_delay, "seconds")
sleep(int(shutdown_delay)) # one second delay to read the eprint message, if startet from terminal
eprint("system is shutting down now")
os.system('sudo shutdown -h now')
except Exception as e:
eprint("Error in function shutdown", str(e))
shutdown()
while True:
sleep(0.5)
Added the script as attachment too.