Skip to content

Instantly share code, notes, and snippets.

@clonejo
Last active June 15, 2016 10:04
Show Gist options
  • Select an option

  • Save clonejo/6bb80ae23e174a3b22ab5f6ecdf24e6b to your computer and use it in GitHub Desktop.

Select an option

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
#!/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