class MQTTConnection(object):
Constructor: MQTTConnection(address, port, keepalive, interface, ...)
This class implements an MQTT client. It is a wrapper around the paho MQTT client. It is implemented as a BackgroundWorker/Actor, so it runs in its own thread.
| Method | __init__ |
Constructor. Initialize the MQTT client. |
| Method | __str__ |
Return a string representation of the MQTTConnection instance. |
| Method | is |
Check whether the number managed subscriptions matches the number of currently subscribed-to topics. |
| Method | publish |
Publish the message data with the topic topic with the MQTT broker. |
| Method | run |
Initialize and run the MQTT client as a BackgroundWorker/Actor. |
| Method | shutdown |
Shutting down the MQTT client. |
| Method | subscribe |
Add one or more MQTT topics to subscribe to. Add the topic(s) afterwards to the list of subscribed-to topics. |
| Method | unsubscribe |
Unsubscribe from a topic. topic is either an MQTTTopic structure with a previously subscribed to topic, or a topic name, in which case it is searched for in the list of MQTTTopics. |
| Class Variable | __slots__ |
Slots of the class. |
| Instance Variable | actor |
The actor for the MQTT client. |
| Instance Variable | address |
The address of the MQTT broker. |
| Instance Variable | bind |
The interface to bind to. |
| Instance Variable | ca |
The CA file for the MQTT broker's certificate. |
| Instance Variable | client |
The client ID for the MQTT client. |
| Instance Variable | enable |
Whether to enable WebSocket support. |
| Instance Variable | is |
Indicator whether the MQTT client is connected. |
| Instance Variable | is |
Indicator whether the MQTT client is stopped. |
| Instance Variable | keepalive |
The keepalive time for the MQTT connection. |
| Instance Variable | low |
Indicator whether to log MQTT messages. |
| Instance Variable | message |
The message handler. |
| Instance Variable | mqtt |
The MQTT client. |
| Instance Variable | mqtts |
The certificate file for the MQTT client. |
| Instance Variable | mqtts |
The key file for the MQTT client. |
| Instance Variable | password |
The password for the MQTT broker. |
| Instance Variable | port |
The port of the MQTT broker. |
| Instance Variable | subscribed |
The number of subscribed-to topics. |
| Instance Variable | subscribed |
The list of subscribed-to topics. |
| Instance Variable | username |
The username for the MQTT broker. |
| Instance Variable | use |
Whether to use TLS for the MQTT connection. |
| Instance Variable | verify |
Indicator whether to verify the MQTT broker's certificate. |
| Instance Variable | websocket |
The websocket path to use (incase of websockets). |
| Method | _mqtt |
BackgroundWorker callback to run the actuall MQTT loop. |
| Method | _on |
Callback when the MQTT client connected to the broker. |
| Method | _on |
Callback when the MQTT client disconnected from the broker. |
| Method | _on |
Mapping of the paho MQTT client's log to the logging system. Also handles different log-level scheme. |
| Method | _on |
Handle a received message. Forward it to the apropriate handler callback (in another Thread). |
| Method | _on |
Callback when the client successfulle subscribed to a topic. The topic is also added to the internal topic list. |
| Method | _on |
Callback when the client successfulle unsubscribed from a topic. The topic is also removed from the internal topic list. |
str, port: int | None = None, keepalive: int = 60, interface: str = '0.0.0.0', clientID: str | None = None, username: str | None = None, password: str | None = None, useTLS: bool = False, caFile: str | None = None, verifyCertificate: bool = False, certfile: str | None = None, keyfile: str | None = None, lowLevelLogging: bool = True, messageHandler: MQTTHandler | None = None, enableWebSocket: bool | None = False, webSocketPort: int | None = 8080, websocketPath: str | None = None):
¶
Constructor. Initialize the MQTT client.
| Parameters | |
address:str | The address of the MQTT broker. |
port:int | None | The port of the MQTT broker. |
keepalive:int | The keepalive time for the MQTT connection. |
interface:str | The interface to bind to. |
clientstr | None | The client ID for the MQTT client. |
username:str | None | The username for the MQTT broker. |
password:str | None | The password for the MQTT broker. |
usebool | Whether to use TLS for the MQTT connection. |
castr | None | The CA file for the MQTT broker's certificate. |
verifybool | Indicator whether to verify the MQTT broker's certificate. |
certfile:str | None | The certificate file for the MQTT client. |
keyfile:str | None | The key file for the MQTT client. |
lowbool | Indicator whether to log MQTT messages. |
messageMQTTHandler | None | The message handler. |
enablebool | None | Whether to enable WebSocket support. |
webint | None | The port to use for WebSocket connections. |
websocketstr | None | The websocket path to use (incase of websockets). |
Return a string representation of the MQTTConnection instance.
| Returns | |
str | A string representation of the MQTTConnection instance. |
Check whether the number managed subscriptions matches the number of currently subscribed-to topics.
| Returns | |
bool | True if fully subscribed, False otherwise. |
Publish the message data with the topic topic with the MQTT broker.
| Parameters | |
topic:str | The topic to publish to. |
data:bytes | The data to publish. |
str | list[ str], callback: MQTTCallback | None = None, **kwargs: Any):
¶
Add one or more MQTT topics to subscribe to. Add the topic(s) afterwards to the list of subscribed-to topics.
| Parameters | |
topic:str | list[ | The topic(s) to subscribe to. Either a single topic or a list of topics. |
callback:MQTTCallback | None | The callback function to call when a message is received for the topic. |
**kwargs:Any | Additional arguments for the callback function. |
MQTTClient, userdata: Any, flags: dict, reason_code: mqtt_rc.ReasonCode, properties: mqtt_pr.Properties):
¶
Callback when the MQTT client connected to the broker.
| Parameters | |
client:MQTTClient | The MQTT client. |
userdata:Any | User data. |
flags:dict | Flags. |
reasonmqtt_rc.ReasonCode | Reason code |
properties:mqtt_pr.Properties | Properties (MQTTv5 Only) |
MQTTClient, userdata: Any, disconnect_flags: mqtt.DisconnectFlags, reason_code: mqtt_rc.ReasonCode, properties: mqtt_pr.Properties):
¶
Callback when the MQTT client disconnected from the broker.
| Parameters | |
client:MQTTClient | The MQTT client. |
userdata:Any | User data. |
disconnectmqtt.DisconnectFlags | Undocumented |
reasonmqtt_rc.ReasonCode | Reason code |
properties:mqtt_pr.Properties | Properties (MQTTv5 Only) |
Mapping of the paho MQTT client's log to the logging system. Also handles different log-level scheme.
| Parameters | |
client:MQTTClient | The MQTT client. |
userdata:Any | User data. |
level:int | Log level. |
buf:str | Log message. |
Handle a received message. Forward it to the apropriate handler callback (in another Thread).
| Parameters | |
client:MQTTClient | The MQTT client. |
userdata:Any | User data. |
message:mqtt.MQTTMessage | The received message. |
MQTTClient, userdata: Any, mid: int, reason_codes: list[ mqtt_rc.ReasonCode], properties: mqtt_pr.Properties):
¶
Callback when the client successfulle subscribed to a topic. The topic is also added to the internal topic list.
| Parameters | |
client:MQTTClient | The MQTT client. |
userdata:Any | User data. |
mid:int | The message ID. |
reasonlist[ | Reason codes received from the broker for each subscription |
properties:mqtt_pr.Properties | Properties (MQTTv5 Only) |
MQTTClient, userdata: Any, mid: int, reason_codes: list[ mqtt_rc.ReasonCode], properties: mqtt_pr.Properties):
¶
Callback when the client successfulle unsubscribed from a topic. The topic is also removed from the internal topic list.