OldPCGuy
Posts: 82
Joined: Mon Jan 02, 2017 4:53 pm
Location: Detroit Michigan

Script Behaviour Changes When Headless

Sun Apr 29, 2018 2:09 pm

I have a Pi3 that with the keyboard, mouse, and monitor plugged in launches my Python script and runs as expected. When I deploy the same Pi3 headless the script immediately executes some shutdown code. The shut down should only happen if I press and hold a button for three seconds.

Why the different behaviour running headless?

In /etc/systemd/system/ I have a waterheater.service running that starts my script Multi_DS18B20.py after boot

Code: Select all

Unit]
Description=My WaterHeater Service
After=multi-user.target

[Service]
ExecStart=/usr/bin/python /home/pi/Multi_DS18B20.py

[Install]
WantedBy=multi-user.target
and then a sub set of my script/code

Code: Select all

# shut down button
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN)
gpio.setup(21, gpio.OUT)
	
readInterval = 60		# sec. 

# shutdown sequence started indicator
def flash():
	count = 0
	while (count < 5):
		gpio.output(21, gpio.LOW)
		time.sleep(.25)
		gpio.output(21, gpio.HIGH)
		time.sleep(.25)
		count += 1
	return

# DS18B20 read and file write code here...........

		if readTime < readInterval:  
			t = readTime
			bPush = 0
			while t < readInterval:
				time.sleep(.1)
				t += .1
				if gpio.input(17):
					bPush += .1
					gpio.output(21, gpio.HIGH)
					if bPush > 3: 						
						f.close()
						flash()
						os.system("sudo shutdown -h now")
				else: 
					bPush = 0
					gpio.output(21, gpio.LOW)

So pin 17 has to go high to even enter the shut down loop. Why would plugging in a keyboard, mouse, and monitor affect the BCM GPIO?

OldPCGuy
Posts: 82
Joined: Mon Jan 02, 2017 4:53 pm
Location: Detroit Michigan

Re: Script Behaviour Changes When Headless

Mon May 07, 2018 11:57 pm

Still scratching my head on this one.

I tried using/launch using crontab and it still executes the shutdown logic immediately when headless.

I figured out that the monitor plugged in or not determines if the scrip runs properly.

What could it possibly be? I have a basic OS load.

Code: Select all

PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

I have a class definition with these imports

Code: Select all

import os
import glob
import time
and main code running these

Code: Select all

from DS18B20_Class import DS18B20	# from file X import func Y Modified class from https://www.raspberrypi.org/forums/viewtopic.php?p=861420
from datetime import datetime, date
import time
import csv
import os

# shut down button
import RPi.GPIO as gpio

pcmanbob
Posts: 6440
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Script Behaviour Changes When Headless

Tue May 08, 2018 10:30 am

When trying to solve problems like this I add lots of print statements to watch the variables and disable the actual shutdown part of the code.

something like this for example

Code: Select all


		if readTime < readInterval:  
			t = readTime
			bPush = 0
			while t < readInterval:
				time.sleep(.1)
				t += .1
				if gpio.input(17):
					bPush += .1
                                        print ("bPush = ", bPush)  # added line
					gpio.output(21, gpio.HIGH)
					if bPush > 3: 						
						f.close()
						flash()
						# os.system("sudo shutdown -h now") disabled.........
				else: 
					bPush = 0
					gpio.output(21, gpio.LOW)

you can do the same for the gpio input so you can see if its triggered or not

then you can workout what's happening as your code runs, if you run it via a ssh connection you will see the printed information as the code runs, without the screen / keyboard attached, you could also increase the time.sleep to 0.5 seconds to slow the code a bit so you can see what's happening.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

OldPCGuy
Posts: 82
Joined: Mon Jan 02, 2017 4:53 pm
Location: Detroit Michigan

Re: Script Behaviour Changes When Headless

Fri May 11, 2018 12:31 am

Finally figured this one out, wasn't the code.

I was missing a pull-down resister on pin 17 for the push button input (shutdown button).

:oops:

Odd that with HDMI plugged in it worked; un-plugged it didn't. The input must float around the threshold so the HDMI load pulled it down low enough.

Return to “Troubleshooting”