EasyRider wrote: ↑Sun Dec 24, 2017 10:31 am
If you have a bit of time please explain the API connection arguments in your mqtt example and provide a simple connection example to broker on local unsecured wifi network.
You can use
mqtt_example.py for connection to broker on local network, just change "loboris.eu" to broker's IP address.
The complete documentation for all modules, including mqtt wil be pushed with the "New Year" update next week.
mqtt runs in separate thread (FreeRTOS task).
Note: When using secure connection more memory is required. If not using psRAM, you may need to lower the MicroPython heap.
Here is the simplified description of available mqtt methods:
Code: Select all
mqtt = network.mqtt(name, server [, user, password, port, autoreconnect, clientid, cleansession, keepalive, qos, retain, secure, connected_cb, disconnected_cb, subscribed_cb, unsubscribed_cb, published_cb, data_cb])
Creates the
mqtt object and starts the
mqtt client task.
Only two first arguments are mandatory:
- name - string, mqtt identifier, used to identify the mqtt client object, for example in collback functions
- server - string, mqtt server (broker) ip address or domain name
Other arguments are optional, if entered they must be entered as kw arguments (arg=value):
- user - string, user name if requred by mqtt server, default: ""
- password - string, user password if requred by mqtt server, default: ""
- clientid - string, mqtt client id, it is recomended to enter some unique ID, default: "mpy_mqtt_client"
- secure - bool, if True, use secure connection, default: False
- port - int, server's mqtt port, default: 1883 if secure=False, 8883 if secure=True
- autoreconnect - bool, if True, reconnect to server if disconnected for some reason, default: False
- cleansession - bool, if True, do not use mqtt persistent session feature, default: False
- keepalive - int, Keep Alive interval in seconds, default: 120
- qos - int, mqtt QoS level, default: 0
- retain - bool, mqtt retain flag, default 0
- connected_cb - callback function executed when mqtt is connected to the server; arguments: mqtt_name
- disconnected_cb - callback function executed when mqtt is disconnected to the server; arguments: mqtt_name
- subscribed_cb - callback function executed on succesful topic subscription; arguments: (mqtt_name, topic)
- unsubscribed_cb - callback function executed when the topic is unsubscribed; arguments: (mqtt_name, topic)
- published_cb - callback function executed when the topic is published; arguments: (mqtt_name, publish_result)
- data_cb - callback function executed when new subscribed topic data arrives; arguments: (mqtt_name, topic_data_length, topic_data)
Code: Select all
mqtt.config([clientid, autoreconnect, cleansession, keepalive, qos, retain, secure, connected_cb, disconnected_cb, subscribed_cb, unsubscribed_cb, published_cb, data_cb])
Reconfigure the mqtt client object.
All arguments are optional, if entered they must be entered as kw arguments (arg=value):
Returns the status of mqtt client task.
The status is returned as tuple: (num_stat, description).
Possible values are:
(0, "Connected")
(1, "Disconnected")
(2, "Stopping")
(3, "Stopped")
Before issuing any other mqtt command always check the mqtt status.
Subscribe to the topic, wait max 2 seconds.
Argument
topic is string, topic name.
Returns True is successfully subscribed, False if not.
Unsubscribe from the topic, wait max 2 seconds.
Argument
topic is string, topic name.
Returns True is successfully subscribed, False if not.
Publish message to the topic.
Argument
topic is string, topic name;
msg is string, topic message
Returns True is successfully published, False if not.
Stop the mqtt client task. Used resources are freed.
Returns True on success, False on error.
Start the stopped mqtt client task.
Stop the mqtt client task. Free resources and destroy the mqtt object
Returns True on success, False on error.