So far, so good - but every so often the connection from the actuator board to the MQTT test server disconnects from the network. This state can be detected when a PINGREQ no longer receives an acknowledgement. In the last episode, we indicated this by switching a red LED on. By pressing the pushbutton, the user can then ensure that the actuator client logs on to the test server again and subscribes to the topic.
The whole thing is of course not very practical, if the actuator is situated somewhere in the field. When you discover on the controller client side of the MQTT connection that acknowledgements are not being received you physically need to go out and reset the actuator board.
A reconnect can however also be initiated automatically; In the Arduino software from the last sequence, all the functions are already available. What I've improved for this installment can be seen by comparing the new sketch (download below) with the sketch from the last episode.
MQTT State MachineI have introduced some definitions for a "NodeState" (see screenshot). Positive numbers represent normal operation, 0 and negative values indicate an error. The functions for connecting and subscribing to the topic (ConnectAndSubscribeToTopic ()) and sending the ping (SendPingRequestToBroker ()) now not only switches the LED red, but also returns a negative value to the main loop. A small state machine has been implemented. Based on the returned value, a variable MQTTClient_Connected is served which only has two states: Namely true if the connection attempt and the corresponding ping were successful and the connection is (still) valid and false if an error has occurred. In the former case the board responds as in the last sequence: A ping is sent regularly and listens to switching commands sent via MQTT. When a command is received the actuator is switched accordingly and a feedback message is published via MQTT.
If, however, MQTTClient_Connected == is false, the connectionAndSubscribeToTopic () function is used to initiate a (renewed) connection attempt. MQTTClient_Connected is also set to false at the beginning of the program so that the first connection attempt occurs immediately after logging into the Wi-Fi network and not when the user first presses the button. Pressing the button again forces a reconnect.
Status messaging using MQTTSo much of this is automatic, the board also generates status messages, locally sent via the serial interface (which is useful when setting up in the field), as well as via MQTT (on the same topic as the actuator feedback). In the event of an error occurring, it is not possible to send status messages via MQTT, so the error is buffered in the NodeStateLog variable and the corresponding message is sent when the connection is restored. Status messages are "S1" to "S3" for normal operation and "E0" to "E4" for errors.
Try it out - the setup is the same as in the last episode. In addition to the Arduino code, where you always have to enter the SSID and the password of your Wi-Fi network, the MQTT client for the PC is also included in the download. The actuator board now also operates for an extended period without external intervention; you can look at the serial output to see that the connection is automatically re-established from time to time (the PC client still needs a restart now and again). I think we can put the unreliable connection down to the fact that we are (still) using a publicly accessible test broker.
There is still much to do, standby for the next installment!