0

I've noticed a lot of payload data encoded as Base64 before transmission in many IoT use cases. Particularly in LPWAN (LoRa, LTE-M, NBIoT Sigfox, etc). For simplicity sake, sending JSON payloads makes a lot of sense. Also it's my understanding that Base64 encoding adds some additional weight to the payload size, which for low bandwidth use cases it seems counter intuitive.

Could someone explain the benefits of using Base64 in IoT (or otherwise) applications? Thanks!

Don T Spamme
  • 199
  • 7
  • 1
    lazy people who implement server-side prefer to use adopted data transfer standards to minimize development time. Also quite often developers off-load data decoding to the server so IoT node needs to send raw bytes from sensor, wMbus receiver or whatever. raw binary data is incompatible with adopted (json/xml/etc) standard so raw data gets converted into the format which is safe to use with clear text payload despite increase of payload size. When you need to preserve bandwidth you usually pick some binary data representation format, i.e. protobuf, flatbuffers, etc – Maxim Sagaydachny Sep 06 '21 at 18:11

1 Answers1

0

Well, base64 is generally used to encode binary formats. Since binary is the native representation of data in a computer, it is obviously the easiest format for resource-constrained embedded devices to handle. It's also reasonably compact.

As an algorithm, base64 is a fairly simple conceptually and requires very few resources to implement, so it's a good compromise for squeezing binary data through a text-only channel. Building a JSON record, on the other hand, typically requires a JSON library which consumes RAM and code space - not horribly much, but still more than base64.

Not to mention that the data channels you've mentioned are rather starved for bandwidth. E.g. public LoRaWAN deployments are notorious for permitting a device to send a few dozen bytes of data a few dozen times per day.

If I want to encode a data record consisting of, say a 32-bit timestamp, an 8-bit code specifying the type of data (i.e. temperature, voltage or pressure) and 32-bit data sample:

struct {
    time_t time;
    uint8_t type;
    uint32_t value;
}

This will use 9 bytes. It grows to around 12 bytes after being encoded with base64.

Compare that with a simple JSON record which is 67 bytes after leaving out all whitespace:

{
    "time": "2012-04-23T18:25:43.511Z",
    "type": "temp",
    "value": 26.94
}

So 12 B or 67 B - not much competition for bandwidth starved data channels. On a LoRaWAN link that could make the different between squeezing into your precious uplink slot 5-6 data records or 1 data record.

Regarding data compression - on a resource constrained embedded device it's much, much more practical to encode data as compact binary instead of transforming it into a verbose format and compressing that.

Tarmo
  • 3,728
  • 1
  • 8
  • 25