LHT52 Decoder example

Example of decoding LHT52 sensor.

[{"id":"8280fdd0.0188c","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"c0caab34.627f78","type":"function","z":"8280fdd0.0188c","name":"str2hex","func":"if(Object.keys(msg.payload).length === 0) return null;\n\nmsg.payload.data = Buffer.from(msg.payload.data, \"hex\")\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":680,"y":100,"wires":[["c6daf445.5a2da8"]]},{"id":"c6daf445.5a2da8","type":"function","z":"8280fdd0.0188c","name":"LHT52 Decoder","func":"//https://github.com/dragino/dragino-end-node-decoder/blob/main/LHT52/LHT52%20Decoder%20Chirpstack%20V4%20.txt\nfunction decodeUplink(input) {\n        return { \n            payload: Decode(msg.payload.port, msg.payload.data, '')\n        };   \n}\nfunction str_pad(byte){\n    var zero = '0';\n    var hex= byte.toString(16);    \n    var tmp  = 2-hex.length;\n    return zero.substr(0,tmp) + hex;\n}\n\nfunction Decode(fPort, bytes, variables) {\n\nvar decode = {};\nif(fPort==2)\n{\n  if(bytes.length==11)\n  {\n    decode.TempC_SHT=parseFloat(((bytes[0]<<24>>16 | bytes[1])/100).toFixed(2));\n    decode.Hum_SHT=parseFloat(((bytes[2]<<24>>16 | bytes[3])/10).toFixed(1));\n    decode.TempC_DS=parseFloat(((bytes[4]<<24>>16 | bytes[5])/100).toFixed(2));\n\n    decode.Ext=bytes[6];\n    decode.Systimestamp=(bytes[7]<<24 | bytes[8]<<16 | bytes[9]<<8 | bytes[10] );\n    \n    return decode;\n  }\n  else\n  {\n    decode.Status=\"RPL data or sensor reset\";\n \n    return decode;\n  }\n}\n\nif(fPort==3)\n{\n  decode.Status=\"Data retrieved, your need to parse it by the application server\";\n \n  return decode;\n}\n\nif(fPort==4)\n{\n  decode.DS18B20_ID=str_pad(bytes[0])+str_pad(bytes[1])+str_pad(bytes[2])+str_pad(bytes[3])+str_pad(bytes[4])+str_pad(bytes[5])+str_pad(bytes[6])+str_pad(bytes[7]);\n\n  return decode;\n}\n\nif(fPort==5)\n{\n  decode.Sensor_Model=bytes[0];\n  decode.Firmware_Version=str_pad((bytes[1]<<8)|bytes[2]);\n  decode.Freq_Band=bytes[3];\n  decode.Sub_Band=bytes[4];\n  decode.Bat_mV=bytes[5]<<8|bytes[6];\n \n  return decode;\n}\n}\n//if(msg.payload.data.length==1 && msg.payload.data[0] === 0){\n//    return null;\n//}\nreturn decodeUplink();\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":860,"y":100,"wires":[["deb606cd.e7e668"]]},{"id":"deb606cd.e7e668","type":"debug","z":"8280fdd0.0188c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1030,"y":100,"wires":[]},{"id":"38481f65.e6c4e","type":"inject","z":"8280fdd0.0188c","name":"data ex LHT2","props":[{"p":"payload.data","v":"0A16026F7FFF0166FDA334","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":250,"y":100,"wires":[["f6f1df2e.a3b688"]]},{"id":"f6f1df2e.a3b688","type":"change","z":"8280fdd0.0188c","name":"","rules":[{"t":"set","p":"payload.port","pt":"msg","to":"2","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":100,"wires":[["c0caab34.627f78"]]}]

Flow Info

Created 5 months ago
Rating: not yet rated

Actions

Rate:

Node Types

Core
  • change (x1)
  • debug (x1)
  • function (x2)
  • inject (x1)
Other
  • tab (x1)

Tags

  • LHT52
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option