python-3.xsynchronizationcloudmqttetcd3

How to keep information synchronized between the cloud and the device


I'm coding a project which needs cloud control device operation, and want to keep information in sync.

The cloud needs to know the state of device, such as when the network is interrupted and when the network is restored.

When the network is restored, the modified information on the cloud is synchronized to device.

anyone got an idea of how my approach should be like? any tips?

I intend to add resident programs in the background at both ends to determine, but in fact, it is impossible for the cloud in the project to connect only one device, and multiple apps may run in one device, which is very tedious to do. Is there any simple component to realize this function?

I wish control information and data information to be synchronized on the cloud and device


Solution

  • Based on your tag, I'm assuming that you are using MQTT as a messaging protocol for your system. If so, to address your need for tracking the device-cloud connection state, MQTT specifies a feature called "Last Will and Testament".

    From the MQTT 3.1.1 Standard Section 3.1.2.5:

    If the Will Flag is set to 1 this indicates that, if the Connect request is accepted, a Will Message MUST be stored on the Server and associated with the Network Connection. The Will Message MUST be published when the Network Connection is subsequently closed unless the Will Message has been deleted by the Server on receipt of a DISCONNECT Packet [MQTT-3.1.2-8].

    This can be leveraged to let the remote MQTT client on the cloud know when the device is connected and when it disconnects by publishing an online payload to a topic (for example) device/conn_status after a successful connection, and registering a Last Will offline message to the same topic. Now, whenever the device client goes offline, the broker will publish the offline payload on his behalf to the cloud client that can now act accordingly.