Last active
August 20, 2018 14:01
-
-
Save SchofieChen/d8bfcf1451d6adfa5b2b237312b9aff4 to your computer and use it in GitHub Desktop.
Mqtt Subscribe
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 time, signal | |
import paho.mqtt.client as mqtt | |
client = None | |
mqtt_looping = False | |
class mqtt_client_program(): | |
def __ini__(self,): | |
pass | |
def on_connect(self,mq, userdata, rc, _): | |
# subscribe when connected. | |
mq.subscribe("SMG/P5/OA/#") | |
def on_message(self,q, userdata, msg): | |
print ("topic: %s" % msg.topic) | |
print ("payload: %s" % msg.payload.decode('utf8')) | |
# print( "qos: %d" % msg.qos) | |
def mqtt_client_thread(self,): | |
global client, mqtt_looping | |
client_id = "" # If broker asks client ID. | |
client = mqtt.Client(client_id=client_id) | |
# If broker asks user/password. | |
user = "" | |
password = "" | |
client.username_pw_set(user, password) | |
client.on_connect = self.on_connect | |
client.on_message = self.on_message | |
try: | |
client.connect("localhost") | |
except: | |
print( "MQTT Broker is not online. Connect later.") | |
mqtt_looping = True | |
print ("Looping...") | |
#mqtt_loop.loop_forever() | |
cnt = 0 | |
while mqtt_looping: | |
client.loop() | |
cnt += 1 | |
if cnt > 20: | |
try: | |
client.reconnect() # to avoid 'Broken pipe' error. | |
except: | |
time.sleep(1) | |
cnt = 0 | |
print ("quit mqtt thread") | |
client.disconnect() | |
def stop_all(*args): | |
global mqtt_looping | |
mqtt_looping = False | |
if __name__ == '__main__': | |
mqtt_client = mqtt_client_program() | |
mqtt_client.mqtt_client_thread() | |
print ("exit program") | |
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 time, signal | |
import paho.mqtt.client as mqtt | |
client = None | |
mqtt_looping = False | |
class mqtt_client_program(): | |
def __ini__(self,): | |
pass | |
def on_connect(self,mq, userdata, rc, _): | |
# subscribe when connected. | |
mq.subscribe("SMG/P5/OA/#") | |
def on_message(self,q, userdata, msg): | |
print ("topic: %s" % msg.topic) | |
print ("payload: %s" % msg.payload.decode('utf8')) | |
# print( "qos: %d" % msg.qos) | |
def mqtt_client_thread(self,): | |
global client, mqtt_looping | |
client_id = "" # If broker asks client ID. | |
client = mqtt.Client(client_id=client_id) | |
# If broker asks user/password. | |
user = "" | |
password = "" | |
client.username_pw_set(user, password) | |
client.on_connect = self.on_connect | |
client.on_message = self.on_message | |
try: | |
client.connect("localhost") | |
except: | |
print( "MQTT Broker is not online. Connect later.") | |
mqtt_looping = True | |
print ("Looping...") | |
#mqtt_loop.loop_forever() | |
cnt = 0 | |
while mqtt_looping: | |
client.loop() | |
cnt += 1 | |
if cnt > 20: | |
try: | |
client.reconnect() # to avoid 'Broken pipe' error. | |
except: | |
time.sleep(1) | |
cnt = 0 | |
print ("quit mqtt thread") | |
client.disconnect() | |
def stop_all(*args): | |
global mqtt_looping | |
mqtt_looping = False | |
if __name__ == '__main__': | |
mqtt_client = mqtt_client_program() | |
mqtt_client.mqtt_client_thread() | |
print ("exit program") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mqtt client for subscription