Component: MQTT Client ( Comms)

From Flowcode Help
Jump to navigationJump to search
Author MatrixTSL
Version 0.1
Category Comms


MQTT Client component

A component providing access to MQTT IoT client. The client can connect to a MQTT server such as ThingSpeak via WebSockets. Allows IoT type data to be published and read back.

Component Source Code

Please click here to download the component source project: FC_Comp_Source_WEBEXP_MQTT.fcsx

Please click here to view the component source code (Beta): FC_Comp_Source_WEBEXP_MQTT.fcsx

Detailed description

Please note this component currently only supports Web Sockets and must be connected to the Web Socket enabled port of the broker.

Examples

Temperature and Humidity

Here is an example MQTT project showing how to collect data from a remote device using MQTT.

The heater and fan buttons have onclick event macros that encode JSON data and send to a MQTT topic named mtx_demo_switchdata.

The OnMqttConnected event macro subscribes to MQTT topics named mtx_demo_sensedata which contains the embdeeded temperature and humidity data and mtx_demo_switchdata which contains the current fan and heater output states.

FC6 Icon.png MQTT Web App Client Demo


Here is a corresponding ESP32 project that samples a temperature and humidity sensor and sends the data.

FC6 Icon.png MQTT ESP32 Client Demo


Temperature and Humidity HTTPS

Here is the temperature and humidity example again but this time set to use HTTPS so that it can be ran from a enabled HTTPS webserver.

FC6 Icon.png MQTT Web App Client HTTPS Demo

An example of this can be seen via the URL: https://flowcode.co.uk/example/index.htm


Macro reference

Connect

Fc9-comp-macro.png Connect
Connects to the MQTT server (returns false if already connected) 
Fc9-bool-icon.png - BOOL Return


Disconnect

Fc9-comp-macro.png Disconnect
Disconnect from the MQTT server (returns false if client is already disconnected) 
Fc9-bool-icon.png - BOOL Return


GetClientId

Fc9-comp-macro.png GetClientId
Returns the unique identifier currently used when communicating with the server. 
Fc9-string-icon.png - STRING Return


Publish

Fc9-comp-macro.png Publish
Publish a message to the MQTT server (returns false in the client is not connected) 
Fc9-string-icon.png - STRING Topic
The name of the topic for the message 
Fc9-string-icon.png - STRING Message
The message to publish 
Fc9-u8-icon.png - BYTE QoS
The quality of service used to deliver the message (0, 1 or 2) 
Fc9-bool-icon.png - BOOL Retained
If true. the message is to be retained by the server and delivered to future subscribers as well as current ones 
Fc9-bool-icon.png - BOOL Return


PublishBuffer

Fc9-comp-macro.png PublishBuffer
Publish an array of bytes to the MQTT server (returns false in the client is not connected) 
Fc9-string-icon.png - STRING Topic
The name of the topic for the message 
Fc9-h32-icon.png - HANDLE Buffer
The message to publish (as an array buffer) 
Fc9-u8-icon.png - BYTE QoS
The quality of service used to deliver the message (0, 1 or 2) 
Fc9-bool-icon.png - BOOL Retained
If true. the message is to be retained by the server and delivered to future subscribers as well as current ones 
Fc9-bool-icon.png - BOOL Return


Subscribe

Fc9-comp-macro.png Subscribe
Subscribe to messages from the MQTT server (returns false in the client is not connected) 
Fc9-string-icon.png - STRING Topic
A filter for the topic name(s) to receive 
Fc9-u8-icon.png - BYTE QoS
The quality of service used to deliver the message (0, 1 or 2) 
Fc9-u8-icon.png - BYTE Timeout
The number of seconds to wait before assuming this operation failed 
Fc9-bool-icon.png - BOOL Return


Unsubscribe

Fc9-comp-macro.png Unsubscribe
Unsubscribe from messages from the MQTT server (returns false in the client is not connected) 
Fc9-string-icon.png - STRING Topic
A filter for the topic name(s) to stop receiving 
Fc9-u8-icon.png - BYTE Timeout
The number of seconds to wait before assuming this operation failed 
Fc9-bool-icon.png - BOOL Return


Property reference

Fc9-prop-icon.png Properties
Fc9-type-10-icon.png Host
The address of the messaging server, as a fully qualified WebSocket URI, as a DNS name or dotted decimal IP address 
Fc9-type-21-icon.png Port
The port number to connect to 
Fc9-type-10-icon.png Directory
An optional subdirectory off the host address (e.g. "/mqtt") 
Fc9-type-10-icon.png Client Id
A unique identifier for this client, between 1 and 23 characters in length 
Fc9-conn-icon.png Advanced
Fc9-type-7-icon.png Use SSL
Use a secure (SSL) connection 
Fc9-type-7-icon.png Use authentication
Use authentication 
Fc9-type-21-icon.png Connection timeout
If connection has not succeeded within this time (in seconds), it is deemed to have failed 
Fc9-type-21-icon.png Keep Alive Interval
The server will disconnect this client if there is no activity for this time period (in seconds) 
Fc9-type-7-icon.png Clean Session
If true, a non-persistent connection is created, meaning subscription information and undelivered messages are not retained when this connection ends 
Fc9-type-7-icon.png Reconnect
If true, this client will attempt to reconnect to the server if the connection is lost 
Fc9-type-7-icon.png Send LWT
Send a 'Last Will and Testament' message if unexpected disconnection occurs 
Fc9-type-12-icon.png MQTT Version
Which version of MQTT to use 
Fc9-type-7-icon.png Debug
Determines if additional logging information is sent to the JavaScript console 
Fc9-conn-icon.png Callbacks
Fc9-type-24-icon.png OnSuccess
Called when an MQTT operation is successful 
Fc9-type-24-icon.png OnFailure
Called when an MQTT operation is unsuccessful  
Fc9-type-24-icon.png OnConnected
Called when an MQTT connection is successfully made to the server  
Fc9-type-24-icon.png OnConnectionLost
Called when an MQTT connection has been lost  
Fc9-type-24-icon.png OnMessageDelivered
Called when an MQTT message has been delivered  
Fc9-type-24-icon.png OnMessageArrived
Called when an MQTT message has arrived at this client