MQTT Client
This is a simplified version of an MQTT Client. Quality of Service 2 is not supported.
Subscriptions are pre-defined and set up on connection. Dynamic modifications to subscriptions not supported.
It is a singleton.
The client is started by calling the start method with a list of MQTTSubscription objects. The client will connect to the MQTT broker and subscribe to the topics defined in the subscription list. The client will then listen for incoming messages and handle them according to the subscription list.
asyncio.Streams are used for reading and writing.
| Class Method | get |
Return the singleton instance |
| Method | __init__ |
Construct the MQTT Client |
| Async Method | publish |
MQTT Publish |
| Async Method | run |
MQTT Client Run |
| Constant | DUP |
MQTT duplicate flag |
| Constant | ERR |
invalid connection acknowledgement |
| Constant | ERR |
ill formed length field |
| Constant | ERR |
Operating System Error on read or write |
| Constant | ERR |
unrecognised packet type |
| Constant | ERR |
invalid ping response |
| Constant | ERR |
invalid publish acknowledgement |
| Constant | ERR |
invalid subscription acknowledgement |
| Constant | ERR |
invalid UTF8 character |
| Constant | M |
no connection |
| Constant | M |
status connected (and subscribed) |
| Constant | M |
new connection - waiting for CONNACK |
| Constant | M |
connected - waiting for PINGRESP |
| Constant | M |
connection rejected by broker (it will terminate it!) |
| Constant | M |
new connection - waiting for SUBACK |
| Constant | PROTOCOL |
MQTT Protocol Level 4 (MQTT version 3.1.1) |
| Constant | QOS0 |
Quality of service 0 |
| Constant | QOS1 |
Quality of service 1 |
| Constant | QOS |
mask for QoS bits |
| Constant | RETAIN |
MQTT retain flag |
| Instance Variable | errors |
counts of errors by type |
| Property | broker |
The MQTT Broker ID. |
| Async Method | _close |
We only close the stream as part of error recovery |
| Async Method | _handle |
Undocumented |
| Async Method | _handle |
Undocumented |
| Async Method | _handle |
PUBACK received |
| Async Method | _handle |
PUBLISH received |
| Async Method | _handle |
Undocumented |
| Async Method | _pinger |
Coroutine to send a ping |
| Async Method | _puback |
Return PUBACK after receiving PUBLISH |
| Async Method | _re |
Open or Re-open the MQTT connection |
| Method | _set |
Undocumented |
| Async Method | _subscribe |
Undocumented |
| Constant | _RESP |
Undocumented |
| Class Variable | _mqtt |
Undocumented |
| Instance Variable | _client |
Undocumented |
| Instance Variable | _client |
Undocumented |
| Instance Variable | _con |
Undocumented |
| Instance Variable | _ping |
Undocumented |
| Instance Variable | _ping |
Undocumented |
| Instance Variable | _reader |
Undocumented |
| Instance Variable | _state |
Undocumented |
| Instance Variable | _subscription |
Undocumented |
| Instance Variable | _wifi |
Undocumented |
| Instance Variable | _writer |
Undocumented |
Construct the MQTT Client
Getting a reference to the Wi-Fi singleton will instantiate it if not already done.
MQTT Publish
At the moment we will assume no duplicates will be sent it only makes sense to send duplicates if clean session not used. I.e. sessions may span connections - therefore despite use of QoS one there's no need to save the message.
TODO Trap 'no free pid error'
| Parameters | |
| topic | MQTT topic |
| payload | the payload message to be sent |
| retain | boolean |
| qos | Quality of Service |
MQTT Client Run
This coroutine runs the MQTT client. It does not return.
| Parameters | |
| subscription | a list of MQTTSubscripion objects to subscribe to |
Coroutine to send a ping
The ping will be sent when the ping timer expires. The coroutine will close the connection and terminate if the previous ping has not been acknowledged. The coroutine will exit if no longer connected.
Undocumented
| Value |
|