หน้าเว็บ

วันพฤหัสบดีที่ 29 สิงหาคม พ.ศ. 2562

ปุ่มเปิด/ปิด Raspberry Pi

ปุ่มเปิด/ปิด Raspberry Pi
1.ต่อ Switch แบบกดติดปล่อยดับที่ขา 5 (GPIO3) กับขา 6 (GND) หรือจะต่อที่ GPIO อื่นก็ได้ แต่ต้องต้องต่อตัวต้านทานเพิ่มด้วย เพื่อป้องกันความเสียหายกับบอร์ด (GPIO3 มีตัวต้านทานภายในอยู่แล้ว) ที่มา : https://learn.sparkfun.com/tutorials/pull-up-resistors
2.กรณีต่อที่ GPIO3 ต้องเขียน Code เปิดใช้งานตัวต้านทานภายในด้วย
  1. GPIO.setmode(GPIO.BOARD)
  2. GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)

3.เขียน Code สำหรับควบคุมการเปิด/ปิด : pishutdown.py
  1. #!/usr/bin/python
  2. # shutdown/reboot(/power on) Raspberry Pi with pushbutton
  3.  
  4. import RPi.GPIO as GPIO
  5. from subprocess import call
  6. from datetime import datetime
  7. import time
  8.  
  9. # pushbutton connected to this GPIO pin, using pin 5 also has the benefit of
  10. # waking / powering up Raspberry Pi when button is pressed
  11. shutdownPin = 5
  12.  
  13. # if button pressed for at least this long then shut down. if less then reboot.
  14. shutdownMinSeconds = 3
  15.  
  16. # button debounce time in seconds
  17. debounceSeconds = 0.01
  18.  
  19. GPIO.setmode(GPIO.BOARD)
  20. GPIO.setup(shutdownPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  21.  
  22. buttonPressedTime = None
  23.  
  24. def buttonStateChanged(pin):
  25.     global buttonPressedTime
  26.  
  27.     if not (GPIO.input(pin)):
  28.         # button is down
  29.         if buttonPressedTime is None:
  30.             buttonPressedTime = datetime.now()
  31.     else:
  32.         # button is up
  33.         if buttonPressedTime is not None:
  34.             elapsed = (datetime.now() - buttonPressedTime).total_seconds()
  35.             buttonPressedTime = None
  36.             if elapsed >= shutdownMinSeconds:
  37.                 # button pressed for more than specified time, shutdown
  38.                 call(['shutdown', '-h', 'now'], shell=False)
  39.             elif elapsed >= debounceSeconds:
  40.                 # button pressed for a shorter time, reboot
  41.                 call(['shutdown', '-r', 'now'], shell=False)
  42.  
  43. # subscribe to button presses
  44. GPIO.add_event_detect(shutdownPin, GPIO.BOTH, callback=buttonStateChanged)
  45.  
  46. while True:
  47.     # sleep to reduce unnecessary CPU usage
  48.     time.sleep(5)

* กดสวิตช์ค้างน้อยกว่า 3 วินาที จะเป็นการ reset, มากกว่า 3 วินาทีจะเป็นการ Shutdown, ถ้า Shutdown ไปแล้วจ่ายไฟค้างไว้ กดสวิตช์จะเป็นการเปิด

4.ถ้าอยากเก็บ Log ดูการทำงาน เพิ่ม Code ดังนี้
  1. import logging
  2.  
  3. logger = logging.getLogger(__name__)
  4. logger.setLevel(logging.INFO)
  5.  
  6. # create a file handler
  7. handler = logging.FileHandler('pishutdown.log')
  8. handler.setLevel(logging.INFO)
  9.  
  10. # create a logging format
  11. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  12. handler.setFormatter(formatter)
  13.  
  14. # add the handlers to the logger
  15. logger.addHandler(handler)
  16.  
  17. logger.info('Shutdown/Restart')

5.เรียก script ให้ทำงาน
  1. sudo python pishutdown.py

6.กำหนดให้ script ทำงานเป็น Service : /etc/systemd/system/pishutdown.service
  1. [Service]
  2. ExecStart=/usr/bin/python /path_to_pishutdown/pishutdown.py
  3. WorkingDirectory=/path_to_pishutdown/
  4. Restart=always
  5. StandardOutput=syslog
  6. StandardError=syslog
  7. SyslogIdentifier=pishutdown
  8. User=root
  9. Group=root
  10.  
  11. [Install]
  12. WantedBy=multi-user.target

7.Enable service:
  1. sudo systemctl enable pishutdown.service

8.Run service (will be automatically started on next reboot):
  1. sudo systemctl start pishutdown.service

ที่มา : https://gilyes.com/pi-shutdown-button/

 

ไม่มีความคิดเห็น:

แสดงความคิดเห็น