|     |   | 
| 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.
 MQTT Web App Client Demo
MQTT Web App Client Demo
Here is a corresponding ESP32 project that samples a temperature and humidity sensor and sends the data.
 MQTT ESP32 Client Demo
MQTT ESP32 Client Demo
Macro reference
Connect
|   | Connect | 
| Connects to the MQTT server (returns false if already connected) | 
|  - BOOL | Return | 
Disconnect
|   | Disconnect | 
| Disconnect from the MQTT server (returns false if client is already disconnected) | 
|  - BOOL | Return | 
GetClientId
|   | GetClientId | 
| Returns the unique identifier currently used when communicating with the server. | 
|  - STRING | Return | 
Publish
|   | Publish | 
| Publish a message to the MQTT server (returns false in the client is not connected) | 
|  - STRING | Topic | 
| The name of the topic for the message | 
|  - STRING | Message | 
| The message to publish | 
|  - BYTE | QoS | 
| The quality of service used to deliver the message (0, 1 or 2) | 
|  - BOOL | Retained | 
| If true. the message is to be retained by the server and delivered to future subscribers as well as current ones | 
|  - BOOL | Return | 
PublishBuffer
|   | PublishBuffer | 
| Publish an array of bytes to the MQTT server (returns false in the client is not connected) | 
|  - STRING | Topic | 
| The name of the topic for the message | 
|  - HANDLE | Buffer | 
| The message to publish (as an array buffer) | 
|  - BYTE | QoS | 
| The quality of service used to deliver the message (0, 1 or 2) | 
|  - BOOL | Retained | 
| If true. the message is to be retained by the server and delivered to future subscribers as well as current ones | 
|  - BOOL | Return | 
Subscribe
|   | Subscribe | 
| Subscribe to messages from the MQTT server (returns false in the client is not connected) | 
|  - STRING | Topic | 
| A filter for the topic name(s) to receive | 
|  - BYTE | QoS | 
| The quality of service used to deliver the message (0, 1 or 2) | 
|  - BYTE | Timeout | 
| The number of seconds to wait before assuming this operation failed | 
|  - BOOL | Return | 
Unsubscribe
|   | Unsubscribe | 
| Unsubscribe from messages from the MQTT server (returns false in the client is not connected) | 
|  - STRING | Topic | 
| A filter for the topic name(s) to stop receiving | 
|  - BYTE | Timeout | 
| The number of seconds to wait before assuming this operation failed | 
|  - BOOL | Return | 
Property reference
|   | Properties | 
|   | Host | 
| The address of the messaging server, as a fully qualified WebSocket URI, as a DNS name or dotted decimal IP address | 
|   | Port | 
| The port number to connect to | 
|   | Directory | 
| An optional subdirectory off the host address (e.g. "/mqtt") | 
|   | Client Id | 
| A unique identifier for this client, between 1 and 23 characters in length | 
|   | Advanced | 
|   | Use SSL | 
| Use a secure (SSL) connection | 
|   | Use authentication | 
| Use authentication | 
|   | Connection timeout | 
| If connection has not succeeded within this time (in seconds), it is deemed to have failed | 
|   | Keep Alive Interval | 
| The server will disconnect this client if there is no activity for this time period (in seconds) | 
|   | Clean Session | 
| If true, a non-persistent connection is created, meaning subscription information and undelivered messages are not retained when this connection ends | 
|   | Reconnect | 
| If true, this client will attempt to reconnect to the server if the connection is lost | 
|   | Send LWT | 
| Send a 'Last Will and Testament' message if unexpected disconnection occurs | 
|   | MQTT Version | 
| Which version of MQTT to use | 
|   | Debug | 
| Determines if additional logging information is sent to the JavaScript console | 
|   | Callbacks | 
|   | OnSuccess | 
| Called when an MQTT operation is successful | 
|   | OnFailure | 
| Called when an MQTT operation is unsuccessful | 
|   | OnConnected | 
| Called when an MQTT connection is successfully made to the server | 
|   | OnConnectionLost | 
| Called when an MQTT connection has been lost | 
|   | OnMessageDelivered | 
| Called when an MQTT message has been delivered | 
|   | OnMessageArrived | 
| Called when an MQTT message has arrived at this client |