-
Notifications
You must be signed in to change notification settings - Fork 2
/
tsl2561-mqtt.py
executable file
·55 lines (48 loc) · 1.51 KB
/
tsl2561-mqtt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python3
# Import required Python libraries
import paho.mqtt.client as mqtt
import board
import adafruit_tsl2561
import time
import ssl
# set the variables
broker='FQDN / IP Adresse'
port=8883
publish_topic="house/pi-tsl2561"
clientid='python-mqtt-tsl2561'
username='mosquitto'
password='password'
insecure=True
qos=1
retain_message=True
# do the stuff
# define BH1750
i2c = board.I2C()
sensor = adafruit_tsl2561.TSL2561(i2c)
# Set high gain mode.
# 0 is low gain 1 is high gain
sensor.gain = 0
# Set integration time.
# A value 0 is 13.7ms, 1 is 101ms, 2 is 402ms, and 3 is manual mode.
sensor.integration_time = 0
#MQTT Connection
client=mqtt.Client(clientid)
client.username_pw_set(username, password)
client.tls_set(cert_reqs=ssl.CERT_NONE) #no client certificate needed
client.tls_insecure_set(insecure)
client.connect(broker, port)
client.loop_start()
#print('Lux: {}'.format(sensor.lux))
#print('Broadband: {}'.format(sensor.broadband))
#print('Infrared: {}'.format(sensor.infrared))
#print('Luminosity: {}'.format(sensor.luminosity))
lux = sensor.lux
if lux == None:
lux = 0
client.publish("{}/lux".format(publish_topic),"{}".format(lux),qos,retain_message)
client.publish("{}/broadband".format(publish_topic),"{}".format(sensor.broadband),qos,retain_message)
client.publish("{}/infrared".format(publish_topic),"{}".format(sensor.infrared),qos,retain_message)
client.publish("{}/luminosity".format(publish_topic),"{}".format(sensor.luminosity),qos,retain_message)
time.sleep(2)
client.disconnect()
client.loop_stop()