Python script to read photoresistor stats - help needed

Moderator: leecollings

Post Reply
lukcinek
Posts: 29
Joined: Saturday 19 March 2016 23:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Python script to read photoresistor stats - help needed

Post by lukcinek »

Hi

I don't now Python and programming...
I try to read photoresistor status with this script with success but I want to send my result with JSON to Domoticz virtual sensor (IDX - 35).
Script reads photoresistor value but doesn't send this value to DOMOTICZ. I don't know what I have to add to this code to make it works. (I think I must add VALUE variable but I doesn't know how:(

The code:

#!/usr/bin/env python

# Example for RC timing reading for Raspberry Pi
# Must be used with GPIO 0.3.1a or later - earlier verions
# are not fast enough!

import RPi.GPIO as GPIO, time, os
import urllib
DEBUG = 1
GPIO.setmode(GPIO.BCM)
import urllib2

def RCtime (RCpin):
reading = 0
GPIO.setup(RCpin, GPIO.OUT)
GPIO.output(RCpin, GPIO.LOW)
time.sleep(0.1)

GPIO.setup(RCpin, GPIO.IN)
# This takes about 1 millisecond per loop cycle
while (GPIO.input(RCpin) == GPIO.LOW):
reading += 1
return reading

print RCtime(18) # Read RC timing using pin #18
httpresponse = urllib.urlopen ("http://192.168.0.16:8081/json.htm?type= ... alue=VALUE")

END of code

Best regards
Toulon7559
Posts: 849
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: Python script to read photoresistor stats - help needed

Post by Toulon7559 »

What is the circuit connected to the GPIO?
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
lukcinek
Posts: 29
Joined: Saturday 19 March 2016 23:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python script to read photoresistor stats - help needed

Post by lukcinek »

Hi

I connected my photoresistor using this manual: https://learn.adafruit.com/basic-resist ... ll-reading. I dont't have Arduino. I do that on raspberry PI.
I modified this script to send one reading (not continuous).
Everything works fine - I see some numbers but I don't know how to send this numbers to domoticz (In Domoticz I have virtal sensor - IDX 35)
lukcinek
Posts: 29
Joined: Saturday 19 March 2016 23:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python script to read photoresistor stats - help needed

Post by lukcinek »

Anybody?
lukcinek
Posts: 29
Joined: Saturday 19 March 2016 23:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python script to read photoresistor stats - help needed

Post by lukcinek »

:(
Toulon7559
Posts: 849
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: Python script to read photoresistor stats - help needed

Post by Toulon7559 »

Perhaps you can borrow some ideas from the Python-script in this message: http://www.domoticz.com/forum/viewtopic ... 808#p72227
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
lukcinek
Posts: 29
Joined: Saturday 19 March 2016 23:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python script to read photoresistor stats - help needed

Post by lukcinek »

Thank You very much
It worked!
I try this code:
httpresponse = urllib.urlopen("http://127.0.0.1:8080/json.htm?type=com ... evice&idx=" + str(domoticz_V) + "&nvalue=0&svalue=" + str(float(VOLTAGE)) )
and now everything works :)

Most important was this section:
I modified:
svalue=" + str(float(VOLTAGE)) )
to:
svalue=" + str(VALUE)) )

Best regards
lukcinek
Posts: 29
Joined: Saturday 19 March 2016 23:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python script to read photoresistor stats - help needed

Post by lukcinek »

Everything works but maybe somoeone have an idea how to change this script to show high Lux values when there is more sun.
Script I use show high lux values when is dark and low values when is bright :)
I want the opposite
lukcinek
Posts: 29
Joined: Saturday 19 March 2016 23:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python script to read photoresistor stats - help needed

Post by lukcinek »

I resolved problem of wrong values. This solution isn't perfect but...

My code:


import RPi.GPIO as GPIO
import datetime
import socket
import time
import os
import urllib

DEBUG = 1
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

def RPlight (RPpin):
reading = 0
GPIO.setup(RPpin, GPIO.OUT)
GPIO.output(RPpin, GPIO.LOW)
time.sleep(0.1)

GPIO.setup(RPpin, GPIO.IN)
# This takes about 1 millisecond per loop cycle
while (GPIO.input(RPpin) == GPIO.LOW):
reading += 1
return reading

light = RPlight(18)
light = 10000 - light

VALUE = light

if light < 0:
print 0
httpresponse = urllib.urlopen("http://192.168.0.15:8080/json.htm?type= ... 3&svalue=0")
else:
print light
httpresponse = urllib.urlopen("http://192.168.0.15:8080/json.htm?type= ... 53&svalue=" + str(VALUE) )
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest