本文整理汇总了Python中paho.mqtt.client.MQTTv31方法的典型用法代码示例。如果您正苦于以下问题:Python client.MQTTv31方法的具体用法?Python client.MQTTv31怎么用?Python client.MQTTv31使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类paho.mqtt.client
的用法示例。
在下文中一共展示了client.MQTTv31方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: single
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import MQTTv31 [as 别名]
def single(topic, payload=None, qos=0, retain=False, hostname="localhost",
port=1883, client_id="", keepalive=60, will=None, auth=None,
tls=None, protocol=mqtt.MQTTv31):
"""Publish a single message to a broker, then disconnect cleanly.
This function creates an MQTT client, connects to a broker and publishes a
single message. Once the message has been delivered, it disconnects cleanly
from the broker.
topic : the only required argument must be the topic string to which the
payload will be published.
payload : the payload to be published. If "" or None, a zero length payload
will be published.
qos : the qos to use when publishing, default to 0.
retain : set the message to be retained (True) or not (False).
hostname : a string containing the address of the broker to connect to.
Defaults to localhost.
port : the port to connect to the broker on. Defaults to 1883.
client_id : the MQTT client id to use. If "" or None, the Paho library will
generate a client id automatically.
keepalive : the keepalive timeout value for the client. Defaults to 60
seconds.
will : a dict containing will parameters for the client: will = {'topic':
"<topic>", 'payload':"<payload">, 'qos':<qos>, 'retain':<retain>}.
Topic is required, all other parameters are optional and will
default to None, 0 and False respectively.
Defaults to None, which indicates no will should be used.
auth : a dict containing authentication parameters for the client:
auth = {'username':"<username>", 'password':"<password>"}
Username is required, password is optional and will default to None
if not provided.
Defaults to None, which indicates no authentication is to be used.
tls : a dict containing TLS configuration parameters for the client:
dict = {'ca_certs':"<ca_certs>", 'certfile':"<certfile>",
'keyfile':"<keyfile>", 'tls_version':"<tls_version>",
'ciphers':"<ciphers">}
ca_certs is required, all other parameters are optional and will
default to None if not provided, which results in the client using
the default behaviour - see the paho.mqtt.client documentation.
Defaults to None, which indicates that TLS should not be used.
"""
msg = {'topic':topic, 'payload':payload, 'qos':qos, 'retain':retain}
multiple([msg], hostname, port, client_id, keepalive, will, auth, tls, protocol)
示例2: get_settings_defaults
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import MQTTv31 [as 别名]
def get_settings_defaults(self):
return dict(
broker=dict(
url=None,
port=1883,
username=None,
password=None,
keepalive=60,
tls=dict(),
tls_insecure=False,
protocol="MQTTv31",
retain=True,
clean_session=True
),
publish=dict(
baseTopic="octoPrint/",
eventTopic="event/{event}",
eventActive=True,
printerData=False,
events=dict(server=True,
comm=True,
files=True,
printjob=True,
gcode=True,
timelapse=True,
slicing=True,
settings=True,
unclassified=True),
progressTopic="progress/{progress}",
progressActive=True,
temperatureTopic="temperature/{temp}",
temperatureActive=True,
temperatureThreshold=0.1,
lwTopic="mqtt",
lwActive=True
),
client=dict(
client_id=None
),
timestamp_fieldname="_timestamp"
)
示例3: mqtt_connect
# 需要导入模块: from paho.mqtt import client [as 别名]
# 或者: from paho.mqtt.client import MQTTv31 [as 别名]
def mqtt_connect(self):
broker_url = self._settings.get(["broker", "url"])
broker_port = self._settings.get_int(["broker", "port"])
broker_username = self._settings.get(["broker", "username"])
broker_password = self._settings.get(["broker", "password"])
broker_keepalive = self._settings.get_int(["broker", "keepalive"])
broker_tls = self._settings.get(["broker", "tls"], asdict=True)
broker_tls_insecure = self._settings.get_boolean(["broker", "tls_insecure"])
broker_protocol = self._settings.get(["broker", "protocol"])
client_id = self._settings.get(["client", "client_id"])
clean_session = self._settings.get_boolean(["broker", "clean_session"])
lw_active = self._settings.get_boolean(["publish", "lwActive"])
lw_topic = self._get_topic("lw")
if broker_url is None:
self._logger.warn("Broker URL is None, can't connect to broker")
return
import paho.mqtt.client as mqtt
protocol_map = dict(MQTTv31=mqtt.MQTTv31, MQTTv311=mqtt.MQTTv311)
if broker_protocol in protocol_map:
protocol = protocol_map[broker_protocol]
else:
protocol = mqtt.MQTTv31
if self._mqtt is None:
self._mqtt = mqtt.Client(client_id=client_id, protocol=protocol, clean_session=clean_session)
if broker_username is not None:
self._mqtt.username_pw_set(broker_username, password=broker_password)
tls_active = False
if broker_tls:
tls_args = dict((key, value) for key, value in broker_tls.items() if value)
ca_certs = tls_args.pop("ca_certs", None)
if ca_certs: # cacerts must not be None for tls_set to work
self._mqtt.tls_set(ca_certs, **tls_args)
tls_active = True
if broker_tls_insecure and tls_active:
self._mqtt.tls_insecure_set(broker_tls_insecure)
_retain = self._settings.get_boolean(["broker", "retain"])
if lw_active and lw_topic:
self._mqtt.will_set(lw_topic, self.LWT_DISCONNECTED, qos=1, retain=_retain)
self._mqtt.on_connect = self._on_mqtt_connect
self._mqtt.on_disconnect = self._on_mqtt_disconnect
self._mqtt.on_message = self._on_mqtt_message
self._mqtt.connect_async(broker_url, broker_port, keepalive=broker_keepalive)
if self._mqtt.loop_start() == mqtt.MQTT_ERR_INVAL:
self._logger.error("Could not start MQTT connection, loop_start returned MQTT_ERR_INVAL")