RaspberryPi webiopi脚本错误不起作用

时间:2014-05-09 22:12:15

标签: python raspberry-pi whitespace indentation webiopi

我想尝试使用RaspberryPi,但.py代码不起作用。它给了我很多错误。

  • 空格错误
  • 缩进错误:expected an indented block " global VELUX_STATE, AUTO_mode "

我只复制了官方论坛中的代码,这些代码适用于其他人。

链接: http://egrasland.blogspot.fr/2014/01/control-your-velux-roller-shutter-with.html

我复制代码并粘贴“sudo nano script.py”

我做错了什么?

此外,来自RaspberryPi的官方webiopi的代码对我来说也不起作用。调试给出错误,程序无法启动。

链接:https://code.google.com/p/webiopi/wiki/Tutorial_Basis

1 个答案:

答案 0 :(得分:0)

感谢您的回答。

我使用的代码是:

Python服务器脚本:

import webiopi import datetime

GPIO = webiopi.GPIO     
VELUX_UP = 17 # GPIO pin using BCM numbering VELUX_DOWN = 18 # GPIO pin using BCM numbering VELUX_STOP = 27 # GPIO pin using BCM numbering     
VELUX_STATE = 0 # DOWN AUTO_MODE = 1     
HOUR_UP  = 9   # Turn Velux UP at HOUR_DOWN = 19 # Turn Velux Down at     
# setup function is automatically called at WebIOPi startup 
def setup():   global VELUX_STATE, AUTO_MODE     
    # set the GPIO used for VELUX command to output  
    GPIO.setFunction(VELUX_UP, GPIO.OUT)   
    GPIO.setFunction(VELUX_DOWN,GPIO.OUT)

    # STOP function not used at this time   
    GPIO.setFunction(VELUX_STOP,GPIO.OUT)
    GPIO.digitalWrite(VELUX_STOP, GPIO.LOW)

    # retrieve current datetime   now = datetime.datetime.now()     
    if (AUTO_MODE==1):
        # test time
        if ((now.hour >= HOUR_UP) and (now.hour < HOUR_DOWN)):
            GPIO.digitalWrite(VELUX_UP, GPIO.HIGH)
            webiopi.sleep(0.2)
            GPIO.digitalWrite(VELUX_UP, GPIO.LOW)
            VELUX_STATE = 1
        else: 
            GPIO.digitalWrite(VELUX_DOWN, GPIO.HIGH)
            webiopi.sleep(0.2)
            GPIO.digitalWrite(VELUX_DOWN, GPIO.LOW)
            VELUX_STATE = 0

# loop function is repeatedly called by WebIOPi  
def loop():   global VELUX_STATE, AUTO_MODE     
    # retrieve current datetime   now = datetime.datetime.now()     
    if (AUTO_MODE==1):
        # toggle VELUX UP all days at the correct time
        if ((now.hour == HOUR_UP) and (now.minute == 0) and (VELUX_STATE == 0)):
            GPIO.digitalWrite(VELUX_UP, GPIO.HIGH)
            webiopi.sleep(0.2)
            GPIO.digitalWrite(VELUX_UP, GPIO.LOW)
            VELUX_STATE = 1 #UP

        # toggle VELUX DOWN all days at the correct time
        if ((now.hour == HOUR_DOWN) and (now.minute == 0) and (VELUX_STATE == 1)):
            GPIO.digitalWrite(VELUX_DOWN, GPIO.HIGH)
            webiopi.sleep(0.2)
            GPIO.digitalWrite(VELUX_DOWN, GPIO.LOW)
            VELUX_STATE = 0 #DOWN
            # gives CPU some time before looping again   webiopi.sleep(1)

# destroy function is called at WebIOPi shutdown 
def destroy():   
    GPIO.digitalWrite(VELUX_UP, GPIO.LOW)   
    GPIO.digitalWrite(VELUX_DOWN, GPIO.LOW)   
    GPIO.digitalWrite(VELUX_STOP, GPIO.LOW)


@webiopi.macro def getHours():
    return "%d;%d" % (HOUR_UP, HOUR_DOWN)

@webiopi.macro def setHours(on, off):
    global HOUR_UP, HOUR_DOWN
    HOUR_UP = int(on)
    HOUR_DOWN = int(off)
    return getHours()

@webiopi.macro def getAutoMode():
    return "%d" % (AUTO_MODE)

@webiopi.macro def setAutoMode():
    global AUTO_MODE

    if AUTO_MODE:
        AUTO_MODE=0
    else:
        AUTO_MODE=1
    return getAutoMode()

调试不能在这里发布,因为它在我的RaspberryPi和浏览器上会慢:) 但他给出的第一个错误是评论VELUX UP的空白空间 “HOUR_UP = 9#转动Velux UP” 我解决了这个问题,但那是错误 “def setup():   全局VELUX_STATE,AUTO_MODE“

为什么?

谢谢!