I want to convert a base64 value to hexadecimal in node red.Is there any way?
Use the node-red-node-base64 node
could u please tell how base64 node can help me acheive hexadecimal conversion?
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;
}
Payload Structure For Ur Refrence
15|690x450
That is what the base64 node is doing
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.
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;
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":""}]