I have Photoresistor connected to my Raspberry PI through 1uF capacitor, and running simple program to check values. It's mostly merged scripts from other programms i have, so it's might by buggy. I'm new in this stuff. I set 2 variables. If value of photoresistor is below 1000 then it's True, otherwise it's False. I Wan't to controll my LED's trough JSON command to Openhab server. When photoresistor gives True, it's sending command "ON" to Openhab, otherwise it's sending command "OFF". Everything's fine, except one thing. Script sending commands to Openhab with every measure of photoresistor value. I want it to send command "ON" only first time when value below 1000 is detected (True) then stay there, not sending commands to Openhab to moment when photoresistor give output above 1000 (False) and so on in other way. Main goal here is to Change LED's color when main lighting is ON, and change it back when main lighting is OFF. I hope i explained it good. Please help.
My current program:
#!/usr/local/bin/python
import RPi.GPIO as GPIO, time
import urllib
import urllib2
import requests
GPIO.setmode(GPIO.BCM)
def RCtime (PiPin):
measurement = 0
# Discharge capacitor
GPIO.setup(PiPin, GPIO.OUT)
GPIO.output(PiPin, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(PiPin, GPIO.IN)
# Count loops until voltage across
# capacitor reads high on GPIO
while (GPIO.input(PiPin) == GPIO.LOW):
measurement += 1
return measurement
def LIGHTcheck():
if RCtime(27)<1000:
LIGHT = True
print LIGHT
return LIGHT
if RCtime(27)>1000:
LIGHT = False
print LIGHT
return LIGHT
def LightON():
url = 'http://openhab-server:8080/CMD?switch2=ON'
postdata = {"ON"}
print(postdata)
resp = requests.get(url=url)
def LightOFF():
url = 'http://openhab-server:8080/CMD?switch2=OFF'
postdata = {"OFF"}
print(postdata)
resp = requests.get(url=url)
while True:
if LIGHTcheck() == True:
LightON()
elif LIGHTcheck() == False:
LightOFF()