Base64 encoding and decoding

Hello,

I’m a new user and I’m registering a new device. My device is connected through Everynet (ATC Brazil), and the payload is encoding in Base64.

How can I do the parse in my payload to view my variables?

Below an exemple about my frame:

payload (Base64): AQAWAgAABQMAxYo=
bytes: 01 00 16 02 00 00 05 03 00 C4 03

In this frame I have three variables, each one is identified by an ALIAS (01, 02 and 03):
variable 1: 01 (ALIAS) 00 16 (data)
variable 2: 02 (ALIAS) 00 00 05 (data)
variable 3: 03 (ALIAS) 00 C4 03 (data)

Thanks
Ronaldo Vieira

Hi, @roniolivi

Have you tried to find your sensor in the Add Device list? If you didn’t found it, you’ll need to use your own payload parser. To do this, you must access your device, go to the Payload Parser tab and activate the Run your own parser option.

After that, you can code your own payload parser. To do so, you can check more details on the docs:

I hope it can help you, let me know if you have any more questions.

Thanks!

Hi @kelvinwelter

Yes I can see my device in the list.

What language should I use to code payload parser?

Tks

Hi @roniolivi, you should use Node.js to code your own payload parser.

Thanks!

@kelvinwelter

I did the code below:

const valuesBase64 = payload.find(item => item.variable === 'values64');

let buff = Buffer.from(values64, 'base64');  
let text = buff.toString('utf-8');

console.log(text);   

Now variable “texte” should appear on my dashboard, right?
I searched the list of variables and I didn’t find

1 Like

Hi @roniolivi

Try this:

const valuesBase64 = payload.find(item => item.variable === 'values64');

let buff = Buffer.from(valuesBase64.value, 'base64');  
let text = buff.toString('utf-8');

console.log(text);   

payload.push({ variable: 'text', value: text });

This should work. What you had forgotten is that you need to insert your variable in the payload that is being processed, if you don’t insert it, everything you did with your variable is discarded at the end of the script.

Also, your variable valuesBase64 wasn’t being used after your declaration, I think it wasn’t on purpose.

Thanks!

@kelvinwelter

After that variable “text” should be available to be used on my dashboard, right?

Hi @roniolivi,
Yes, the code will result in the "text"variable in your device, which you can use in the dashboards.

The console.log in your code will output to the Live Inspector in your device page. When using the Payload Parser, I always recommend you to check the Live Inspector for logs and errors.

Hi @vitor

I’m creating a dashboard, but that variable does not appear in the “list of variables” in “main configuration”.

Only variables like counter_up, duplicate, encrypetd_payload, method, etc are available

Tks

Sorry guys

I wrote it wrong, my code was missing a few ’ ’

Now I can see my variables on dashboard. The value still strange, I think the problem is the conversion Base64 to String

Hum… That’s strange. If it is base64, it should be parsed correctly.

I can’t help you more without knowing what is being sent and what is the expected result. Can you get the value of your payload and post here?
I tried to use https://www.base64decode.org/ in the payload of your first post, but it doesn’t give me any readable result.

Also be in mind if you are using Custom Everynet, TagoIO automatically converts all payloads to hexadecimal. It’s the same for any official connector. That’s because TagoIO uses hexadecimal for all parsers and data decoding.

hi @vitor

It’s working, I made a mistake. I tested this code on the emulator it worked:

‘use strict’;

const valuesBase64 = payload.find(item => item.variable === ‘payload’);
let buff = new Buffer(valuesBase64.value, ‘base64’);
let valuesHex = buff.toString(‘hex’);

console.log(valuesHex);

Tks

@vitor

Taking advantage of the topic, in my payload there are 3 variables, I already separated them using the slice function and I can see the value of each one in the dashboard.

Can I set an initial value for these variables? Like a offset

Not sure what you mean by initial value.

You can use the emulator to insert initial values on the variables before your device even send any data.

Also you have access to the Configuration Parameters through the payload parser. Just use console.log(device) in your payload parser to check all the device parameters you have access to.

@vitor

In my application the sensor have a initial value like a car hodometer. I solved this subtracting this value of my variable in payload parser.

Thank you

1 Like