forked from elmariofredo/raspberrypi-tempstation-htu21d-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·38 lines (26 loc) · 808 Bytes
/
main.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
#!/usr/bin/env python3
#
import os
import time
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
from Adafruit_HTU21D.HTU21D import HTU21D
broker = os.environ["TEMP_BROKER_ADDRESS"]
room_name = os.environ["TEMP_ROOM_NAME"]
delay = int(os.environ["TEMP_BROKER_DELAY"])
client = mqtt.Client()
client.connect(broker)
client.loop_start()
h = HTU21D()
temperature=0
humidity=0
while True:
current_temperature = h.read_temperature()
current_humidity = h.read_humidity()
if current_temperature != temperature:
temperature = current_temperature
client.publish(room_name + '/temperature', temperature)
if current_humidity != humidity:
humidity = current_humidity
client.publish(room_name + '/humidity', humidity)
time.sleep(delay)