1

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()
Morgoth
  • 4,935
  • 8
  • 40
  • 66
arasadasos
  • 11
  • 3

1 Answers1

0

Ok. I figured it out in some way. If anyone ever need solution like this, here's a running programm. It's not professional craft, so it's probably buggy, still testing. I did not found something like that elsewhere, so better this than nothing.

IN SHORT: When Main light is off, if photoresistor detects that Main lighting changed to ON, then script saves current color and brightness states to files, then changing color to predefinied with Openhab script/rules through REST API. Then if photoresistor detects that main lighting is off, script reads content of files saved before and restores previous color and brightness of LED's. That script handling photoresistor and sending commands but most of action happening on Openhab.

If anyone have some sugestions, i would be pleased to here them.

  #!/usr/local/bin/python
import RPi.GPIO as GPIO, time
from subprocess import call
import smbus
import time
import urllib
import urllib2
import json
import requests
from ctypes import c_short



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)

  while (GPIO.input(PiPin) == GPIO.LOW):
    measurement += 1

  return measurement

def LIGHTcheck():   # Checking photoresistor Values, and defining Variables as True/False:
    if RCtime(27)<150:
        LIGHT = True
        return LIGHT

    if RCtime(27)>150:
        LIGHT = False 
        return LIGHT

def LIGHTstatusSaveColor():   # Saving Color State to file:
    urllib.urlretrieve('http://openhab-server:8080/rest/items/Light_scene/state', 'color.log')

def LIGHTstatusSaveDimmer():   # Saving Brightness State to file:
    urllib.urlretrieve('http://openhab-server:8080/rest/items/Brightness_switch/state', 'dimmer.log')   

def LightON():
        url = 'http://openhab-server:8080/CMD?Light_scene=43' # Openhab Command to switch color to predefined, in my case "Light_scene" with predefined responses trough Openhab scripts/rules:
        resp = requests.get(url=url)        


def LightOFFcolor(): # Reading state of color from previously saved file and sending command to Openhab to change color to Value from before Turning Main Lighting on:
        f = open("color.log", "r")
        content1 = f.read()
        # print(content1)
        url = 'http://openhab-server:8080/CMD?'
        postdata = {"Light_scene":content1}
        # print(postdata)
        resp = requests.get(url=url, params=postdata)   
        f.close()

def LightOFFdimmer(): # Reading state of brightness from previously saved file and sending command to Openhab to change brightness to Value from before Turning Main Lighting on:
        f = open("dimmer.log", "r")
        content2 = f.read()
        # print(content2)
        url = 'http://openhab-server:8080/CMD?'
        postdata = {"Brightness_switch":content2}
        # print(postdata)
        resp = requests.get(url=url, params=postdata)           
        f.close()


waiting_for_high_value = True

while True: # Loop Waiting for value change, then executing suitable Function:
    if waiting_for_high_value:
        if LIGHTcheck() == True:
            print LIGHTcheck()  
            LIGHTstatusSaveColor() 
            LIGHTstatusSaveDimmer()             
            LightON()
            waiting_for_high_value = False
    else:
        if LIGHTcheck() == False:
            print LIGHTcheck()  
            LightOFFcolor()
            time.sleep(2.0)
            LightOFFdimmer()
            waiting_for_high_value = True
arasadasos
  • 11
  • 3