forked from slzatz/esp8266
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbme280_mqtt_timer.py
46 lines (34 loc) · 972 Bytes
/
bme280_mqtt_timer.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
'''
Based on driver at https://github.com/catdog2/mpy_bme280_esp8266
Created on 4-15-2017
'''
from bme280 import BME280
from machine import I2C, Pin, Timer
from time import sleep
from umqtt_client_official import MQTTClient as umc
import json
from config import mqtt_aws_host, pos
i2c = I2C(scl=Pin(5), sda=Pin(4))
bme = BME280(i2c=i2c, address=0x77) #0x77
flag = bytearray(1)
flag[0] = 0
sleep(20)
c = umc("10222017z", mqtt_aws_host, 1883)
c.connect()
def callback(t):
flag[0] = 1
tim = Timer(-1)
tim.init(period=150000, mode=Timer.PERIODIC, callback=callback)
while 1:
if flag[0]:
z = bme.read_compensated_data()
temp = 32 + 9*z[0]/500
humidity = z[2]/1024
try:
c.publish("esp_tft", json.dumps({"header":"CT Temp/Humidity", "text":["temperature: {:.1f}".format(temp), "humidity: {:.0f}%".format(humidity)], "pos":pos}))
except Exception as e:
print(e)
c.sock.close()
c.connect()
flag[0] = 0
sleep(1)