Problem with decoding Hex string

Hello,

I am kinda new to this and can not write code.

My problem is that I can't decode a Hex String that I am getting from my RakWireless gateway.

It is the data (in this case: 0100E00216070E2314000F9392) that I can't convert.

The node is from Elsys and I have tried their Generic Javascript decoder in a Function node with no luck.
https://www.elsys.se/en/elsys-payload/

This is how it should look:
{
"temperature": 22.4,
"humidity": 22,
"vdd": 3619,
"pressure": 1020.818
}

I am using Node Red on a Raspberry Pi4 with Home Assistant.

Thank you in advance for looking into my problem.

Using the Javascript provide by the link here is an example

[{"id":"5d7d2fce.6e09a8","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0100E00216070E2314000F9392","payloadType":"str","x":160,"y":1880,"wires":[["1f84bba1.10f32c"]]},{"id":"1f84bba1.10f32c","type":"function","z":"c791cbc0.84f648","name":"","func":"        /*\n  ______ _       _______     _______ \n |  ____| |     / ____\\ \\   / / ____|\n | |__  | |    | (___  \\ \\_/ / (___  \n |  __| | |     \\___ \\  \\   / \\___ \\ \n | |____| |____ ____) |  | |  ____) |\n |______|______|_____/   |_| |_____/ \n \n  ELSYS simple payload decoder. \n  Use it as it is or remove the bugs :)\n  www.elsys.se\n  peter@elsys.se\n*/\nconst TYPE_TEMP = 0x01; //temp 2 bytes -3276.8°C -->3276.7°C\nconst TYPE_RH = 0x02; //Humidity 1 byte  0-100%\nconst TYPE_ACC = 0x03; //acceleration 3 bytes X,Y,Z -128 --> 127 +/-63=1G\nconst TYPE_LIGHT = 0x04; //Light 2 bytes 0-->65535 Lux\nconst TYPE_MOTION = 0x05; //No of motion 1 byte  0-255\nconst TYPE_CO2 = 0x06; //Co2 2 bytes 0-65535 ppm\nconst TYPE_VDD = 0x07; //VDD 2byte 0-65535mV\nconst TYPE_ANALOG1 = 0x08; //VDD 2byte 0-65535mV\nconst TYPE_GPS = 0x09; //3bytes lat 3bytes long binary\nconst TYPE_PULSE1 = 0x0A; //2bytes relative pulse count\nconst TYPE_PULSE1_ABS = 0x0B; //4bytes no 0->0xFFFFFFFF\nconst TYPE_EXT_TEMP1 = 0x0C; //2bytes -3276.5C-->3276.5C\nconst TYPE_EXT_DIGITAL = 0x0D; //1bytes value 1 or 0\nconst TYPE_EXT_DISTANCE = 0x0E; //2bytes distance in mm\nconst TYPE_ACC_MOTION = 0x0F; //1byte number of vibration/motion\nconst TYPE_IR_TEMP = 0x10; //2bytes internal temp 2bytes external temp -3276.5C-->3276.5C\nconst TYPE_OCCUPANCY = 0x11; //1byte data\nconst TYPE_WATERLEAK = 0x12; //1byte data 0-255\nconst TYPE_GRIDEYE = 0x13; //65byte temperature data 1byte ref+64byte external temp\nconst TYPE_PRESSURE = 0x14; //4byte pressure data (hPa)\nconst TYPE_SOUND = 0x15; //2byte sound data (peak/avg)\nconst TYPE_PULSE2 = 0x16; //2bytes 0-->0xFFFF\nconst TYPE_PULSE2_ABS = 0x17; //4bytes no 0->0xFFFFFFFF\nconst TYPE_ANALOG2 = 0x18; //2bytes voltage in mV\nconst TYPE_EXT_TEMP2 = 0x19; //2bytes -3276.5C-->3276.5C\nconst TYPE_EXT_DIGITAL2 = 0x1A; // 1bytes value 1 or 0\nconst TYPE_EXT_ANALOG_UV = 0x1B; // 4 bytes signed int (uV)\nconst TYPE_TVOC = 0x1C; // 2 bytes (ppb)\nconst TYPE_DEBUG = 0x3D; // 4bytes debug\n\nfunction bin16dec(bin) {\n    var num = bin & 0xFFFF;\n    if (0x8000 & num)\n        num = -(0x010000 - num);\n    return num;\n}\n\nfunction bin8dec(bin) {\n    var num = bin & 0xFF;\n    if (0x80 & num)\n        num = -(0x0100 - num);\n    return num;\n}\n\nfunction hexToBytes(hex) {\n    for (var bytes = [], c = 0; c < hex.length; c += 2)\n        bytes.push(parseInt(hex.substr(c, 2), 16));\n    return bytes;\n}\n\nfunction DecodeElsysPayload(data) {\n    var obj = new Object();\n    for (i = 0; i < data.length; i++) {\n        //console.log(data[i]);\n        switch (data[i]) {\n        case TYPE_TEMP: //Temperature\n            var temp = (data[i + 1] << 8) | (data[i + 2]);\n            temp = bin16dec(temp);\n            obj.temperature = temp / 10;\n            i += 2;\n            break\n        case TYPE_RH: //Humidity\n            var rh = (data[i + 1]);\n            obj.humidity = rh;\n            i += 1;\n            break\n        case TYPE_ACC: //Acceleration\n            obj.x = bin8dec(data[i + 1]);\n            obj.y = bin8dec(data[i + 2]);\n            obj.z = bin8dec(data[i + 3]);\n            i += 3;\n            break\n        case TYPE_LIGHT: //Light\n            obj.light = (data[i + 1] << 8) | (data[i + 2]);\n            i += 2;\n            break\n        case TYPE_MOTION: //Motion sensor(PIR)\n            obj.motion = (data[i + 1]);\n            i += 1;\n            break\n        case TYPE_CO2: //CO2\n            obj.co2 = (data[i + 1] << 8) | (data[i + 2]);\n            i += 2;\n            break\n        case TYPE_VDD: //Battery level\n            obj.vdd = (data[i + 1] << 8) | (data[i + 2]);\n            i += 2;\n            break\n        case TYPE_ANALOG1: //Analog input 1\n            obj.analog1 = (data[i + 1] << 8) | (data[i + 2]);\n            i += 2;\n            break\n        case TYPE_GPS: //gps\n            i++;\n            obj.lat = (data[i + 0] | data[i + 1] << 8 | data[i + 2] << 16 | (data[i + 2] & 0x80 ? 0xFF << 24 : 0)) / 10000;\n            obj.long = (data[i + 3] | data[i + 4] << 8 | data[i + 5] << 16 | (data[i + 5] & 0x80 ? 0xFF << 24 : 0)) / 10000;\n            i += 5;\n            break\n        case TYPE_PULSE1: //Pulse input 1\n            obj.pulse1 = (data[i + 1] << 8) | (data[i + 2]);\n            i += 2;\n            break\n        case TYPE_PULSE1_ABS: //Pulse input 1 absolute value\n            var pulseAbs = (data[i + 1] << 24) | (data[i + 2] << 16) | (data[i + 3] << 8) | (data[i + 4]);\n            obj.pulseAbs = pulseAbs;\n            i += 4;\n            break\n        case TYPE_EXT_TEMP1: //External temp\n            var temp = (data[i + 1] << 8) | (data[i + 2]);\n            temp = bin16dec(temp);\n            obj.externalTemperature = temp / 10;\n            i += 2;\n            break\n        case TYPE_EXT_DIGITAL: //Digital input\n            obj.digital = (data[i + 1]);\n            i += 1;\n            break\n        case TYPE_EXT_DISTANCE: //Distance sensor input\n            obj.distance = (data[i + 1] << 8) | (data[i + 2]);\n            i += 2;\n            break\n        case TYPE_ACC_MOTION: //Acc motion\n            obj.accMotion = (data[i + 1]);\n            i += 1;\n            break\n        case TYPE_IR_TEMP: //IR temperature\n            var iTemp = (data[i + 1] << 8) | (data[i + 2]);\n            iTemp = bin16dec(iTemp);\n            var eTemp = (data[i + 3] << 8) | (data[i + 4]);\n            eTemp = bin16dec(eTemp);\n            obj.irInternalTemperature = iTemp / 10;\n            obj.irExternalTemperature = eTemp / 10;\n            i += 4;\n            break\n        case TYPE_OCCUPANCY: //Body occupancy\n            obj.occupancy = (data[i + 1]);\n            i += 1;\n            break\n        case TYPE_WATERLEAK: //Water leak\n            obj.waterleak = (data[i + 1]);\n            i += 1;\n            break\n        case TYPE_GRIDEYE: //Grideye data\n            var ref = data[i+1];\n            i++;\n            obj.grideye = [];\n            for(var j = 0; j < 64; j++) {\n                obj.grideye[j] = ref + (data[1+i+j] / 10.0);\n            }\n            i += 64;\n            break\n        case TYPE_PRESSURE: //External Pressure\n            temp = (data[i + 1] << 24) | (data[i + 2] << 16) | (data[i + 3] << 8) | (data[i + 4]);\n            obj.pressure = temp / 1000;\n            i += 4;\n            break\n        case TYPE_SOUND: //Sound\n            obj.soundPeak = data[i + 1];\n            obj.soundAvg = data[i + 2];\n            i += 2;\n            break\n        case TYPE_PULSE2: //Pulse 2\n            obj.pulse2 = (data[i + 1] << 8) | (data[i + 2]);\n            i += 2;\n            break\n        case TYPE_PULSE2_ABS: //Pulse input 2 absolute value\n            obj.pulseAbs2 = (data[i + 1] << 24) | (data[i + 2] << 16) | (data[i + 3] << 8) | (data[i + 4]);\n            i += 4;\n            break\n        case TYPE_ANALOG2: //Analog input 2\n            obj.analog2 = (data[i + 1] << 8) | (data[i + 2]);\n            i += 2;\n            break\n        case TYPE_EXT_TEMP2: //External temp 2\n            temp = (data[i + 1] << 8) | (data[i + 2]);\n            temp = bin16dec(temp);\n            if(typeof obj.externalTemperature2 === \"number\") {\n                obj.externalTemperature2 = [obj.externalTemperature2];\n            } \n            if(typeof obj.externalTemperature2 === \"object\") {\n                obj.externalTemperature2.push(temp / 10);\n            } else {\n                obj.externalTemperature2 = temp / 10;\n            }\n            i += 2;\n            break\n        case TYPE_EXT_DIGITAL2: //Digital input 2\n            obj.digital2 = (data[i + 1]);\n            i += 1;\n            break\n        case TYPE_EXT_ANALOG_UV: //Load cell analog uV\n            obj.analogUv = (data[i + 1] << 24) | (data[i + 2] << 16) | (data[i + 3] << 8) | (data[i + 4]);\n            i += 4;\n            break\n        case TYPE_TVOC:\n            obj.tvoc = (data[i + 1] << 8) | (data[i + 2]);\n            i += 2;\n            break\n        default: //somthing is wrong with data\n            i = data.length;\n            break\n        }\n    }\n    return obj;\n}\n\n    msg.payload = DecodeElsysPayload(hexToBytes(msg.payload));\nreturn msg;\n        ","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":1880,"wires":[["1f3cc108.ff5d5f"]]},{"id":"1f3cc108.ff5d5f","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":540,"y":1900,"wires":[]}]

Just need to edit the end as it was retrieving data from the document. You need to change that to the msg.payload and return the result.

I tried that code in function nodes (see func 1, func 2 and func 3) and still the same result.
Am I supposed to in another way?

As I said, I am new to this, so please explain like I was three years old.

Please provide your full flow so we can look at it.

Hi,

If you are using a MQTT node in your flow, to get the data from your gateway then set the output to a parsed JSON object and I guess that the javascript function will work.

image

Hello,

Yes, that should have been mentioned earlier, I use MQTT from the Gateway.

I had missed that feature, but unfortunately it just removed the JSON parser.

How do I do that? As I said, I am new to this.

Here are the relevant pages from the doc's.

Selecting nodes/flows

Import/export

Working with messages

Now when I learned that I can Import, I imported the Javascript that E1cid made for me = Great Succes! :smiley:

1 Like

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

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