Page 1 of 1

Script send value in a dummy captor : new problem...

Posted: Wednesday 06 March 2019 22:04
by samourai47
Hello everybody and sorry for my poor english...
I am on raspberry and i found this pyton script here (-> https://raspberrytips.nl/tsl2561-licht- ... pberry-pi/) for testing my TSL2561. It work's fine.... and i obtain in cmd 3 values

Code: Select all

# TSL2561
import smbus
import time

# Get I2C bus
bus = smbus.SMBus(1)

# TSL2561 address, 0x39(57)
# Select control register, 0x00(00) with command register, 0x80(128)
#		0x03(03)	Power ON mode
bus.write_byte_data(0x39, 0x00 | 0x80, 0x03)
# TSL2561 address, 0x39(57)
# Select timing register, 0x01(01) with command register, 0x80(128)
#		0x02(02)	Nominal integration time = 402ms
bus.write_byte_data(0x39, 0x01 | 0x80, 0x02)

time.sleep(0.5)

# Read data back from 0x0C(12) with command register, 0x80(128), 2 bytes
# ch0 LSB, ch0 MSB
data = bus.read_i2c_block_data(0x39, 0x0C | 0x80, 2)

# Read data back from 0x0E(14) with command register, 0x80(128), 2 bytes
# ch1 LSB, ch1 MSB
data1 = bus.read_i2c_block_data(0x39, 0x0E | 0x80, 2)

# Convert the data
ch0 = data[1] * 256 + data[0]
ch1 = data1[1] * 256 + data1[0]

# Output data to screen
print "Full Spectrum(IR + Visible) :%d lux" %ch0
print "Infrared Value :%d lux" %ch1
print "Visible Value :%d lux" %(ch0 - ch1)
Can i have some help to "modify" the script? I want to use in a dummy virtual captor and i don't know how to make that.... I wan't the "Full Spectrum(IR + Visible) :%d lux" %ch0, go in a txt dummy captor.

Thank's for your help

Re: Script send value in a dummy captor

Posted: Friday 08 March 2019 14:24
by samourai47
Well, no solution at this time.
I have added

Code: Select all

import urllib
and

Code: Select all

httpresponse = urllib.urlopen('http://192.168.0.120:8080/json.htm?type=command&param=udevice&idx=44&nvalue=0&svalue="str(ch0)"')
It's only txt ; "str(ch0)" who is send to the the dummy captor, not the value ch0
I dont know how to recover this value

Re: Script send value in a dummy captor

Posted: Friday 08 March 2019 15:00
by jvdz
samourai47 wrote: Friday 08 March 2019 14:24

Code: Select all

httpresponse = urllib.urlopen('http://192.168.0.120:8080/json.htm?type=command&param=udevice&idx=44&nvalue=0&svalue="str(ch0)"')
You need to concatenate the value... something like this?:

Code: Select all

httpresponse = urllib.urlopen('http://192.168.0.120:8080/json.htm?type=command&param=udevice&idx=44&nvalue=0&svalue="'+str(ch0)+'"')
Jos

Re: Script send value in a dummy captor

Posted: Friday 08 March 2019 16:04
by samourai47
Yes!!!!
Thank's for your help

Next work is to create a cron tab for running this script every minutes.....

I am too happy...

Re: Script send value in a dummy captor

Posted: Monday 11 March 2019 11:00
by samourai47
Hello
Here the completed script that works on my raspberry p2. Thak's for your help
I have 2 dummy sensor
One is "Lux" (idx=45)
The other is Txt (idx=44) and receive the 3 data of the tsl2561

Code: Select all

# TSL2561
import smbus
import time
import urllib

# Get I2C bus
bus = smbus.SMBus(1)

# TSL2561 address, 0x39(57)
# Select control register, 0x00(00) with command register, 0x80(128)
#		0x03(03)	Power ON mode
bus.write_byte_data(0x39, 0x00 | 0x80, 0x03)
# TSL2561 address, 0x39(57)
# Select timing register, 0x01(01) with command register, 0x80(128)
#		0x02(02)	Nominal integration time = 402ms
bus.write_byte_data(0x39, 0x01 | 0x80, 0x02)

time.sleep(0.5)

# Read data back from 0x0C(12) with command register, 0x80(128), 2 bytes
# ch0 LSB, ch0 MSB
data = bus.read_i2c_block_data(0x39, 0x0C | 0x80, 2)

# Read data back from 0x0E(14) with command register, 0x80(128), 2 bytes
# ch1 LSB, ch1 MSB
data1 = bus.read_i2c_block_data(0x39, 0x0E | 0x80, 2)

# Convert the data
ch0 = data[1] * 256 + data[0]
ch1 = data1[1] * 256 + data1[0]


# Output data to screen
# print "Full Spectrum(IR + Visible) :%d lux" %ch0
# print "Infrared Value :%d lux" %ch1
# print "Visible Value :%d lux" %(ch0 - ch1)

httpresponse = urllib.urlopen('http://192.168.0.120:8080/json.htm?type=command&param=udevice&idx=45&nvalue=0&svalue='+str(ch0)+'')
httpresponse = urllib.urlopen('http://192.168.0.120:8080/json.htm?type=command&param=udevice&idx=44&nvalue=0&svalue=Full Value: '+str(ch0)+'; Visible Value: '+str(ch0 - ch1)+'; Infrared Value: '+str(ch1)+'')


Output data to screen is desactivated. I need it only for the test
I create a crontab that send the data every minutes
Thank you for your help

Re: Script send value in a dummy captor

Posted: Sunday 01 September 2019 15:20
by samourai47
Hello!!
Unfornutally i must rienstall raspian. I take the last buster full version. And the last domoticz version
I have a probleme with the script, who works perfectly until the software problems

When in run the script in command line the values are correctly print to the screen console, but for the domoticz i have this messages....

Can someone help me to understand and modifie the script?
Thank's

Full Spectrum(IR + Visible) :7813 lux
Infrared Value :4702 lux
Visible Value :3111 lux
Traceback (most recent call last):
File "tsl2561.py", line 38, in <module>
httpresponse = urllib.urlopen('http://192.168.0.120:8080/json.htm?type ... '+str(ch0)+'')
File "/usr/lib/python2.7/urllib.py", line 87, in urlopen
return opener.open(url)
File "/usr/lib/python2.7/urllib.py", line 216, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 353, in open_http
h.endheaders(data)
File "/usr/lib/python2.7/httplib.py", line 1038, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 882, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 844, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 821, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 575, in create_connection
raise err
IOError: [Errno socket error] [Errno 111] Connection refused

Re: Script send value in a dummy captor : new problem...

Posted: Sunday 01 September 2019 16:48
by jvdz
Last error line indicates that you do not have the access to the site.
Is 192.168.0.120 still the correct IP address?
Is this the Domoticz server and have you allowed the 192.168.0.* subnet access without password?

Jos

Re: Script send value in a dummy captor : new problem...

Posted: Sunday 01 September 2019 18:24
by samourai47
I found the error. i install domoticz only with ssl port. And in the domoticz.sh the standard port was at 0. i modify to 8080

And i dont know if it possible to configure the line with https

httpresponse = urllib.urlopen('http://192.168.0.120:8080/json.htm?typ............

in
httpresponse = urllib.urlopen('https://192.168.0.120:443/json.htm?typ

Thank you