Converting Base 64 to Hexadecimal

The base 64 node will convert from base64 back to a buffer which is made of hex characters. When you say hex how do you want them to appear? Do you mean you just want to print them out as “xA9” etc ?

this is showing me array of values as output.what i require is base 64 to hexadecimal conversion first step.And after that hexadecimal to json conversion second step.Final Result value of temperature and humidity on gauge in form of dashboard.

Decoder function used in TTN(ThingsNetwork)
// T1: Payload Decoder
function Decoder(bytes, port) {
var decoded={};
for(i=0;i< bytes.length;){
//BATTERY
if(bytes[i]==0x03){
decoded.battery=bytes[i+2];
i+=3;
continue;
}
//TEMPERATURE
if(bytes[i]==0x01){
decoded.temperature=(readInt16LE(bytes.slice(i+2, i+4)))/10;
i+=4;
continue;
}
//HUMIDITY
if(bytes[i]==0x02){
decoded.humidity=readUInt8LE(bytes[i+2]) / 2;
i+=3;
continue;
}
}
return decoded;
}

function readUInt8LE(bytes) {
return (bytes & 0xFF);
}

function readInt8LE(bytes) {
var ref = readUInt8LE(bytes);
return (ref > 0x7F) ? ref - 0x100 : ref;
}

function readUInt16LE(bytes) {
var value = (bytes[1] << 8) + bytes[0];
return (value & 0xFFFF);
}

function readInt16LE(bytes) {
var ref = readUInt16LE(bytes);
return (ref > 0x7FFF) ? ref - 0x10000 : ref;
}

.This payload is coming from uc11-t1 temperature sensor and uplinking to ug85 ursalink lorawan gateway.

Payload Structure For Ur Refrence
15|690x450

That is what the base64 node is doing

it is giving me array of values.how do i proceed further pls guide?

I know but it is not showing desired output as hexadecimal what we want

If you click on the values in the debug it will show them to you in different formats - they are as you want - you just need to feed into your decoder.

image


image

Here is the code for decoder function
function Decoder(bytes, port) {
if(bytes[0]==01){
var temperature = (bytes[3] << 8) + bytes[2];
var humidity= bytes[6];
return {
temperature: temperature/10, humidity: humidity/2
}
}
else{
var power=bytes[2];
return{
power: power
}
}
}



After pasting ur code it is showing red sign on function node that means this snippet of code is not proper.please dont close my post as it is not resolved on my end.

Here are the docs you need to help

here is the first part

var bytes = msg.payload;
if (bytes[0]==01) {
    var temperature = (bytes[3] << 8) + bytes[2];
    var humidity= bytes[6];
    return { 
        payload: {
            temperature: temperature/10, 
            humidity: humidity/2
        }
    }
}
// else {
//     var power=bytes[2];
//     return {
//         power: power
//     }
// }

return msg;

now it is same as your code but with a minor change in if statement.that helps in deploying

then please show what you have now got in the function as I can't see from here.
I'm not closing this thread - I'm closing all the other ones you are opening that ask the same as this one.

ok go ahead and close but my issue will not be solved.

I'm not going to close this here - this is where we are discussing it... the function works fine as I tested it before giving it to you... please show that you have tested it as is.

Here is my complete test flow -

[{"id":"8ad0ef68.15c8","type":"mqtt in","z":"e9b4dddd.70115","name":"","topic":"bachloo.rahul@gmail.com/test/#","qos":"0","datatype":"auto","broker":"c44802ed.2f5f7","x":150,"y":760,"wires":[["e0f57bd6.3cf268"]]},{"id":"e0f57bd6.3cf268","type":"base64","z":"e9b4dddd.70115","name":"","action":"","property":"payload","x":400,"y":760,"wires":[["bebca09d.a0895","e5854f65.e7aed"]]},{"id":"92dc4345.5331f","type":"inject","z":"e9b4dddd.70115","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"AWcwAQJoaQ==","payloadType":"str","x":140,"y":800,"wires":[["e0f57bd6.3cf268"]]},{"id":"bebca09d.a0895","type":"debug","z":"e9b4dddd.70115","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":760,"wires":[]},{"id":"e5854f65.e7aed","type":"function","z":"e9b4dddd.70115","name":"","func":"var bytes = msg.payload;\nif (bytes[0]==01) {\n    var temperature = (bytes[3] << 8) + bytes[2];\n    var humidity= bytes[6];\n    return { \n        payload: {\n            temperature: temperature/10, \n            humidity: humidity/2\n        }\n    }\n}\nelse {\n    var power=bytes[2];\n    return {\n        payload: { power: power }\n    }\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":800,"wires":[["1292b759.873b89"]]},{"id":"1292b759.873b89","type":"debug","z":"e9b4dddd.70115","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":800,"wires":[]},{"id":"c44802ed.2f5f7","type":"mqtt-broker","z":"","name":"","broker":"maqiatto.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]


This is when i pasted your code it shows no output in debug window as well as red traingle sign on function node.

I don't see the first function node when I import Dave's flow. Where did it come from??

@dceejay When I first import the flow, the function node is fine, but if I open it and close it without making a change, the red triangle shows up. the cause it the first if statement
if (bytes[0]==01) {
If I change the 01 to 1 then the configuration error goes away, changing it back to 01 and the issue pops up again.

1 Like

Yes I tested your complete flow it works well.Thank you for the support,really appreciate it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.