Example of using ingestion API with binary payload?

I’ve had success using the ingestion API to post a small wav file with Python and JSON. I’d like to take the next step and use the binary payload option defined in the docs. However, I’m getting tripped up on steps 3 and 4:

3. Write the CBOR header. Note that this needs to be a valid CBOR structure, *and* that it requires termination with `0xFF`. The easiest to achieve this is by writing the `values` field as an indefinite length array (which needs to be terminated by `0xFF`.
4. Write your payload (little endian).
  * If you need to align your writes the easiest is to pad the `Ref-Binary-i16` string with spaces on the right.

By “write the CBOR header” am I encoding the data object from the sample code with a cbor library? And then literally appending the binary wav file to it?

I learn best by copying other people’s code :slight_smile: so if anyone has a code sample of binary payload usage (preferably in Python or JavaScript) that would be amazing.

Hi @roblauer, there has been a similar discussion here Universal header+binary payload data generator and at the bottom of the thread there is an example in: https://github.com/edgeimpulse/linux-sdk-python/blob/master/examples/custom/collect.py
Does this help?

Hi @roblauer, if you look at the example string in the docs:

a36970726f746563746564a26376657262763163616c67654853323536697369676e6174757265784038346432316234343665323831383561356336303761396437656134636336333735356137363261666164643036343334613136353766623433386237656630677061796c6f6164a56b6465766963655f6e616d657143343a37463a35313a38443a31383a34416b6465766963655f7479706573444953434f5f4c34373556475f494f543031416b696e74657276616c5f6d73f92c006773656e736f727381a2646e616d6565617564696f65756e697473637761766676616c7565739f6e5265662d42494e4152592d693136fff918fa18e718de18e218dd18e118e218f218f018f918f418e918ee18f618f918fc180919101915191119fe18f6180b190e190a19021912191419fc18f218f718

And you throw that through CBOR.me you’ll see the structure of the data:

A3                                      # map(3)
   69                                   # text(9)
      70726F746563746564                # "protected"
   A2                                   # map(2)
      63                                # text(3)
         766572                         # "ver"
      62                                # text(2)
         7631                           # "v1"
      63                                # text(3)
         616C67                         # "alg"
      65                                # text(5)
         4853323536                     # "HS256"
   69                                   # text(9)
      7369676E6174757265                # "signature"
   78 40                                # text(64)
      38346432316234343665323831383561356336303761396437656134636336333735356137363261666164643036343334613136353766623433386237656630 # "84d21b446e28185a5c607a9d7ea4cc63755a762afadd06434a1657fb438b7ef0"
   67                                   # text(7)
      7061796C6F6164                    # "payload"
   A5                                   # map(5)
      6B                                # text(11)
         6465766963655F6E616D65         # "device_name"
      71                                # text(17)
         43343A37463A35313A38443A31383A3441 # "C4:7F:51:8D:18:4A"
      6B                                # text(11)
         6465766963655F74797065         # "device_type"
      73                                # text(19)
         444953434F5F4C34373556475F494F54303141 # "DISCO_L475VG_IOT01A"
      6B                                # text(11)
         696E74657276616C5F6D73         # "interval_ms"
      F9 2C00                           # primitive(11264)
      67                                # text(7)
         73656E736F7273                 # "sensors"
      81                                # array(1)
         A2                             # map(2)
            64                          # text(4)
               6E616D65                 # "name"
            65                          # text(5)
               617564696F               # "audio"
            65                          # text(5)
               756E697473               # "units"
            63                          # text(3)
               776176                   # "wav"
      66                                # text(6)
         76616C756573                   # "values"
      9F                                # array(*)
         6E                             # text(14)
            5265662D42494E4152592D693136 # "Ref-BINARY-i16"
         FF                             # primitive(*)


##### 64 unused bytes after the end of the data item:

F9 18 FA 18 E7 18 DE 18 E2 18 DD 18 E1 18 E2 18 F2 18 F0 18 F9 18 F4 18 E9 18 EE 18 F6 18 F9 18 FC 18 09 19 10 19 15 19 11 19 FE 18 F6 18 0B 19 0E 19 0A 19 02 19 12 19 14 19 FC 18 F2 18 F7 18

The 64 unused bytes are the binary payload.

If you’re on a Linux system (I guess so as you’re referencing Python / JS) you can skip the binary payload and just stick audio data into the normal values array. This is really done for embedded systems where unaligned writes are expensive.

This all helps, thanks!