print-service.py take 2 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/python
  2. import RPi.GPIO as GPIO # import RPi.GPIO module
  3. import time
  4. import os
  5. import subprocess
  6. GPIO.setmode(GPIO.BCM) # choose BCM or BOARD
  7. GPIO.setup(23, GPIO.OUT) # set a port/pin as an output
  8. GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # set a port/pin as an input
  9. f1 = os.popen('lpq -a')
  10. printq = f1.read()
  11. printer_state = GPIO.input(24)
  12. if "Job" in printq:
  13. print "Job(s) are in print queue"
  14. printer_state = GPIO.input(24)
  15. print "Printer state is:", printer_state
  16. if printer_state == 1:
  17. while printer_state == 1: #1 equals off
  18. printer_state = GPIO.input(24)
  19. GPIO.output(23, 1) #Turn on printer
  20. time.sleep(45)
  21. print "Printer is started"
  22. os.popen('cupsenable Pi_Deskjet_F4400') ## Restart print queue if needed
  23. time.sleep(45)
  24. if "no entries" in printq:
  25. print "Queue is now empty, printing complete."
  26. else:
  27. print "Done printing, this is the else statement."
  28. else:
  29. print "Else statement when printer was off"
  30. ## Turn off printer
  31. ## Check if successful, if not, send signal again to power off.
  32. print "Shutting down printer."
  33. printer_state = GPIO.input(24)
  34. while printer_state == 0: #0 equals on
  35. printer_state = GPIO.input(24)
  36. GPIO.output(23, 0) #Turn printer off
  37. time.sleep(10)
  38. print "Printer will sleep now."
  39. else:
  40. #if "no entries" in printq:
  41. print "Queue is empty"
  42. print "Done"
  43. GPIO.cleanup()