1

我通过 1uF 电容器将光敏电阻连接到我的 Raspberry PI,并运行简单的程序来检查值。它主要是我拥有的其他程序的合并脚本,所以它可能是错误的。我是这方面的新手。我设置了2个变量。如果光敏电阻的值低于 1000 则为 True,否则为 False。我不想控制我的 LED 的槽 JSON 命令到 Openhab 服务器。当光敏电阻为 True 时,它​​向 Openhab 发送命令“ON”,否则它发送命令“OFF”。一切都很好,除了一件事。使用每个测量光敏电阻值的脚本向 Openhab 发送命令。我希望它仅在检测到低于 1000 的值(真)时才第一次发送命令“ON”,然后留在那里,当光敏电阻输出高于 1000(假)时,不向 Openhab 发送命令,以此类推。这里的主要目标是在主照明打开时更改 LED 的颜色,并在主照明关闭时将其更改回来。我希望我解释得很好。请帮忙。

我目前的程序:

#!/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()
4

1 回答 1

0

好的。我以某种方式想通了。如果有人需要这样的解决方案,这里有一个正在运行的程序。这不是专业工艺,所以它可能有问题,仍在测试中。我没有在其他地方找到类似的东西,所以这总比没有好。

简而言之:当主灯关闭时,如果光敏电阻检测到主灯变为 ON,则脚本将当前颜色和亮度状态保存到文件中,然后通过 REST API 使用 Openhab 脚本/规则将颜色更改为预定义。然后,如果光敏电阻检测到主照明关闭,脚本会读取之前保存的文件内容并恢复 LED 之前的颜色和亮度。该脚本处理光敏电阻和发送命令,但大部分操作发生在 Openhab 上。

如果有人有一些建议,我会很高兴来到这里。

  #!/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
于 2017-03-23T20:05:24.750 回答