From 95453be6a920dc8e0ecf33cdda1322169e47bde1 Mon Sep 17 00:00:00 2001 From: Pierrick C Date: Wed, 25 Mar 2020 16:28:22 +0100 Subject: [PATCH] Add timestamp (with ntptime and RTC) --- code/main.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/code/main.py b/code/main.py index 05103d4..0f9ed41 100644 --- a/code/main.py +++ b/code/main.py @@ -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)