2017-03-21 9 views
1

1W 커패시터를 통해 내 Raspberry PI에 Photoresistor를 연결하고 간단한 프로그램을 실행하여 값을 확인했습니다. 주로 가지고있는 다른 프로그램의 스크립트를 병합했기 때문에 버그가있을 수 있습니다. 나는이 물건에 새로운 사람이야. 나는 2 개의 변수를 설정했다. 포토 레지스터의 값이 1000 미만이면 True, 그렇지 않으면 False입니다. 내 LED의 물마루 JSON 명령을 Openhab 서버로 제어 할 필요가 없습니다. Photoresistor가 True가되면 Openhab 명령에 "ON"명령을 보내고 그렇지 않으면 명령을 "OFF"로 보냅니다. 한 가지는 제외하고 모두 괜찮습니다. Photoresistor 가치의 모든 측정과 Openhab에 명령을 보내는 스크립트. 1000 이하의 값이 감지되면 (True) 명령을 "ON"으로 보내고, 포토 레지스터가 1000 이상 (False) 이상의 출력을주는 순간 Openhab에 명령을 보내지 말고 다른 방법으로 명령을 보내길 원합니다. 주된 목적은 메인 조명이 켜졌을 때 LED의 색상을 변경하고, 메인 조명이 꺼져있을 때 다시 변경하는 것입니다. 나는 그것을 설명하기를 바란다. 도와주세요.광 센서 값에 따라 Openhab에 명령 보내기 Python Raspberry Pi

나의 현재 프로그램 :

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

답변

0

좋아. 나는 그것을 어떤 식 으로든 알아 냈다. 누구나 이와 같은 해결책이 필요한 경우 여기에 실행중인 프로그램이 있습니다. 그것은 전문 공예품이 아니므로, 여전히 테스트 중이며 아마도 버그 일 것입니다. 나는 다른 곳에서 그런 것을 발견하지 못했다.

: 포토 레지스터는 메인 조명이 ON으로 변경을 감지하면 홈페이지 표시등이 꺼져있는 경우, 다음 스크립트는 Openhab 스크립트/REST API를 통해 규칙 개의 정의하는 색상을 변경, 파일을 현재의 색상과 밝기 상태를 저장합니다. 그런 다음 포토 레지스터가 주 조명이 꺼져 있음을 감지하면 스크립트는 이전에 저장 한 파일 내용을 읽고 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