Created
October 26, 2016 17:03
-
-
Save LarsBergqvist/a80f0eae0d63814410a9e9f9ed06243d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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