Last active
June 15, 2016 10:04
-
-
Save clonejo/6bb80ae23e174a3b22ab5f6ecdf24e6b to your computer and use it in GitHub Desktop.
python 2 script which publishes the current unix timestamp to mqtt at regular intervals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python2 | |
| import paho.mqtt.client as mqtt | |
| import time | |
| import math | |
| interval = 60 | |
| mqttc = mqtt.Client() | |
| mqttc.connect("mqtt.space.aachen.ccc.de") | |
| mqttc.loop_start() | |
| def get_sleep_until(now): | |
| scaled_up = now / interval | |
| if math.ceil(scaled_up) == scaled_up: | |
| sleep_until = scaled_up + 1 | |
| else: | |
| sleep_until = math.ceil(scaled_up) | |
| return sleep_until * interval | |
| while True: | |
| now = time.time() | |
| sleep_until = get_sleep_until(now) | |
| if sleep_until - now > 0.001: | |
| time.sleep(sleep_until - now) | |
| mqttc.publish("time", int(round(sleep_until))) | |
| mqttc.loop_stop() | |
| mqttc.disconnect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment