OpenMQTTGateway icon indicating copy to clipboard operation
OpenMQTTGateway copied to clipboard

[FR] LYWSD02 NTP clock sync

Open nonflammable opened this issue 2 years ago • 5 comments

Feature request: LYWSD02 internal clock is not precise. After few weeks time can be shitf over minute. A good solution would be to synchronize the time with a NTP server via OMG.

nonflammable avatar Oct 29 '22 19:10 nonflammable

Good catch, for info Theengs Gateway does it automatically thanks to @koenvervloesem. https://github.com/theengs/gateway

But agreed, could be considered with OMG

1technophile avatar Oct 29 '22 19:10 1technophile

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Oct 06 '23 00:10 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Oct 21 '23 00:10 github-actions[bot]

I'm able to do this using the commands/MQTTtoBT topic with a payload like this

{
  "ble_write_address":"[[LYWSD02 MAC ADDRESS]]",
  "ble_write_service":"ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6",
  "ble_write_char":"ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6",
  "ble_write_value":"09c93f6501",
  "value_type":"HEX",
  "immediate":true
}

I'm calculating the ble_write_value using this JS code in my browser console

(() => {
  const timeZone = 1; // CET
  const buffer = new ArrayBuffer(5);
  new DataView(buffer).setUint32(0, Math.floor(Date.now()/1000), true);
  new DataView(buffer).setUint8(4, timeZone);
  return [...new Uint8Array(buffer)]
    .map(x => x.toString(16).padStart(2, '0'))
    .join('')
})();

So, theoretically it should be possible to add this

  1. in main/ZgatewayBT.ino, as a GATT characteristic write right after reading temperature/humidity (which seems too frequent, and seems to drain the battery much quicker), or
  2. as a button using createDiscovery("button" in main/ZmqttDiscovery.ino to let users press a button to trigger the sync via mqtt, or
  3. somehow add keep track of all clocks that need syncing, and then sync them in syncNTP once every day or so.

netroy avatar Oct 30 '23 15:10 netroy

bash version:

timestamp=`date +%s|xargs printf %x`
czas=$(printf %s "$timestamp" | dd conv=swab 2> /dev/null | rev)

mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT -m '{
  "ble_write_address":"AA:BB:CC:DD:EE:FF",
  "ble_write_service":"ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6",
  "ble_write_char":"ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6",
  "ble_write_value":"'$czas'01",
  "value_type":"HEX",
  "immediate":true
}'

be sure to change:

MQTT TOPIC: mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT

MAC: "ble_write_address":"AA:BB:CC:DD:EE:FF"

TIMEZONE: "ble_write_value":"'$czas'01" 01=CET

nonflammable avatar Dec 29 '23 18:12 nonflammable