{"id":19713,"date":"2026-08-07T11:47:47","date_gmt":"2026-08-07T03:47:47","guid":{"rendered":"https:\/\/www.lansitec.com\/?post_type=docs&#038;p=19713"},"modified":"2026-08-07T11:47:56","modified_gmt":"2026-08-07T03:47:56","password":"","slug":"decoder-lorawan-temperaturehumidity-sensor-ttn-chirpstack","status":"publish","type":"docs","link":"https:\/\/www.lansitec.com\/es\/docs\/decoders\/decoder-lorawan-temperaturehumidity-sensor-ttn-chirpstack\/","title":{"rendered":"Decodificador LoRaWAN \u2013 sensor de temperatura y humedad (TTN\/Chirpstack)"},"content":{"rendered":"<div class=\"wp-block-file\"><a id=\"wp-block-file--media-638185d2-d691-4492-be45-af8cb61faf47\" href=\"https:\/\/www.lansitec.com\/wp-content\/uploads\/2026\/08\/decode-temperature-humidity-ttn_chirpstackv4.js.zip\">decode-temperature-humidity-ttn_chirpstackv4.js<\/a><a href=\"https:\/\/www.lansitec.com\/wp-content\/uploads\/2026\/08\/decode-temperature-humidity-ttn_chirpstackv4.js.zip\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-638185d2-d691-4492-be45-af8cb61faf47\">Descargar<\/a><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Below the full decoder code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Decode decodes an array of bytes into an object.\n\/\/  - fPort contains the <a href=\"https:\/\/www.lansitec.com\/lorawan\/\">LoRaWAN<\/a> fPort number\n\/\/  - bytes is an array of bytes, e.g. &#91;225, 230, 255, 0]\n\/\/  - variables contains the device variables e.g. {\"calibration\": \"3.5\"} (both the key \/ value are of type string)\n\/\/ The function must return an object, e.g. {\"temperature\": 22.5}\n\n\/\/ Temperature Humidity\nfunction decodeUplink(input) {\n  \/\/ bytes\n  var bytes = input.bytes;\n  \/\/ type\n  var uplinkType = (bytes&#91;0] >> 4) &amp; 0x0f;\n\n  switch (uplinkType) {\n    case 0x01:\n      return { data: decodeRegistration(bytes) };\n\n    case 0x02:\n      return { data: decodeHeartbeat(bytes) };\n\n    case 0x0f:\n      return { data: decodeAcknowledgment(bytes) };\n\n    default:\n      return null;\n  }\n}\n\n\/\/ type: 0x1 Registration\nfunction decodeRegistration(bytes) {\n  var data = {};\n  data.type = \"Registration\";\n  \/\/ adr\n  data.adr = ((bytes&#91;0] >> 3) &amp; 0x1) == 0 ? \"OFF\" : \"ON\";\n  \/\/ mode\n  var modeValue = bytes&#91;0] &amp; 0x07;\n  if (modeValue == 0x01) {\n    data.mode = \"AU920\";\n  } else if (modeValue == 0x02) {\n    data.mode = \"CLAA\";\n  } else if (modeValue == 0x03) {\n    data.mode = \"CN470\";\n  } else if (modeValue == 0x04) {\n    data.mode = \"AS923\";\n  } else if (modeValue == 0x05) {\n    data.mode = \"EU433\";\n  } else if (modeValue == 0x06) {\n    data.mode = \"EU868\";\n  } else if (modeValue == 0x07) {\n    data.mode = \"US915\";\n  }\n  \/\/ smode\n  var smodeValue = bytes&#91;1];\n  if (smodeValue == 0x01) {\n    data.smode = \"AU920\";\n  } else if (smodeValue == 0x02) {\n    data.smode = \"CLAA\";\n  } else if (smodeValue == 0x04) {\n    data.smode = \"CN470\";\n  } else if (smodeValue == 0x08) {\n    data.smode = \"AS923\";\n  } else if (smodeValue == 0x10) {\n    data.smode = \"EU433\";\n  } else if (smodeValue == 0x20) {\n    data.smode = \"EU868\";\n  } else if (smodeValue == 0x40) {\n    data.smode = \"US915\";\n  }\n  \/\/ power\n  data.power = ((bytes&#91;2] >> 3) &amp; 0x1f) + \"dBm\";\n  \/\/ reserved\n  var reservedValue = bytes&#91;2] &amp; 0x07;\n  if (reservedValue == 1) {\n    data.frequencySweepMode = \"A mode\";\n  } else if (reservedValue == 2) {\n    data.frequencySweepMode = \"B mode\";\n  } else if (reservedValue == 3) {\n    data.frequencySweepMode = \"C mode\";\n  } else if (reservedValue == 4) {\n    data.frequencySweepMode = \"D mode\";\n  } else if (reservedValue == 5) {\n    data.frequencySweepMode = \"E mode\";\n  } else if (reservedValue == 6) {\n    data.frequencySweepMode = \"All frequency sweep\";\n  }\n  \/\/ dr\n  data.dr = (bytes&#91;3] >> 4) &amp; 0x0f;\n  \/\/ repting\n  data.repting = ((bytes&#91;3] >> 3) &amp; 0x01) == 0 ? \"false\" : \"true\";\n  \/\/ temperatureReportPeriod\n  data.temperatureReportPeriod =\n    (((bytes&#91;4] &lt;&lt; 8) &amp; 0xff00) | (bytes&#91;5] &amp; 0xff)) * 10 + \"s\";\n  \/\/ crc\n  data.crc = ((bytes&#91;6] &lt;&lt; 8) &amp; 0xff00) | (bytes&#91;7] &amp; 0xff);\n  return data;\n}\n\n\/\/ type: 0x2 Heartbeat\nfunction decodeHeartbeat(bytes) {\n  var data = {};\n  \/\/ type\n  data.type = \"Heartbeat\";\n  \/\/ version\n  data.version = bytes&#91;0] &amp; 0x0f;\n  \/\/ battery\n  data.battery = bytes&#91;1] * 0.1 + \"V\";\n  \/\/ <a href=\"https:\/\/www.lansitec.com\/blogs\/rssi-indoor-positioning\/\">rssi<\/a>\n  data.rssi = bytes&#91;2] * -1 + \"dBm\";\n  \/\/ snr\n  data.snr = (((bytes&#91;3] &lt;&lt; 8) &amp; 0xff00) | (bytes&#91;4] &amp; 0xff)) \/ 100;\n  \/\/ temperature\n  if (0 == ((bytes&#91;5] >> 7) &amp; 0x01)) {\n    var tempInt = bytes&#91;5] &amp; 0x7f;\n    var tempFra = bytes&#91;6];\n    data.temperature = tempInt + \".\" + tempFra + \"\u2103\";\n  } else {\n    var tempInt = (bytes&#91;5] &amp; 0x7f) * -1;\n    var tempFra = bytes&#91;6];\n    data.temperature = tempInt + \".\" + tempFra + \"\u2103\";\n  }\n  \/\/ humidity\n  data.humidity = bytes&#91;7] + \"%\";\n  \/\/ crc\n  data.crc = ((bytes&#91;8] &lt;&lt; 8) &amp; 0xff00) | (bytes&#91;9] &amp; 0xff);\n\n  return data;\n}\n\n\/\/ type: 0xf Acknowledgment\nfunction decodeAcknowledgment(bytes) {\n  var data = {};\n  data.type = \"Acknowledgment\";\n  data.result = (bytes&#91;0] &amp; 0x0f) == 0 ? \"Success\" : \"Failure\";\n  data.msgId = (bytes&#91;1] &amp; 0xff).toString(16).toUpperCase();\n\n  return data;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>","protected":false},"excerpt":{"rendered":"<p>Below the full decoder code:<\/p>","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"doc_category":[338],"doc_tag":[],"class_list":["post-19713","docs","type-docs","status-publish","hentry","doc_category-decoders"],"year_month":"2026-08","word_count":440,"total_views":"21","reactions":{"happy":"0","normal":"0","sad":"0"},"author_info":{"name":"Ma\u00edz","author_nicename":"maizi","author_url":"https:\/\/www.lansitec.com\/es\/author\/maizi\/"},"doc_category_info":[{"term_name":"Decoders","term_url":"https:\/\/www.lansitec.com\/es\/doc\/decoders\/"}],"doc_tag_info":[],"knowledge_base_info":[],"knowledge_base_slug":[],"_links":{"self":[{"href":"https:\/\/www.lansitec.com\/es\/wp-json\/wp\/v2\/docs\/19713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lansitec.com\/es\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/www.lansitec.com\/es\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/www.lansitec.com\/es\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lansitec.com\/es\/wp-json\/wp\/v2\/comments?post=19713"}],"version-history":[{"count":1,"href":"https:\/\/www.lansitec.com\/es\/wp-json\/wp\/v2\/docs\/19713\/revisions"}],"predecessor-version":[{"id":19717,"href":"https:\/\/www.lansitec.com\/es\/wp-json\/wp\/v2\/docs\/19713\/revisions\/19717"}],"wp:attachment":[{"href":"https:\/\/www.lansitec.com\/es\/wp-json\/wp\/v2\/media?parent=19713"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/www.lansitec.com\/es\/wp-json\/wp\/v2\/doc_category?post=19713"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/www.lansitec.com\/es\/wp-json\/wp\/v2\/doc_tag?post=19713"}],"curies":[{"name":"gracias","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}