![]() ![]()
Of course, you could make the delay smaller so that the script reacts faster. RASPBIAN ONCE I RUN A SCRIPT IT SLOWS DOWN CODEIf you have already added this example code into an application, simply add the time.sleep(0.5) in the while loop, save the changes, and reboot the Pi for the changes to take effect. The delay has been implemented in this tutorial's example code for your convenience. RASPBIAN ONCE I RUN A SCRIPT IT SLOWS DOWN FREEAdding a simple time.sleep(0.5) in the while loop will free up the Pi's CPU significantly but still allow the code to read the button press to reboot or shutdown the Raspberry Pi. Since a Raspberry Pi will just run the program as fast as it can, the delay prevents it from polling the pin at a high rate in the loop. Below are two possible solutions to reduce the load on the Pi's CPU. If it reaches a value above 4, we will restart.Īfter checking with a co-worker, we agreed that the cause was from the while loop constantly checking the pin's state. #For troubleshooting, uncomment this line to view the counter. While GPIO.input(reset_shutdown_pin) = False: If GPIO.input(reset_shutdown_pin) = False: #print('GPIO state is = ", GPIO.input(reset_shutdown_pin)) # For troubleshooting, uncomment this line to output button status on command line #tup(reset_shutdown_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)Ĭommand = "/usr/bin/sudo /sbin/shutdown -r now" ![]() ![]() # 2.) Holding down the button for about 3 seconds the Pi will shutdown. # 1.) If you press the button momentarily, the Pi will reboot. # Raspberry Pi Safe Restart and Shutdown Python Script Just make sure to name this file as safe_restart_shutdown_Pi.py and remember the location that the file was saved. You can also copy the code and paste it in a text editor. #print('GPIO state is = ', GPIO.input(shutdown_pin))ĭownload "safe_restart_shutdown_Pi.py" (PY) Here! # For troubleshooting, uncomment this line to output buton status on command line #short delay, otherwise this code will take up a lot of the Pi's processing power # Check button if we want to shutdown the Pi safely Process = subprocess.Popen(command.split(), stdout=subprocess.PIPE) # Use Qwiic pHAT's pullup resistor so that the pin is not floatingĬommand = "/usr/bin/sudo /sbin/shutdown -h now" #tup(shutdown_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # if using a momentary push button without a resistor. # Use built-in internal pullup resistor so the pin is not floating RASPBIAN ONCE I RUN A SCRIPT IT SLOWS DOWN LICENSE# LICENSE: This code is released under the MIT License () # instead of stacking the pHAT on your Pi. # You can also use any button but you would need to wire it up # Feel like supporting our work? Buy a board from SparkFun! # check out the associated tutorial to adjust your "rc.local" file: # For more information on running this script on startup, # 1.) If you press the button momentarily, the Pi will shutdown. # built-in general purpose button to safely reboot/shutdown you Pi: # This python script takes advantage of the Qwiic pHat v2.0's # Based on code from the following blog and tutorials: # Raspberry Pi Safe Shutdown Python Script Just make sure to name this file as safe_shutdown_Pi.py and remember the location that the file was saved. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |