Page 1 of 1

Python script to read photoresistor stats - help needed

Posted: Sunday 10 April 2016 22:56
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

Re: Python script to read photoresistor stats - help needed

Posted: Monday 11 April 2016 12:47
by Toulon7559
What is the circuit connected to the GPIO?

Re: Python script to read photoresistor stats - help needed

Posted: Monday 11 April 2016 13:35
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)

Re: Python script to read photoresistor stats - help needed

Posted: Monday 11 April 2016 17:21
by lukcinek
Anybody?

Re: Python script to read photoresistor stats - help needed

Posted: Wednesday 13 April 2016 19:23
by lukcinek
:(

Re: Python script to read photoresistor stats - help needed

Posted: Saturday 14 May 2016 14:09
by Toulon7559
Perhaps you can borrow some ideas from the Python-script in this message: http://www.domoticz.com/forum/viewtopic ... 808#p72227

Re: Python script to read photoresistor stats - help needed

Posted: Saturday 14 May 2016 15:16
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

Re: Python script to read photoresistor stats - help needed

Posted: Sunday 15 May 2016 15:29
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

Re: Python script to read photoresistor stats - help needed

Posted: Monday 16 May 2016 18:48
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) )