-2
#!/usr/bin/python



# Import der Python libraries

import RPi.GPIO as GPIO

import time

import datetime

import subprocess



# Verwendung des Board Mode, Angabe der PIN Nummern anstelle der GPIO BCM Nummer

GPIO.setmode(GPIO.BOARD)



# GPIO definieren, bei mir ist es PIN 8

GPIO_PIR = 8



#  GPIO als "Input" festlegen

GPIO.setup(GPIO_PIR,GPIO.IN)



Current_State  = 0

Previous_State = 0



try:



# erst mal schlafen bis der bootvorgang abgeschlossen ist

time.sleep(60)



# Warten bis Sensor sich meldet

while GPIO.input(GPIO_PIR)==1:

  Current_State  = 0



subprocess.Popen('echo initilized PIR | wall', shell=True)



# Schleife bis CTRL+C

while True :



  #Status von Sensor auslesen

  Current_State = GPIO.input(GPIO_PIR)



  if Current_State==1 and Previous_State==0:



    # Kommando zum anschalten, Frambuffer erneuern

    subprocess.Popen('echo Monitor on | wall', shell=True)

    subprocess.Popen('/opt/vc/bin/tvservice -p', shell=True)

    subprocess.Popen('fbset -depth 8', shell=True)

    subprocess.Popen('fbset -depth 16', shell=True)

    subprocess.Popen('sudo /bin/chvt 6 && sudo /bin/chvt 7', shell=True)

    Previous_State=1



  elif Current_State==0 and Previous_State==1:



    # Ausschalten des Monitors

    subprocess.Popen('echo Monitor off | wall', shell=True)

    subprocess.Popen('/opt/vc/bin/tvservice -o', shell=True)

    Previous_State=0



  # 5 Sek Warten

  time.sleep(5)



except KeyboardInterrupt:

print " Exit"

GPIO.cleanup()

Hello, i am getting:

File "PIR.py", line 30
  time.sleep(60)
     ^
IndentationError: expected an indented block

Can you help me? when i set a tab before "time.sleep" it Comes a Syntax error in the line "while GPIO.input(GPIO_PIR)==1:". I use this script to manage a PIR sensor for the Magic mirror but it wont work. So i wrote in the terminal "python PIR.py". After that i recognize the error, it is logic that the PIR sensor wont work. But I am a noob in python, so I would like to have your help.

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
XXXL
  • 1
  • 1
  • 1
    `try` "opens" a new block. The following `time.sleep(60)` - and most everything following - is therefore not correctly indented to be "inside" the `try..except`. Using small methods makes indentation (and code in general) easier to follow, as does using an appropriate Python-specific editor to highlight these issues while editing code. – user2864740 Nov 14 '17 at 01:50
  • okay thank you for the fast answer. How can i fast fix it or. how looks it correctly ? – XXXL Nov 14 '17 at 01:56
  • The "immediate fix" is to add additional indentation to be valid syntax. Here is how to use [`try..except` (shows indentation as well)](https://docs.python.org/2/tutorial/errors.html#handling-exceptions). I recommend putting everything inside the `try..` into a separate method as the `try..except` is mostly just boilerplate unrelated to the actual problem being worked on; this change may also make the indentation more clear. – user2864740 Nov 14 '17 at 02:05

1 Answers1

0

A block is a group of statements in a program or script. Usually it consists of at least one statement and of declarations for the block, depending on the programming or scripting language. A language, which allows grouping with blocks, is called a block structured language. Python is such a language. There are a lot of documentation and tutorials that will tell you why. Here is a start: https://www.python-course.eu/python3_blocks.php

Your code miss some of these block structures when you are required to use them, for example with try: and else:. Other examples you see this is while True: and if - else statements.

#!/usr/bin/python
# Import der Python libraries
import RPi.GPIO as GPIO
import time
import datetime
import subprocess

# Verwendung des Board Mode, Angabe der PIN Nummern anstelle der GPIO BCM Nummer
GPIO.setmode(GPIO.BOARD)
# GPIO definieren, bei mir ist es PIN 8
GPIO_PIR = 8
#  GPIO als "Input" festlegen
GPIO.setup(GPIO_PIR,GPIO.IN)
Current_State  = 0
Previous_State = 0
try:
  # erst mal schlafen bis der bootvorgang abgeschlossen ist
  time.sleep(60)
  # Warten bis Sensor sich meldet
  while GPIO.input(GPIO_PIR)==1:
    Current_State  = 0
  subprocess.Popen('echo initilized PIR | wall', shell=True)
  # Schleife bis CTRL+C
  while True :
    #Status von Sensor auslesen
    Current_State = GPIO.input(GPIO_PIR)
    if Current_State==1 and Previous_State==0:
      # Kommando zum anschalten, Frambuffer erneuern
      subprocess.Popen('echo Monitor on | wall', shell=True)
      subprocess.Popen('/opt/vc/bin/tvservice -p', shell=True)
      subprocess.Popen('fbset -depth 8', shell=True)
      subprocess.Popen('fbset -depth 16', shell=True)
      subprocess.Popen('sudo /bin/chvt 6 && sudo /bin/chvt 7', shell=True)
      Previous_State=1
    elif Current_State==0 and Previous_State==1:
      # Ausschalten des Monitors
      subprocess.Popen('echo Monitor off | wall', shell=True)
      subprocess.Popen('/opt/vc/bin/tvservice -o', shell=True)
      Previous_State=0
    # 5 Sek Warten
    time.sleep(5)
except KeyboardInterrupt:
  print " Exit"
  GPIO.cleanup()
pastaleg
  • 1,782
  • 2
  • 17
  • 23
  • Thank you really much!! It works. But now I have a new problem. I want to start this python File with cronjob on the RPi3. But i see on another website that they write in crontab -e this line: @reboot /home/pi/PIR.sh So i need a PIR.sh File. My first Approach was: #!/usr/bin/python sudo python PIR.py But this don't work. Can you help me ? – XXXL Nov 15 '17 at 15:22
  • Great! Accept this answer as it solved the stated question. As for your follow up, cronjob is a scheduler that runs a command. The first statement, @reboot states to run this when the RPi3 reboots, after that comes the command. As this file is a python file (.py), you need to say `@reboot python /home/pi/PIR.py` replace /home/pi/ with the actual path to the file – pastaleg Nov 15 '17 at 20:12
  • Hello. thank you. I have a new question. The PIR works but when the PIR don't detect a motion after a longer time, the display won't go on. When i look at the SSH, I see that the PIR detect the motion but the display shows no picture. What can i do? – XXXL Nov 27 '17 at 22:19