andypiper
9/15/2011 - 12:18 PM

MQTT blaster - repeated publish on a topic

MQTT blaster - repeated publish on a topic

#!/usr/bin/python
#
# simple script to repeatedly publish an MQTT message
#
# uses the Python MQTT client from the Paho project
# http://eclipse.org/paho
#
# Andy Piper @andypiper http://andypiper.co.uk
# 
# 2011/09/15 first version 
# 2012/05/28 updated to use new pure python client from mosquitto 
# 2014/02/03 updated to use the Paho client 
#
# pip install paho-mqtt
# python blast.py

import paho.mqtt.client as paho
import os
import time

broker = "m2m.eclipse.org"
port = 1883

mypid = os.getpid()
client_uniq = "pubclient_"+str(mypid)
mqttc = paho.Client(client_uniq, False) #nocleanstart

#connect to broker
mqttc.connect(broker, port, 60)

#remain connected and publish
while mqttc.loop() == 0:
    msg = "test message "+time.ctime()
    mqttc.publish("timesample", msg, 0, True) #qos=0, retain=y
    print "message published"
    time.sleep(1.5)
    pass