Jump to content

Component: MQTT Client (Comms: IoT/Cloud): Difference between revisions

From Flowcode Help
No edit summary
m Text replacement - "width="90%" style="background-color:#D8C9D8; color:#4B008D;"" to "width="90%" class="mtx-class-macrohead""
 
(17 intermediate revisions by 2 users not shown)
Line 15: Line 15:
A component providing access to MQTT IoT client. The client can connect to a MQTT server such as ThingSpeak. Allowing IoT type data to be published and read back.
A component providing access to MQTT IoT client. The client can connect to a MQTT server such as ThingSpeak. Allowing IoT type data to be published and read back.


==Component Source Code==
==Version information==


Please click here for the component source code: [https://www.flowcode.co.uk/wikicopy/componentsource/FC_Comp_Source_MQTT_Client.fcfx FC_Comp_Source_MQTT_Client.fcfx]
Library Version, Component Version, Date, Author, Info
7, 1.1, 23-07-24, BR, Added Max Packet Size property to allow the size of a single packet to be altered by the user
8, 1.1, 30-07-24, BR, Resolved ESP compile error due to missing return and added TopicTooLong error


==Detailed description==
==Detailed description==




Line 34: Line 48:


==Examples==
==Examples==




Line 103: Line 129:




==Downloadable macro reference==


{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
 
 
 
 
 
 
 
 
 
 
 
==Macro reference==
 
===Connect===
{| class="mtx-class-macrotable wikitable"
|-
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" class="mtx-class-macrohead" | '''Connect'''
|-
| colspan="2" | Connect to the MQTT Server. Returns the connected state. 0 if not connected, 1 if successful connection, or already connected.  
|-
|-
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
| width="90%" style="border-top: 2px solid #000;" | ''Return''
|}
 
 
===ConnectTo===
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''GetTopic'''
| width="90%" class="mtx-class-macrohead" | '''ConnectTo'''
|-
|-
| colspan="2" | Get the Topic of the last received (Read) packet. Returns the number of bytes for the Topic 
| colspan="2" | Connect to a variable MQTT Server, overriding the Host and Port properties. Returns the connected state. 0 if not connected, 1 if successful connection, or already connected. 
|-
|-
|-
|-
| width="10%" align="center" | [[File:Fc9-string-icon.png]] - STRING
| width="10%" align="center" | [[File:Fc9-string-icon.png]] - STRING
| width="90%" | Topic
| width="90%" | Host
|-
| colspan="2" |  
|-
| width="10%" align="center" | [[File:Fc9-u16-icon.png]] - UINT
| width="90%" | Port
|-
|-
| colspan="2" |  
| colspan="2" |  
|-
|-
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
| width="10%" align="center" | [[File:Fc9-string-icon.png]] - STRING
| width="90%" | Size
| width="90%" | ClientID
|-
| colspan="2" | Client identifier name - must be unique on server 
|-
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
| width="90%" style="border-top: 2px solid #000;" | ''Return''
|}
 
 
===Disconnect===
{| class="mtx-class-macrotable wikitable"
|-
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" class="mtx-class-macrohead" | '''Disconnect'''
|-
| colspan="2" | Disconnects from the MQTT server and the network 
|-
|-
| colspan="2" | Byte count the Topic buffer 
|-
|-
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
Line 128: Line 200:




{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
===ErrorAsString===
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ErrorAsString'''
| width="90%" class="mtx-class-macrohead" | '''ErrorAsString'''
|-
|-
| colspan="2" |  
| colspan="2" |  
Line 146: Line 219:




{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
===GetPayload===
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Connect'''
| width="90%" class="mtx-class-macrohead" | '''GetPayload'''
|-
|-
| colspan="2" | Connect to the MQTT Server. Returns the connected state. 0 if not connected, 1 if successful connection, or already connected.  
| colspan="2" | Get the Payload of the last received (Read) packet. Returns the number of bytes for the Payload 
|-
|-
| width="10%" align="center" | [[File:Fc9-string-icon.png]] - STRING
| width="90%" | Payload
|-
| colspan="2" |  
|-
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
| width="90%" | Size
|-
|-
| colspan="2" | Byte count the Payload buffer 
|-
|-
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
Line 159: Line 243:




{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
===GetTopic===
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Publish'''
| width="90%" class="mtx-class-macrohead" | '''GetTopic'''
|-
|-
| colspan="2" | Publish data to the server (broker) Returns non-zero if successful 
| colspan="2" | Get the Topic of the last received (Read) packet. Returns the number of bytes for the Topic 
|-
|-
|-
|-
Line 172: Line 257:
| colspan="2" |  
| colspan="2" |  
|-
|-
| width="10%" align="center" | [[File:Fc9-string-icon.png]] - STRING
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
| width="90%" | Payload
| width="90%" | Size
|-
|-
| colspan="2" |  
| colspan="2" | Byte count the Topic buffer 
|-
|-
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
Line 182: Line 267:




{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
===Initialise===
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''LastError'''
| width="90%" class="mtx-class-macrohead" | '''Initialise'''
|-
|-
| colspan="2" | Returns the last error 
| colspan="2" |  
|-
|-
|-
|-
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-void-icon.png]] - VOID
| width="90%" style="border-top: 2px solid #000;" | ''Return''
| width="90%" style="border-top: 2px solid #000;" | ''Return''
|}
|}




{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
===LastError===
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Read'''
| width="90%" class="mtx-class-macrohead" | '''LastError'''
|-
|-
| colspan="2" | Read incoming data. Returns non-zero if packet received. Use GetTopic and GetPayload to read the contents. 
| colspan="2" | Returns the last error 
|-
|-
|-
|-
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u16-icon.png]] - UINT
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
| width="90%" style="border-top: 2px solid #000;" | ''Return''
| width="90%" style="border-top: 2px solid #000;" | ''Return''
|}
|}




{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
===Ping===
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Disconnect'''
| width="90%" class="mtx-class-macrohead" | '''Ping'''
|-
|-
| colspan="2" | Disconnects from the MQTT server and the network 
| colspan="2" | Ping packet is sent to the Server and checks for a reply. Returns non-zero value if successful  
|-
|-
|-
|-
Line 221: Line 309:




{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
===Publish===
{| class="mtx-class-macrotable wikitable"
|-
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" class="mtx-class-macrohead" | '''Publish'''
|-
| colspan="2" | Publish data to the server (broker) Returns non-zero if successful 
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Ping'''
|-
|-
| colspan="2" | Ping packet is sent to the Server and checks for a reply. Returns non-zero value if successful  
| width="10%" align="center" | [[File:Fc9-string-icon.png]] - STRING
| width="90%" | Topic
|-
| colspan="2" |  
|-
|-
| width="10%" align="center" | [[File:Fc9-string-icon.png]] - STRING
| width="90%" | Payload
|-
| colspan="2" |  
|-
|-
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
Line 234: Line 333:




{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
===Read===
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''GetPayload'''
| width="90%" class="mtx-class-macrohead" | '''Read'''
|-
|-
| colspan="2" | Get the Payload of the last received (Read) packet. Returns the number of bytes for the Payload 
| colspan="2" | Read incoming data. Returns non-zero if packet received. Use GetTopic and GetPayload to read the contents. 
|-
|-
|-
|-
| width="10%" align="center" | [[File:Fc9-string-icon.png]] - STRING
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u16-icon.png]] - UINT
| width="90%" | Payload
|-
| colspan="2" |  
|-
| width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE
| width="90%" | Size
|-
| colspan="2" | Byte count the Payload buffer 
|-
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE
| width="90%" style="border-top: 2px solid #000;" | ''Return''
| width="90%" style="border-top: 2px solid #000;" | ''Return''
|}
|}




{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
===Subscribe===
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Subscribe'''
| width="90%" class="mtx-class-macrohead" | '''Subscribe'''
|-
|-
| colspan="2" | Subscribe to a Topic. Must be Connected first Returns non-zero if successful  
| colspan="2" | Subscribe to a Topic. Must be Connected first Returns non-zero if successful  
Line 275: Line 366:




{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
===Unsubscribe===
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Unsubscribe'''
| width="90%" class="mtx-class-macrohead" | '''Unsubscribe'''
|-
|-
| colspan="2" | Unsubscribe from a Topic. Must be Connected first Returns non-zero if successful  
| colspan="2" | Unsubscribe from a Topic. Must be Connected first Returns non-zero if successful  
Line 293: Line 385:




{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
==Property reference==
 
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-prop-icon.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Initialise'''
| width="90%" class="mtx-class-macrohead" | '''Properties'''  
|-
|-
| colspan="2" |  
|-
|-
| width="10%" align="center" | [[File:Fc9-type-16-icon.png]]
| width="90%" | LinkTo
|-
|-
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-void-icon.png]] - VOID
| colspan="2" |  
| width="90%" style="border-top: 2px solid #000;" | ''Return''
|}
 
 
 
 
==Property reference==
 
{| class="wikitable" style="width:60%; background-color:#FFFFFF;"
|-
| width="10%" align="center" style="background-color:#D8C9D8;" | [[File:Fc9-prop-icon.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Properties'''
|-
|-
|-
| width="10%" align="center" | [[File:Fc9-type-10-icon.png]]
| width="10%" align="center" | [[File:Fc9-type-10-icon.png]]
Line 345: Line 427:
|-
|-
| colspan="2" | The Keep Alive is the maximum time interval that is permitted to elapse  between the point at which the Client finishes transmitting one Control Packet  and the point it starts sending the next, measured in seconds. 
| colspan="2" | The Keep Alive is the maximum time interval that is permitted to elapse  between the point at which the Client finishes transmitting one Control Packet  and the point it starts sending the next, measured in seconds. 
|-
| width="10%" align="center" | [[File:Fc9-type-17-icon.png]]
| width="90%" | Network Component
|-
| colspan="2" | Pointer to the network communications component providing TCP simulation and abstraction for embedded TCP modules. 
|-
|-
| width="10%" align="center" | [[File:Fc9-type-16-icon.png]]
| width="10%" align="center" | [[File:Fc9-type-16-icon.png]]
Line 355: Line 432:
|-
|-
| colspan="2" | Selects if the broker is open or if the broker requires a username and password. 
| colspan="2" | Selects if the broker is open or if the broker requires a username and password. 
|-
| width="10%" align="center" | [[File:Fc9-type-21-icon.png]]
| width="90%" | Max Packet Size
|-
| colspan="2" | Sets the number of bytes that can be sent and received in a single packet. A single packet consists of 4 header bytes, plus the topic and payload string data. 
|}
|}
==Component Source Code==
Please click here to download the component source project: [https://www.flowcode.co.uk/wiki/componentsource/FC_Comp_Source_MQTT_Client.fcfx FC_Comp_Source_MQTT_Client.fcfx]
Please click here to view the component source code (Beta): [https://www.flowcode.co.uk/FlowchartView/?wfile=componentsource/FC_Comp_Source_MQTT_Client.fcfx FC_Comp_Source_MQTT_Client.fcfx]

Latest revision as of 14:23, 13 July 2026

Author Matrix TSL
Version 1.1
Category Comms: IoT/Cloud


MQTT Client component

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

Version information

Library Version, Component Version, Date, Author, Info
7, 1.1, 23-07-24, BR, Added Max Packet Size property to allow the size of a single packet to be altered by the user
8, 1.1, 30-07-24, BR, Resolved ESP compile error due to missing return and added TopicTooLong error

Detailed description

No detailed description exists yet for this component

Examples

The MQTT component requires connection to a Network Comms component via the "Network Component" property, to provide the bridge for simulated TCP/IP communication.

The Network Comms component can then be linked to an embedded TCP/IP component to provide embedded TCP/IP support.

Here is a typical setup for using MQTT on a Raspberry Pi.


Local Weather

A simple example demonstrating how to use MQTT with Flowcode SCADA to get the local weather conditions.

The example shows temperatures from Halifax in the UK and from Sydney in Australia.

SCADA BBC TEMPERATURES


Publish

Raspberry Pi

Here we have an embedded Raspberry Pi project connected to an I2C Sensor, the sensor is sampled and the data is published to a MQTT server.

RPI3 MQTT IOT EBM016


ESP8266 Module

Here we have an embedded project using an ESP8266 wifi module, an analogue input is sampled and the data is published to a MQTT server.

MQTT IOT ESP8266


Subscribe

ESP8266 Module

Here we subscribe to a topic and receive it back into Flowcode embedded using an ESP8266 module.

Subscribe ESP8266 MQTT


Flowcode App Developer

Here we subscribe to a topic and receive it back into Flowcode App Developer.

Subscribe App Developer MQTT


Making your own Raspberry Pi based MQTT Broker

Click here for instructions on creating your own Raspberry Pi MQTT Broker.








Macro reference

Connect

Connect
Connect to the MQTT Server. Returns the connected state. 0 if not connected, 1 if successful connection, or already connected.  
- BYTE Return


ConnectTo

ConnectTo
Connect to a variable MQTT Server, overriding the Host and Port properties. Returns the connected state. 0 if not connected, 1 if successful connection, or already connected. 
- STRING Host
 
- UINT Port
 
- STRING ClientID
Client identifier name - must be unique on server 
- BYTE Return


Disconnect

Disconnect
Disconnects from the MQTT server and the network 
- BYTE Return


ErrorAsString

ErrorAsString
 
- BYTE Error
 
- STRING Return


GetPayload

GetPayload
Get the Payload of the last received (Read) packet. Returns the number of bytes for the Payload 
- STRING Payload
 
- BYTE Size
Byte count the Payload buffer 
- BYTE Return


GetTopic

GetTopic
Get the Topic of the last received (Read) packet. Returns the number of bytes for the Topic 
- STRING Topic
 
- BYTE Size
Byte count the Topic buffer 
- BYTE Return


Initialise

Initialise
 
- VOID Return


LastError

LastError
Returns the last error 
- BYTE Return


Ping

Ping
Ping packet is sent to the Server and checks for a reply. Returns non-zero value if successful  
- BYTE Return


Publish

Publish
Publish data to the server (broker) Returns non-zero if successful 
- STRING Topic
 
- STRING Payload
 
- BYTE Return


Read

Read
Read incoming data. Returns non-zero if packet received. Use GetTopic and GetPayload to read the contents. 
- UINT Return


Subscribe

Subscribe
Subscribe to a Topic. Must be Connected first Returns non-zero if successful  
- STRING Topic
 
- BYTE Return


Unsubscribe

Unsubscribe
Unsubscribe from a Topic. Must be Connected first Returns non-zero if successful  
- STRING Topic
 
- BYTE Return


Property reference

Properties
LinkTo
 
Host
Address of MQTT Broker Can be an IP Address or URL (URL only supported by some hardware platforms) 
Port
Port used by MQTT Broker Default 1883 for unencrypted access and 8883 for encrypted access. 
Client identifier
Client identifier (Maximum length 20 characters) Must be unique to the broker. 
Network Timeout
Maximum time in mS to wait for network responses 
Read Timeout
Amount of time in ms to wait for incoming messages. 
Keep Alive
The Keep Alive is the maximum time interval that is permitted to elapse between the point at which the Client finishes transmitting one Control Packet and the point it starts sending the next, measured in seconds. 
Authentication
Selects if the broker is open or if the broker requires a username and password. 
Max Packet Size
Sets the number of bytes that can be sent and received in a single packet. A single packet consists of 4 header bytes, plus the topic and payload string data. 

Component Source Code

Please click here to download the component source project: FC_Comp_Source_MQTT_Client.fcfx

Please click here to view the component source code (Beta): FC_Comp_Source_MQTT_Client.fcfx