I've built a head unit for my car running Open Auto Pro. This mirrors Android Auto and my phone connects to the Pi via Bluetooth. To get round the issue of a clean shutdown without messing with hardware, I'm running a Python script to detect the phone on Bluetooth and run a shutdown if it's not detected. This is my code with some print statements and the shutdown command commented to help debug:
Code: Select all
#!/usr/bin/python2 import bluetooth import time from subprocess import call while True: score = 0 for x in range(15): result = bluetooth.lookup_name('xx:xx:xx:xx:xx:xx', timeout=5) if (result == None): score = score +1 print "Not detected" print score if (score == 2): call("echo 1 > /sys/class/backlight/rpi_backlight/bl_power", shell=True) print "Screen off" time.sleep(10) else: score = 0 print "Detected" print score call("echo 0 > /sys/class/backlight/rpi_backlight/bl_power", shell=True) print "Screen on again" time.sleep(10) if (score == 15): #call("sudo shutdown -h 5", shell=True) print "Gone away forever" else: #call("sudo shutdown -c", shell=True) print "Back again" time.sleep(30)
So, running it from the command line at the moment with my phone in airplane mode and one iteration shows:
You can see that the 11th iteration says "Back again" even though it says "Undetected". So it looks like it come out of the 15 x loop, seen that the score isn't 15 and then started again. This doesn't happen all the time, it's not always at iteration 11 but in the 4 times I've seen it it's been at 11 three times and 12 once.
Code: Select all
0 Screen on again Not detected 1 Not detected 2 Screen off Not detected 3 Not detected 4 Not detected 5 Not detected 6 Not detected 7 Not detected 8 Not detected 9 Not detected 10 Not detected 11 Back again Not detected 1 Not detected 2 Screen off Not detected 3 Not detected 4 Not detected 5 Not detected 6 Not detected
I think my Python logic is "sound" but I'm wondering if there's a timing issue going on in the background with Bluetooth and Raspian and that's what's causing the problem. I'm going to leave it running. If it's always around the 11th iteration then I'll set my loop to 10 and extend the time period to see if it always happens around the same time rather than with the same iteration. Apart from that, I'm a bit lost for where you start.
I'm not sure why it's doing this?