Add timestamp (with ntptime and RTC)

This commit is contained in:
Pierrick C 2020-03-25 16:28:22 +01:00
parent e3bf80bbe8
commit 95453be6a9
1 changed files with 13 additions and 1 deletions

View File

@ -44,6 +44,15 @@ bme.set_measurement_settings({
# Start the sensor automatically sensing
bme.set_power_mode(bme280_i2c.BME280_NORMAL_MODE)
# RTC setup
rtc=machine.RTC()
settime()
def now():
"""Return a string representing date/time now"""
return "{0:04d}/{1:02d}/{2:02d}_{4:02d}:{5:02d}:{6:02d}".format(*rtc.datetime())
def MqttPublish(client, topic, message, retain=False, qos=0, sleep=10):
"""MQTT publish helper"""
@ -61,6 +70,7 @@ client = MQTTClient(client_id=config.CLIENT_ID,
port=config.MQTT_PORT)
client.connect()
MqttPublish(client, "last_boot", now())
MqttPublish(client, "location", config.LOCATION, retain=True, qos=1)
MqttPublish(client, "humidity/unit", "%", retain=True)
MqttPublish(client, "humidity/desc", "Capteur Bosch BME280", retain=True, qos=0)
@ -86,7 +96,8 @@ topics = {"humidity" : ["humidity/value", "{:.0f}", 1, 1],
while 1:
bme_data = bme.get_measurement()
print(bme_data)
meas_time = now()
print(meas_time, ":", bme_data)
for nature, param in topics.items():
MqttPublish(client,
@ -96,5 +107,6 @@ while 1:
sleep=50)
MqttPublish(client, "wifi/rssi", "{:.0f}".format(wlan.status('rssi')))
MqttPublish(client, "time/last_values", meas_time, retain=True)
time.sleep_ms(1000)