#!/usr/bin/python import logging import asyncio from amqtt.client import MQTTClient from amqtt.mqtt.constants import QOS_0, QOS_1, QOS_2 import time import json logger = logging.getLogger(__name__) def encoder(inputdict): return json.dumps(inputdict).encode('utf-8') def get_uptime(): with open('/proc/uptime', 'r') as f: uptime_seconds = float(f.readline().split()[0]) print(uptime_seconds) return uptime_seconds async def test_coro(): C = MQTTClient() print("START" + str(get_uptime())) await C.connect('mqtt://127.0.0.1:1883/') tasks = [ #asyncio.ensure_future(C.publish('sensors/pico1/distance', encoder({"data": 23.0, "sensorID": 0, "timestamp": time.time_ns()}))), #asyncio.ensure_future(C.publish('a/b', b'TEST MESSAGE WITH QOS_1', qos=QOS_1)), asyncio.ensure_future(C.publish('sensors/pico1/uptime', encoder({"data": get_uptime(), "sensorID": 0, "timestamp": time.time_ns()}), qos=QOS_2)), ] await asyncio.wait(tasks) logger.info("messages published") await C.disconnect() print("END" + str(get_uptime())) if __name__ == '__main__': formatter = "[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s" logging.basicConfig(level=logging.INFO, format=formatter) with open('client.yaml', 'r') as fileread: clientconfig = yaml.safe_load(fileread) subdict(clientconfig, None) print(sublist) x = 0 while x < 5: asyncio.get_event_loop().run_until_complete(test_coro()) x = x + 1 #asyncio.get_event_loop().run_until_complete(test_coro2())