MQTT_STATUS (n) | (only available on Wx PLC firmware >= F94.2) |
Purpose | Return the status of MQTT publish, subscribe, and connection events, as well as the status of a MQTT client connection to a broker (server). | ||||||
Details | There are two valid MQTT_STATUS parameter
values; ie. 1 (for event status) and 2 (for connection status). The value returned by MQTT_STATUS(1) or MQTT_STATUS(2) is an integer that could negative, zero, or positive. Each return value corresponds to the state and circumstances of a MQTT event or connection. The possible return values for these MQTT_STATUS function parameter values are specified below.
When making a connection via MQTT_CONNECT, a callback function is specified that can be programmed to handle various MQTT events and status updates. Callback function is invoked in the following events:
Successful MQTT_DISCONNECT, MQTT_PUBLISH, MQTT_SUBSCRIBE and MQTT_UNSUBSCRIBE. These commands briefly block the PLC until completion or until an error occurred. Only MQTT_STATUS(1) is updated, and MQTT_STATUS(2) is NOT affected. Command successfully completed:
Note: DISCONNECT is not the same as LOST_CONNECTION, the former is voluntary termination of connection and will not result in auto-reconnection. The latter is an involuntary loss of connection. MQTT_STATUS(1)= 2 only after a voluntary MQTT_DISCONNECT command. Command failed: The following are values returned by MQTT_STATUS(1) after the command is run: MQTT_DISCONNECT failed: : -2 MQTT_PUBLISH failed : -3 MQTT_SUBSCRIBE failed : -4 MQTT_UNSUBSCRIBE failed : -5 Note that the only time when MQTT_STATUS(1) = -1 is when connection failed or if there is a lost connection, and this is handled in the callback function. |
||||||
Examples | '
Here we use DM[1] and DM[2] to store the MQTT statuses for easy
online viewing. ' You can define other memory location for this purpose #DEFINELOCAL EVENT_TYPE = DM[1] #DEFINELOCAL NETWORK_STATUS = DM[2] #DEFINELOCAL RECEIVE_COUNTER = DM[3] #DEFINELOCAL MQTT_STATUS_MSG_ARRIVED = 10 #DEFINELOCAL MQTT_LOST_CONNECTION = -1 #DEFINELOCAL MQTT_CONNECTED = 1 #DEFINELOCAL MQTT_RECONNECTED = 2 EVENT_TYPE = MQTT_STATUS(1) NETWORK_STATUS = MQTT_STATUS(2) IF EVENT_TYPE = MQTT_STATUS_MSG_ARRIVED ' message of a subscribed topic arrived A$ = MQTT_TOPIC$(1) B$ = MQTT_DATA$(1) ' send message to "System & User Log" PRINT #7 "MQTT Topic="+ A$ PRINT #7 "MQTT Data=" + B$ RECEIVE_COUNTER = RECEIVE_COUNTER + 1 DRAW_TEXTPOS 0,12,"Received #" + STR$(RECEIVE_COUNTER)+" " RETURN ELSE IF EVENT_TYPE < 0 ' ERROR in previous operations SETIO rMQTT_Err ERROR_COUNT = ERROR_COUNT + 1 ELSE CLRIO rMQTT_Err ENDIF ' Now check the network connection status. It is either "not connected", "lost connection", "connected" or ' "reconnected". These 4 statuses are mutually exclusive '-------------------------------------------------------------------------------------------------------- CLRIO rLostConnect CLRIO rConnected CLRIO rReconnected ' Connection lost event. IF NETWORK_STATUS = MQTT_LOST_CONNECTION SETIO rLostConnect ' Normal connection event. ELIF NETWORK_STATUS = MQTT_CONNECTED SETIO rConnected ' Reconnection event occurred. Here we indicate it as a separate event from rConnected although you could ' combine the two events into a single rConnected instruction. Separating the two events give you a chance ' to handle "connected" and "re-connected" events differently if you wish. ELIF NETWORK_STATUS = MQTT_RECONNECTED SETIO rReConnected ENDIF ENDIF |
||||||
See Also | MQTT_CONNECT, MQTT_DISCONNECT, MQTT_PUBLISH, MQTT_SUBSCRIBE, MQTT_UNSUBSCRIBE |