Difference between revisions of "Component: MQTT Client ( Comms)"

From Flowcode Help
Jump to navigationJump to search
(Created page with "{| style="width:50%" |- | width="20%" style="color:gray;" | Author | MatrixTSL |- | width="20%" style="color:gray;" | Version | 0.1 |- | width="20%" style="color:gray;...")
 
Line 27: Line 27:
 
==Examples==
 
==Examples==
  
''<span style="color:red;">No additional examples</span>''
+
===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.
 +
 
 +
{{Fcfile|WebApp_MQTT_Client_Demo.fcsx|MQTT Web App Client Demo}}
 +
 
 +
 
 +
Here is a corresponding ESP32 project that samples a temperature and humidity sensor and sends the data.
 +
 
 +
{{Fcfile|ESP_MQTT_Client_TempHumidityDemo.fcfx|MQTT ESP32 Client Demo}}
  
 
==Macro reference==
 
==Macro reference==

Revision as of 17:13, 1 December 2023

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

No detailed description exists yet for this component

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

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