My requirement is to gather data on my device every 5th minute. This resembles a function app which is triggered every 5th minute. My device has limited resources, and i wish to do this with Python.
I have tried something similar to this, which sleeps until clock is a multiple of 5.
from azure.iot.device.aio import IoTHubModuleClient
from azure.iot.device import Message
from six.moves import input
import asyncio
from datetime import datetime, timedelta
async def main():
sample_interval = 5
executionTime = datetime.utcnow()
try:
nextSample = executionTime + timedelta(minutes=5)
secondsUntilSampleInterval = (nextSample - datetime.utcnow()).seconds
while True:
print("module is connected: " + str(module_client.connected))
if not module_client.connected:
await module_client.connect()
await asyncio.gather(asyncio.sleep(secondsUntilSampleInterval), GatherData())
secondsUntilSampleInterval = sample_interval*60
except Exception as e:
print("Unexpected error %s " % e)
raise
However this does not meet my demands as it drifts over time.
I wish to have the GatherData function triggered when the clock is 10:00, 10:05, 10:10, ...
EDIT: I forgot to mention that the GatherData function is async.