How to control LEDs via a LDR and a button as well? - python

I'm currently working on a Node-RED webapplication to control several leds with switch buttons. Next, using a Python script to read the LDR value. This value will be used to determine if it's light or dark outside. When it's dark, one led has to be enabled and the others disabled, the opposite when it's light.
#!/usr/bin/env python
import cgitb ; cgitb.enable()
import spidev
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
spi = spidev.SpiDev() # create spi object
spi.open(0,0) # open spi port 0, device CS0 pin 24
# read SPI data 8 possible adc's (0 thru 7)
def readadc(adcnum):
if ((adcnum > 7) or (adcnum < 0)):
return -1
r = spi.xfer2([1,(8+adcnum)<<4,0])
adcout = ((r[1]&3) << 8) + r[2]
return adcout
As you can see, the rest of this script keeps going because of the while loop.
while True:
tmp0 = readadc(0) # read channel 0
if(tmp0 > 500):
msg = "LIGHT"
GPIO.output(17, False)
else:
msg = "DARK"
GPIO.output(17, True)
GPIO.output(18, False)
GPIO.output(22, False)
GPIO.output(27, False)
time.sleep(0.3)
print msg
Finnally, I can't use both. Once I click one a switch the led will go on for 1 second. I need to find a manner to combine both methods. Any tips?

IF what you want, is for the light conditions to dictate the initial led state, as well as updating the state when the light conditions change; but the switches to over-ride these until the light conditions change, then this should help:
light_set = None
while True:
tmp0 = readadc(0) # read channel 0
if(tmp0 > 500) and light_set != "LIGHT":
msg = "LIGHT"
light_set = msg
GPIO.output(17, False)
else if light_set != "DARK":
msg = "DARK"
light_set = msg
GPIO.output(17, True)
GPIO.output(18, False)
GPIO.output(22, False)
GPIO.output(27, False)
time.sleep(0.3)
print msg
This should "remember" what this code section last set the led to, so it won't keep setting it if it's changed with a switch. It will still change it, when the current light conditions aren't what it last set it to though.

Related

PWM problem with importing motor module in python

I am having problems with a module that I am trying to import in python.
It is being run through a RPi 4b, driving 4 motors through 2 L298N h bridges.
When I run this code (FourMotorClassWithTurn) there is no problem and the motors perform as expected.
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)#setup the pin out to be board numbers not BCM
GPIO.setwarnings(False)#stops set warnings from showing
class Motor():
def __init__(self, EnFL, In1FL, In2FL, EnFR, In3FR, In4FR,
EnBR, In1BR, In2BR, EnBL, In3BL, In4BL):
self.EnFL = EnFL
self.In1FL = In1FL
self.In2FL = In2FL
#Front left motor
self.EnFR = EnFR
self.In3FR = In3FR
self.In4FR = In4FR
#Front right motor
self.EnBR = EnBR
self.In1BR = In1BR
self.In2BR = In2BR
#Back right motor
self.EnBL = EnBL
self.In3BL = In3BL
self.In4BL = In4BL
#Back left motor
GPIO.setup(self.EnFL,GPIO.OUT)
GPIO.setup(self.In1FL,GPIO.OUT)
GPIO.setup(self.In2FL,GPIO.OUT)
GPIO.setup(self.EnFR,GPIO.OUT)
GPIO.setup(self.In3FR,GPIO.OUT)
GPIO.setup(self.In4FR,GPIO.OUT)
GPIO.setup(self.EnBR,GPIO.OUT)
GPIO.setup(self.In1BR,GPIO.OUT)
GPIO.setup(self.In2BR,GPIO.OUT)
GPIO.setup(self.EnBL,GPIO.OUT)
GPIO.setup(self.In3BL,GPIO.OUT)
GPIO.setup(self.In4BL,GPIO.OUT)
self.pwmFL = GPIO.PWM(self.EnFL,100);
self.pwmFL.start(0);
self.pwmFR = GPIO.PWM(self.EnFR,100);
self.pwmFR.start(0);
self.pwmBR = GPIO.PWM(self.EnBR,100);
self.pwmBR.start(0);
self.pwmBL = GPIO.PWM(self.EnBL,100);
self.pwmBL.start(0);
#-------------------------------------------------------------------------
def move(self, speed=0.5, turn=0, time=0):
"""
Sets the motor to move
Parameters
----------
speed : TYPE, float
DESCRIPTION. Sets the speed of the motor 0 - 1. The default is 0.
turn : TYPE, float
DESCRIPTION. Sets the turn direction and rate of the
platform (-1) - 1. Full left = -1, full right = 1 The default is 0.
time : TYPE, float
DESCRIPTION. Sets the run time of the motor in seconds.
The default is 0.
Returns
-------
None.
"""
speed *= 100 #converts speed in PWM duty cycle value
turn *= 100 #converts turn in PWM duty cycle value
leftSpeed = speed + turn #adjusts the motor speed on each side for
#differential steering
rightSpeed = speed - turn
if leftSpeed>100: leftSpeed=100
elif leftSpeed<-100: leftSpeed=-100
if rightSpeed>100: rightSpeed=100
elif rightSpeed<-100: rightSpeed=-100 #PWM can only accept a maximum
#of 100 so values need limiting
self.pwmFL.ChangeDutyCycle(abs(leftSpeed))
self.pwmFR.ChangeDutyCycle(abs(rightSpeed))
self.pwmBR.ChangeDutyCycle(abs(rightSpeed))
self.pwmBL.ChangeDutyCycle(abs(leftSpeed))
#PWm can only accept positive values
if leftSpeed>0:
GPIO.output(self.In1FL, GPIO.LOW)
GPIO.output(self.In2FL, GPIO.HIGH)
GPIO.output(self.In3BL, GPIO.LOW)
GPIO.output(self.In4BL, GPIO.HIGH)
#left motor forward = In1_LOW & In3_LOW
# In2_HIGH & IN4_HIGH
else:
GPIO.output(self.In1FL, GPIO.HIGH)
GPIO.output(self.In2FL, GPIO.LOW)
GPIO.output(self.In3BL, GPIO.HIGH)
GPIO.output(self.In4BL, GPIO.LOW)
#left motor backwards = In1_HIGH & In3_HIGH
# In2_LOW & IN4_LOW
if rightSpeed>0:
GPIO.output(self.In3FR, GPIO.LOW)
GPIO.output(self.In4FR, GPIO.HIGH)
GPIO.output(self.In1BR, GPIO.LOW)
GPIO.output(self.In2BR, GPIO.HIGH)
#right motor forward = In1_LOW & In3_LOW
# In2_HIGH & IN4_HIGH
else:
GPIO.output(self.In3FR, GPIO.HIGH)
GPIO.output(self.In4FR, GPIO.LOW)
GPIO.output(self.In1BR, GPIO.HIGH)
GPIO.output(self.In2BR, GPIO.LOW)
#right motor backwards = In1_HIGH & In3_HIGH
# In2_LOW & IN4_LOW
print(f'Right speed is {rightSpeed}.')
print(f'Left speed is {leftSpeed}.')
sleep(time)
#-------------------------------------------------------------------------
def stop(self, time):
"""
Stops the motor
Parameters
----------
t : TYPE, optional
DESCRIPTION. Sets the stop time of the motor. The default is 0.
Returns
-------
None.
"""
self.pwmFL.ChangeDutyCycle(0)
self.pwmFR.ChangeDutyCycle(0)
self.pwmBR.ChangeDutyCycle(0)
self.pwmBL.ChangeDutyCycle(0)
sleep(time)
#----End of class--------------------------------------------------------------
def main():
"""
Test script if motor class is run in main
Returns
-------
None.
"""
motor1 = Motor(13,3,4,19,22,27,21,20,16,14,15,18)
motor1.stop(2)
motor1.move(0.4,-0.6,2)
GPIO.cleanup()
#-----------------------------------------------------------------------------
if __name__ == '__main__':
main()
However when I try to import the class into another module
from FourMotorClassWithTurn import Motor
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)#setup the pin out to be board numbers not BCM
GPIO.setwarnings(False)#stops set warnings from showing
motor1 = Motor(13,3,4,19,22,27,21,20,16,14,15,18)
motor1.stop(2)
motor1.move(0.4,-0.6,2)
GPIO.cleanup()
The motors will run once correctly but when I try to run it a second time I get a PWM fault
motor1 = Motor(13,3,4,19,22,27,21,20,16,14,15,18)
line 58, in init
self.pwmFL = GPIO.PWM(self.EnFL,100);
RuntimeError: A PWM object already exists for this GPIO channel
Anyone have an idea how to solve this?

Python Threading: Using 2 Light Dependant Resistors (LDR)

I am using a simple code from PiMyLife tutorial to read from a Light Dependant Resistor using a capacitor. This works great..
I then added a 2nd LDR and capacitor, added it to pin 31 and then duplicated the code (the rc_time function and the fuction call) to add an additional Light Dependant Resistor but for some reason the second function call never happens.
#!/usr/local/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
#define the pin that goes to the circuit
pin_ldr_one = 29
pin_ldr_two = 31
def rc_time_one (pin_ldr_one):
count_one = 0
#Output on the pin for
GPIO.setup(pin_ldr_one, GPIO.OUT)
GPIO.output(pin_ldr_one, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_ldr_one, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_ldr_one) == GPIO.LOW):
count_one += 1
return count_one
def rc_time_two (pin_ldr_two):
count_two = 0
#Output on the pin for
GPIO.setup(pin_ldr_two, GPIO.OUT)
GPIO.output(pin_ldr_two, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_ldr_two, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_ldr_two) == GPIO.LOW):
count_two += 1
return count_two
#Catch when script is interrupted, cleanup correctly
try:
# Main loop
while True:
print("LDR 1: ",rc_time_one(pin_ldr_one))
print("LDR 2: ",rc_time_two(pin_ldr_two))
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
So I thought I would try threading instead thinking that the 2 function calls were having issues.
So this is the threading code I came up with however this too fails to run the second (in this cask 2nd thread). Any ideas where I am going wrong. Thanks for all your help
#!/usr/local/bin/python
import RPi.GPIO as GPIO
import time
from time import sleep, perf_counter
from threading import Thread
GPIO.setmode(GPIO.BOARD)
#define the pin that goes to the circuit
pin_ldr_one = 29
pin_ldr_two = 31
def rc_time_one (pin_ldr_one = 29):
count_one = 0
#Output on the pin for
GPIO.setup(pin_ldr_one, GPIO.OUT)
GPIO.output(pin_ldr_one, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_ldr_one, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_ldr_one) == GPIO.LOW):
count_one += 1
print("LDR 1: ",count_one)
return count_one
def rc_time_two (pin_ldr_two = 31):
count_two = 0
#Output on the pin for
GPIO.setup(pin_ldr_two, GPIO.OUT)
GPIO.output(pin_ldr_two, GPIO.LOW)
time.sleep(0.1)
#Change the pin back to input
GPIO.setup(pin_ldr_two, GPIO.IN)
#Count until the pin goes high
while (GPIO.input(pin_ldr_two) == GPIO.LOW):
count_two += 1
print("LDR 2: ",count_two)
return count_two
#Catch when script is interrupted, cleanup correctly
try:
# Main loop
while True:
# create two new threads
t1 = Thread(target=rc_time_one)
t2 = Thread(target=rc_time_two)
# start the threads
t1.start()
t2.start()
t1.join()
t2.join()
#print("LDR 1: ",rc_time_one(pin_ldr_one))
#print("LDR 2: ",rc_time_two(pin_ldr_two))
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
Peter

Using If statement in python comparing time to a value

I am trying to write a simple code in python which working on the Raspberry pi.
This code is working but I want to make a better code when I turb on LED in different time.
such as 5minutes later turn on the first led and another 5minutes later second led ....
The problem is when I changing the dlay time such as 5minutes or 10minutes, I have to do hard coding and it seems not a good way to do.
I also tried to adding time to timeset but could not find out the solution.
import RPi.GPIO as GPIO
from datetime import datetime
from datetime import timedelta
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)
try:
while True:
timeset = datetime.now().strftime("%H:%M:%S")
print(timeset)
time.sleep(5)
if timeset > "19:11":
GPIO.output(24, True)
print('Please take a medicine')
if timeset > "19:12":
GPIO.output(6, True)
time.sleep(10)
GPIO.output(24, False)
GPIO.output(6, False)
time.sleep(0.30)
#if timeset =="00:23:30":
# timeset = datetime.now() + timedelta(minutes=1)
finally:
print("clean up")
GPIO.cleanup()`
https://discuss.tryton.org/t/typeerror-unsupported-operand-type-s-for-int-and-datetime-timedelta/4966
I have looked this and tried applying on my code but does not work.
You could go with a basic state machine.
Keep just sleep periods. No need anymore to deal with dates.
Example :
import time
i = 0
state = 0
while True:
time.sleep(5)
state = i%3
if state == 0:
print("light on led A")
elif state == 1:
print("light on led B")
elif state == 2:
print("light on led C")
i += 1
i increments forever
At each loop iteration thanks to the modulo function, state changes value in a cyclic way : 0, 1, 2, 0, 1, 2, ...
For each state value it's up to you to do whatever you decide with the RPi leds.

Is there an "interupt" command i should be using in my program

I would kindly ask for some support with my first program i am trying to write.
I have a raspberry pi 4 and am using the program Thonny and Python 3.9.2.
My program is trying to do the following.
There is a Microphone, and if it hears something it should turn on the yellow light. If it hears something 3 times in a set period of time it will turn on the red light.
The display runs though a script of explination and when the yellow or red light comes on it should switch to those expressions.
This is the problem I am having:
(to the best of my inspection) if the display is trying to change to the next line in the scrip and the yellow LED message is triggered the display gets "confused" and puts out nonsense.
I have tried to tell the display to only put out the discriptive script if the yellow and red light are off, but this didnt seem to really help.
The other observation i have is with the yellow and red LED. If the RED led triggers i want the counter reset on the yellow counter so that both yellow and red stay on for the same time, but the yellow light just stays on for the remainder of its time.sleep cycle.
I am a complete beginner and cant figure this out, is there an interupt command or something i could use here?
Here is the code for reference:
#V02 Trying to clean up the code and remove unessisary lines but working on fixing the strange characters that arrive and trying to get the system to reboot on its own.
import RPi.GPIO as GPIO #starts the IO functionality of the pi
import time # we need this for the sleep function
import threading #threading so we can get more than one thing to run at the same time
from time import sleep #sleep function
GPIO.setmode(GPIO.BCM) # setting for the IO
GPIO.setwarnings(False) #Suppress warnings
GPIO.setup(12,GPIO.OUT) #green
GPIO.setup(16,GPIO.OUT) #yellow
GPIO.setup(20,GPIO.OUT) #red
GPIO.setup(21,GPIO.IN) #NOISE SENSOR
# GPIO to LCD mapping
LCD_RS = 4 # BCM GPIO 4
LCD_E = 17 # BCM GPIO 17
LCD_D4 = 6 # BCM GPIO 6
LCD_D5 = 13 # BCM GPIO 13
LCD_D6 = 19 # BCM GPIO 19
LCD_D7 = 26 # BCM GPIO 26
# Device constants
LCD_CHR = True # Character mode
LCD_CMD = False # Command mode
LCD_CHARS = 16 # Characters per line (16 max)
LCD_LINE_1 = 0x80 # LCD memory location for 1st line
LCD_LINE_2 = 0xC0 # LCD memory location 2nd line
GPIO.output(16,GPIO.LOW) #set all pins to LOW Start postion for program
GPIO.output(20,GPIO.LOW) #set all pins to LOW Start postion for program
GPIO.output(12,GPIO.LOW) #set all pins to LOW Start postion for program
def Green(): #This is the status LED for the Green LED
while True:
GPIO.output(12,GPIO.HIGH)
time.sleep(2.5)
GPIO.output(12,GPIO.LOW)
time.sleep(.5)
def Yellow():
while True:
if GPIO.input(21) == 1: #this is the microphone sending a detection signal
print("Sound Detected")
GPIO.output(16,GPIO.HIGH) #turn on the yellow LED
lcd_text("I heard that",LCD_LINE_1)
lcd_text("Relax",LCD_LINE_2)
time.sleep(10)
GPIO.output(16,GPIO.LOW) #turn off the yellow LED
x = 0
def Red():
x = 0
while True:
if GPIO.input(16) == 1 and GPIO.input(21) == 1: #if Yellow light and Microphone activate at the same time we start counter for RED LED
x += 1
print(x)
time.sleep(.45)
if x > 2:
print("Too noisy in here")
GPIO.output(16,GPIO.HIGH)#if the x counter (sound counter) reaches greater than 3 we set the yellow and red LED on for 5 seconds
GPIO.output(20,GPIO.HIGH)
lcd_text("Schnauze",LCD_LINE_1)
lcd_text("sonst Beule",LCD_LINE_2)
time.sleep(10)
GPIO.output(20,GPIO.LOW)
GPIO.output(16,GPIO.LOW)
x = 0
if GPIO.input(16) == 0: #if the yellow light goes out before we reach x > 2 we set x back to 0
x = 0
t1 = threading.Thread(target=Green)
t2 = threading.Thread(target=Yellow)
t3 = threading.Thread(target=Red)
t1.start()
t2.start()
t3.start()
# Define main program code
def main():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setup(LCD_E, GPIO.OUT) # Set GPIO's to output mode
GPIO.setup(LCD_RS, GPIO.OUT)
GPIO.setup(LCD_D4, GPIO.OUT)
GPIO.setup(LCD_D5, GPIO.OUT)
GPIO.setup(LCD_D6, GPIO.OUT)
GPIO.setup(LCD_D7, GPIO.OUT)
# Initialize display
lcd_init()
# Loop - send text and sleep 3 seconds between texts
while True:
if GPIO.input(16) == 0 and GPIO.input(20) == 0:
lcd_text("Welcome to",LCD_LINE_1)
lcd_text("P4DQ",LCD_LINE_2)
time.sleep(5) # 3 second delay
lcd_init() # start the LCD init program at the end of the loop to clear out strange characters on the display
if GPIO.input(16) == 0 and GPIO.input(20) == 0:
lcd_text("I detect loud",LCD_LINE_1)
lcd_text("noise",LCD_LINE_2)
time.sleep(5) # 3 second delay
lcd_init() # start the LCD init program at the end of the loop to clear out strange characters on the display
if GPIO.input(16) == 0 and GPIO.input(20) == 0:
lcd_text("Green LED",LCD_LINE_1)
lcd_text("I'm listening",LCD_LINE_2)
time.sleep(5) # 3 second delay
lcd_init() # start the LCD init program at the end of the loop to clear out strange characters on the display
if GPIO.input(16) == 0 and GPIO.input(20) == 0:
lcd_text("Yellow LED",LCD_LINE_1)
lcd_text("I hear something",LCD_LINE_2)
time.sleep(5)
lcd_init() # start the LCD init program at the end of the loop to clear out strange characters on the display
if GPIO.input(16) == 0 and GPIO.input(20) == 0:
lcd_text("Red LED",LCD_LINE_1)
lcd_text("It's TOO LOUD :(",LCD_LINE_2)
time.sleep(5)
lcd_init() # start the LCD init program at the end of the loop to clear out strange characters on the display
if GPIO.input(16) == 0 and GPIO.input(20) == 0:
lcd_init() # start the LCD init program at the end of the loop to clear out strange characters on the display
# End of main program code
# Initialize and clear display
def lcd_init():
lcd_write(0x33,LCD_CMD) # Initialize
lcd_write(0x32,LCD_CMD) # Set to 4-bit mode
lcd_write(0x06,LCD_CMD) # Cursor move direction
lcd_write(0x0C,LCD_CMD) # Turn cursor off
lcd_write(0x28,LCD_CMD) # 2 line display
lcd_write(0x01,LCD_CMD) # Clear display
time.sleep(0.0005) # Delay to allow commands to process
def lcd_write(bits, mode):
# High bits
GPIO.output(LCD_RS, mode) # RS
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x10==0x10:
GPIO.output(LCD_D4, True)
if bits&0x20==0x20:
GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D6, True)
if bits&0x80==0x80:
GPIO.output(LCD_D7, True)
# Toggle 'Enable' pin
lcd_toggle_enable()
# Low bits
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x01==0x01:
GPIO.output(LCD_D4, True)
if bits&0x02==0x02:
GPIO.output(LCD_D5, True)
if bits&0x04==0x04:
GPIO.output(LCD_D6, True)
if bits&0x08==0x08:
GPIO.output(LCD_D7, True)
# Toggle 'Enable' pin
lcd_toggle_enable()
def lcd_toggle_enable():
time.sleep(0.0005)
GPIO.output(LCD_E, True)
time.sleep(0.0005)
GPIO.output(LCD_E, False)
time.sleep(0.0005)
def lcd_text(message,line):
# Send text to display
message = message.ljust(LCD_CHARS," ")
lcd_write(line, LCD_CMD)
for i in range(LCD_CHARS):
lcd_write(ord(message[i]),LCD_CHR)
#Begin program
try:
main()
finally:
lcd_write(0x01, LCD_CMD)
GPIO.cleanup()

RaspberryPi webiopi script error doesn't work

I want to experiment with the RaspberryPi, but the .py code doesn't work. It gives me a lot of errors.
a space error
indentation error: expected an indented block " global VELUX_STATE, AUTO_mode "
I only copied the code from the official forum, that works for other people.
Link:
http://egrasland.blogspot.fr/2014/01/control-your-velux-roller-shutter-with.html
I copy the code and paste in "sudo nano script.py"
What do I do wrong ?
Furthermore, the code from the official webiopi from RaspberryPi doesn't work either for me. Debug gives error, and the program doesn't start.
Link: https://code.google.com/p/webiopi/wiki/Tutorial_Basis
Thanks for the answers.
The code I used is:
Python server script :
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()
The debug can't I post here because its on my RaspberryPi and the browser is to slow :)
But the first error he give is the dubble spaces by the comment VELUX UP
"HOUR_UP = 9 # Turn Velux UP at"
I solved this but then is it the error
"def setup():
global VELUX_STATE, AUTO_MODE"
Why?
Thanks!

Categories