Queue and Timestamp

obniz Product Manual: Transmission Queue and Clock Settings

Target Version: OS7 and later

Normally, data is transmitted to the internet in real-time. However, by enabling the Transmission Queue, you can achieve the following:

  • Reduce Packet Usage: Group data together and send it at intervals rather than sending each piece individually.
  • Accurate Timekeeping: Attach timestamp information to all outgoing data to track precise timing.
  • Offline Data Buffering: Store data received during offline periods and transmit it once the connection is restored.

obniz.setQueueMode({ timestamp, interval })

By specifying a value greater than 0 for the interval, you can switch from immediate transmission to queued transmission.

obnizOS maintains an internal transmission queue. While it attempts to send data as quickly as possible by default, if you set an interval (e.g., 10 seconds), the system will buffer the data and only send it every 10 seconds or when the queue becomes full.

Note: The Queue handles all data sent to the cloud, including command responses. Please be aware that enabling this may cause unexpected latency in various operations.

When using the Queue, it can be difficult to determine exactly when the data was generated on the device side. By specifying the timestamp option, you can attach time information to each piece of data. This timestamp can be retrieved on the receiving end (e.g., in your application) via obniz.deviceTimestamp. Regardless of whether you choose seconds or milliseconds for the setting, obniz.deviceTimestamp will provide a Unix timestamp (in milliseconds), similar to Date.now().

Note: obniz.deviceTimestamp is only valid at the moment the data is received. If there is a delay due to async/await or other processing, it may be overwritten by the timestamp of the next incoming data.

Parameters

Key Type Default Value
timestamp string 'none' Choose from 'none', 'unix_seconds', or 'unix_milliseconds'. Selecting anything other than none adds a timestamp to each packet. unix_seconds adds 4 bytes long date information; unix_milliseconds adds 8 bytes long date information
interval number 0 Transmission interval in milliseconds.

Javascript Example

// Example: Switching between immediate and queued transmission
  
  // 1. Set current time to obniz
  obniz.setClock(); 
  
  // 2. Default mode: No delay, no timestamp
  obniz.setQueueMode({
    timestamp: 'none',
    interval: 0 
  });

  await obniz.ble!.initWait();
  obniz.ble!.scan.onfind = (peripheral) => {
    // Access the timestamp of the received data
    console.log("Data Timestamp:", obniz.deviceTimestamp);
    console.log("Device Address:", peripheral.address);
  }
  await obniz.ble!.scan.startWait(null, { duration: null, duplicate: true });

  // 3. Queue mode: Buffer data for 10 seconds with second-level timestamps
  obniz.setQueueMode({
    timestamp: 'unix_seconds',
    interval: 10 * 1000 // Data is stored for up to 10 seconds or until the buffer is full
  });

obniz.setClock()

Since the device's internal clock does not synchronize automatically, you must manually send the current time information. This is required if you wish to use accurate timestamps.

You can synchronize the clock using either of the following methods:

  • Call obniz.pingWait()
  • Call obniz.setClock()

Both methods will send the current system time to the device and adjust its internal clock.

Javascript Example

// Synchronize the device clock with the current system time
await obniz.setClock();