def dth_read():
global client
global d
global oled
global CLIENT_ID
global loop
topic = 'micro/{0}/temperature'.format(CLIENT_ID.decode("utf-8"))
while True:
try:
d.measure()
oled.fill(0)
oled.text('ESP32', 45, 5)
oled.text('MicroPython', 20, 20)
oled.text('T:{0:.2f} C'.format(d.temperature()), 3, 35)
oled.text('H:{0:.2f} %'.format(d.humidity()), 3, 50)
oled.show()
msg = json.dumps({
'heap': gc.mem_free(), 'Type':7,
'Id': CLIENT_ID,
'temperature': '{0:.2f}'.format(d.temperature()),
'humidity': '{0:.2f}'.format(d.humidity())
})
print(topic, msg)
client.publish(topic, msg)
except OSError as e:
if e.args[0] == errno.ETIMEDOUT:
print('error dht: ', e)
else:
loop.stop()
print('error mqtt client: ', e)
print('restart mqtt client')
cfg = load_config()
mqtt_cfg = cfg['mqtt']
print('restart mqtt client: ', mqtt_cfg)
loop.run_until_complete(mqtt_connection(mqtt_cfg))
loop.run_forever()
continue
await asyncio.sleep(5)
评论列表
文章目录