In addition to temperature and humidity sensors, there is an illuminance sensor as an easy-to-use sensor. The unit can be measured in lux. This time, we have prepared an Adafruit TSL2561 digital illuminance sensor. There is also a motion sensor that uses infrared rays as an optical sensor. It seems that you can do something like IoT when used in combination.
The breadboard wiring is for the Arduino version on Adafruit's lern site, but can be found at TSL2561 Luminosity Sensor.
If the wiring is successful, you can check the 0x39 address.
$ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- 39 -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Python 2
The driver for retrieving data from the I2C bus is Python's [Adafruit_I2C.py] in the Adafruit-Raspberry-Pi-Python-Code repository. ](Https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_I2C/Adafruit_I2C.py) is used. This driver works with Python 2, which is installed by default on the Raspberry Pi.
$ cd ~/python_apps
$ git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code
$ cd Adafruit-Raspberry-Pi-Python-Code/Adafruit_I2C
Python 3
Quick2Wire is also used by RPi-Light-Sensor etc. I will. I haven't installed Python 3 on my Raspberry Pi, so I'll prepare a virtualenv and use it next time.
The program to measure the illuminance from TSL2561 is [TSL2561.py](https://github. com / seanbechhofer / raspberrypi / blob / master / python / TSL2561.py) is introduced.
Also, in Raspberry Pi Hacks, tsl2561-lux.py The method using -lux.py) is posted. Both are almost the same code, but this time I will download tsl2561-lux.py
from the rpihacks repository to the ʻAdafruit_I2C` directory and use it.
$ cd ./Adafruit_I2C
$ wget https://raw.githubusercontent.com/spotrh/rpihacks/master/tsl2561-lux.py
$ chmod +x tsl2561-lux.py
Even if you comment out the end of tsl2561-lux.py
, it can be called from the outside as a module.
~/python_apps/Adafruit-Raspberry-Pi-Python-Code/Adafruit_I2C/tsl2561-lux.py
...
#oLuxmeter=Luxmeter()
#print "LUX HIGH GAIN ", oLuxmeter.getLux(16)
#print "LUX LOW GAIN ", oLuxmeter.getLux(1)
#print "LUX AUTO GAIN ", oLuxmeter.getLux()
Unfortunately, the file name contains a hyphen, so Python cannot import it as it is. Replace it with an underscore to create a symbolic link.
$ ln -s tsl2561-lux.py tsl2561_lux.py
Create tsl2561.py
as an entry point to measure the illuminance using the Luxmeter class.
~/python_apps/Adafruit-Raspberry-Pi-Python-Code/Adafruit_I2C/tsl2561.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from time import sleep
from tsl2561_lux import Luxmeter
if __name__ == "__main__":
while True:
tsl=Luxmeter()
sensor_value = tsl.getLux()
print(sensor_value)
sleep(5)
Run the program that measures the illuminance at 5-second intervals. The unit is lux. The data has some variations, probably because of the instantaneous measurement.
$ chmod u+x tsl2561.py
$ ./tsl2561.py
1691.71324345
961.73944068
1274.95548759
690.479479419
14.30208
0
11.50656
388.866054431
1316.01531121
0
0
Illuminate the sensor with the iPhone flashlight app to increase lux. Next time, I will try to acquire stable data by measuring in a pitch-black room.
19088.793866
0
0
UPDATE 2015-05-25
I modified the program to publish to MQTT when it exceeds 500 lux. The measurement interval is also shortened to 3 seconds.
~/meshblu/publish.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from time import sleep
from tsl2561_lux import Luxmeter
import paho.mqtt.client as mqtt
import json
def sensor():
sensor = W1ThermSensor()
celsius = sensor.get_temperature()
return celsius
def on_connect(client, userdata, rc):
print("Connected with result code " + str(rc))
def on_publish(client, userdata, mid):
print("publish: " + str(mid))
def main():
client = mqtt.Client(client_id='',clean_session=True,
protocol=mqtt.MQTTv311)
client.username_pw_set("{username}","{password}")
client.connect("{mqtt_broker_host}", 1883, 60)
client.on_connect = on_connect
client.on_publish = on_publish
tsl=Luxmeter()
while True:
sensor_value = tsl.getLux()
print(sensor_value)
if sensor_value > 500.0:
message = json.dumps(dict(trigger="on"))
client.publish("data",message)
sleep(3)
if __name__ == '__main__':
main()
I moved to a dark room and tested it. Accurate measurement is possible when the illuminance is stable and dark. It seems to be 0 if the illuminance changes too much. I slowly brought the iPhone light closer and published it twice.
$ python ./publish.py
1.42263737647
1.42263737647
1.42263737647
1.42263737647
1.42263737647
1.42263737647
1.42263737647
781.750737506
publish: 1
1212.30526902
publish: 2
2.45526898227
1.48479374557
119.459947915
287.1274
45.49168
0
0
1.45533948132
1.41892978382
1.41892978382
Recommended Posts