Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
Created October 26, 2016 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LarsBergqvist/a80f0eae0d63814410a9e9f9ed06243d to your computer and use it in GitHub Desktop.
Save LarsBergqvist/a80f0eae0d63814410a9e9f9ed06243d to your computer and use it in GitHub Desktop.
import paho.mqtt.client as mqtt
import time
class MQTTpublisher:
brokerIP = ""
brokerPort = 0
def __init__(self,brokerIP,brokerPort):
self.brokerIP = brokerIP
self.brokerPort = brokerPort
def postMessage(self,topic,message):
print("Publishing message " + message + " on topic " + topic)
# Initialize the client that should connect to the Mosquitto broker
client = mqtt.Client()
connOK=False
print("Connecting to " + self.brokerIP + " on port " + str(self.brokerPort))
while(connOK == False):
try:
print("try connect")
client.connect(self.brokerIP, self.brokerPort, 60)
connOK = True
except:
connOK = False
time.sleep(2)
client.publish(topic,message)
print("Publish done")
client.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment