for my project I want a python code to control the light.
So if a input from my PIR sensor turn the light on, but check the light level and then turn the light on or not.
I have a basic script for the PIR sensor light control without any light sensor addon:
Code: Select all
import RPi.GPIO as GPIO
import time
import smbus
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(25, GPIO.OUT)
GPIO.output(25, GPIO.HIGH)
delay = 40 # set number of seconds delay before light turns off
while True:
time.sleep(0.1)
#wait for pir to trigger.
print "waiting "
while GPIO.input(12) == 0:
time.sleep (0.5)
print "turn light on here"
GPIO.output(25, GPIO.LOW)
time.sleep(0.5)
GPIO.output(25, GPIO.HIGH)
count = 0
#start count down to turn off
print "count down started "
while count < delay:
count = count + 1
# here if the input goes high again we reset the counter to 0
if GPIO.input(12) == 1:
count = 0
print "count down now ", (delay - count)
time.sleep(1)
print "Turn light off"
GPIO.output(25, GPIO.LOW)
time.sleep(0.5)
GPIO.output(25, GPIO.HIGH)
Code: Select all
import smbus
def convertToNumber(data):
# Simple function to convert 2 bytes of data
# into a decimal number
return ((data[1] + (256 * data[0])) / 1.2)
def readLight(addr=0x23):
data = smbus.SMBus(1).read_i2c_block_data(0x23,0x11)
return convertToNumber(data)Code: Select all
print "" + str(int(readLight())) + """else:...." they don't switch with the tasks from " if .. > 100" to "else:...".
For Example:
Code: Select all
import smbus
def convertToNumber(data):
# Simple function to convert 2 bytes of data
# into a decimal number
return ((data[1] + (256 * data[0])) / 1.2)
def readLight(addr=0x23):
data = smbus.SMBus(1).read_i2c_block_data(0x23,0x11)
return convertToNumber(data)
while True:
if " + str(int(readLight())) + " > 100:
print "mode 1"
time.sleep(1)
else:
print "mode 2"
time.sleep(1) At the end I want one code with the base on the first PIR Control code with the integration of the light sensor.
I hope you unterstand my problem
Thanks!!