Pushing data to Bytebeam
The previous section shown us how to connect to bytebeam. It is now time to start pushing some data.
All communication with Bytebeam happens using JSON. On Bytebeam data is organised into Streams. Each stream is mapped to an MQTT topic. Go through the Creating a Stream guide to create a stream. Every project in bytebeam comes with a default stream called Device Shadow. We will be illustring how to push data using this stream.
To push data to device shadow you need to publish data to the MQTT topic:
/tenants/{tenant_id}/devices/{device_id}/events/device_shadow/jsonarray.
Replace {tenant_id} with your project name and {device_id} with the device id. You can find these in the JSON file downloaded in the previous section. To push data to a different stream replace device_shadow in the path above with the name of the stream.
The payload shoud look like this:
Each packet consists of an array of data points. Each data point consists of sequence, timestamp and all the other fields that the stream has. sequence is an auto incrementing number for each packet. timestamp is the timestamp in milliseconds. If you have created a custom stream replace Status with the fields from that stream. Field names are case sensitive.
You can try publishing data using MQTTX as shown below. Notice the QoS1 setting:
You should see the Status change on Bytebeam now:
Follow the below steps for implementing data push:
- Programatically construct the json structure as discussed above. Ensure the timestamp and sequence fields are present and timestamp is in milliseconds.
- Publish the data to the topic as show above.
- Optionally implement batching. In the payload the server accepts an batch of data as a JSON array. If your client is expected to generate a large amount of data it will be more efficient to batch data and send it to the cloud instead of sending a single packet with each message.
- Optionally implement compression. If you are sending larger batches of data you can save on data size by sending zlib or lz4 compressed data to /tenants/{tenant_id}/devices/{device_id}/events/device_shadow/jsonarray/zlibdeflate or /tenants/{tenant_id}/devices/{device_id}/events/device_shadow/jsonarray/lz4 respectively. Notice the zlibdeflate and lz4 at the end of the topics.
- Optionally implement persistence. If you are likely to have frequent periods of network disconnections it is useful to batch data in memory or on disk during periods of no network and then publish this once the connection is re-established.