from paho.mqtt import client as mqtt_client import time from datetime import datetime from os import environ is_docker = environ.get('AM_I_IN_A_DOCKER_CONTAINER', False) broker = "mqttbroker" if is_docker else "localhost" client = mqtt_client.Client("testasset") # Wait for broker if is_docker: time.sleep(3) client.connect(broker, 1883, 60) # Send the current datetime stamp to the topic "testopic/testparam1" and "testopic/testparam2" every 1 second while True: now = datetime.now() client.publish("testopic/testparam1", str(now)) client.publish("testopic/testparam2", str(now)) time.sleep(1)