A conceptual illustration showing a LoRaWAN gateway converting raw hexadecimal sensor data into readable JSON format locally.

LoRaWAN Gateway Decoding: Hex to JSON at the Edge

Written by: Robert Liao

|

Published on

|

Time to read 5 min

Author: Robert Liao, Technical Support Engineer

Robert Liao is an IoT Technical Support Engineer at Robustel with hands-on experience in industrial networking and edge connectivity. Certified as a Networking Engineer, he specializes in helping customers deploy, configure, and troubleshoot IIoT solutions in real-world environments. In addition to delivering expert training and support, Robert provides tailored solutions based on customer needs—ensuring reliable, scalable, and efficient system performance across a wide range of industrial applications.

Summary

IoT sensors speak a compact language called "Hexadecimal" to save battery. The Cloud speaks a readable language called "JSON." Traditionally, the translation happens in the cloud, forcing developers to manage complex server-side scripts. This guide explores a smarter architecture: Edge Decoding. We explain how to run JavaScript decoders directly on an industrial LoRaWAN gateway. By converting raw bytes (e.g., 0167) into human-readable values (e.g., "temp": 25) at the edge, you simplify integration, reduce cloud computing costs, and enable direct local control.

Key Takeaways

The Language Barrier: Sensors send efficient but unreadable binary data. A LoRaWAN gateway usually forwards this blindly, pushing the work to the cloud.

Edge Intelligence: A smart LoRaWAN gateway can run a "Decoder" script locally, transforming the packet before it even leaves the building.

JavaScript Compatibility: RobustOS supports standard JavaScript functions, allowing you to copy-paste existing decoders from sensor manufacturers directly into the gateway.

Integration Readiness: Sending pre-decoded JSON means your SCADA or Dashboard can display the data immediately without needing a middleware processing layer.

LoRaWAN Gateway Decoding: Hex to JSON at the Edge

In the world of LoRaWAN, efficiency is everything. To save battery, a temperature sensor does not send the text "25.5 Degrees." It sends a tiny payload like 00FF.

This "Hexadecimal" data is great for the radio, but terrible for the human operator. Before you can use the data, it must be decoded.

Most architectures send the raw hex to the cloud and decode it there (e.g., using an AWS Lambda function). But there is a better way.

Why not decode it before it leaves the site?

By using a powerful industrial LoRaWAN gateway with edge computing capabilities, you can translate "Hex" to "JSON" locally. This guide explains how to program your gateway to become a smart translator.


A conceptual illustration showing a LoRaWAN gateway converting raw hexadecimal sensor data into readable JSON format locally.


The Problem: Raw Hex Data

When a standard LoRaWAN gateway forwards a packet, the payload looks like this: Payload: AQIDBA== (Base64 encoded Hex).

To a database or a SCADA system, this is gibberish. You cannot graph it. You cannot trigger an alert on it. You need a separate server, a cloud subscription, or a Node-RED flow just to turn that string into a number. This adds latency, complexity, and points of failure to your infrastructure.

The Solution: Edge Decoding on the LoRaWAN Gateway

An intelligent LoRaWAN gateway (like the Robustel R1520LG) is not just a radio; it is a Linux computer. It can run scripts.

By loading a JavaScript Decoder onto the gateway, the device performs the translation instantly upon receipt.

  • Input:0x016700FF
  • Process: The LoRaWAN gateway runs the Decoder() function.
  • Output:{"temperature": 25.5, "humidity": 60}

Now, the data leaving the LoRaWAN gateway via MQTT or HTTP is already formatted as a clean JSON object. Your dashboard can ingest it directly.

How to Write a Decoder for Your LoRaWAN Gateway

The decoding logic typically follows the standard defined by The Things Network (TTN). If you have a decoder for TTN, it will likely work on your Robustel LoRaWAN gateway.

Basic Structure:

JavaScript


function Decoder(bytes, port) { var decoded = {}; if (port === 1) { decoded.temperature = (bytes[0] << 8 | bytes[1]) / 100; } return decoded; }

You don't need to be a coding wizard. Most sensor manufacturers (Dragino, Bosch, Milesight) provide these .js files on their websites. You simply copy the code and paste it into the LoRaWAN gateway configuration.


A graphic showing a standard JavaScript decoder script running inside the RobustOS interface of a LoRaWAN gateway.


Step-by-Step Implementation

Here is how to deploy this on a Robustel device running RobustOS.

  1. Access the GUI: Log in to your LoRaWAN gateway.
  2. Open the App: Navigate to Interface > LoRa > Advanced.
  3. Enable Custom Decoder: Toggle the switch to "On."
  4. Paste the Script: Copy your sensor's JavaScript code into the text box.
  5. Test: Use the "Test" button. Input a sample Hex string (e.g., 0167) and see if the LoRaWAN gateway outputs the correct JSON.
  6. Save: Apply the settings.

From this moment on, every packet sent to the MQTT broker will include a fields object containing your readable data.

Benefits of Gateway-Level Decoding

Why shift the workload to the LoRaWAN gateway?

  1. SCADA Compatibility: Legacy SCADA systems don't know how to parse Hex. They expect values. By decoding at the edge, you can map "Temperature" directly to a Modbus register or MQTT topic.
  2. Bandwidth Optimization: While JSON is larger than Hex, sending meaningful keys allows you to filter data. You can configure the LoRaWAN gateway to only upload the data if the temperature changes by >1 degree, saving 4G data costs.
  3. Offline Autonomy: If the internet cuts out, the gateway stores the decoded data. When you retrieve the logs later, they are readable, not a pile of encrypted hex strings that you have to decode manually.

A comparison diagram showing how edge decoding on a LoRaWAN gateway simplifies the data pipeline by removing the need for cloud-based decoding functions.


Conclusion: Smarter Edge, Simpler Cloud

The trend in IoT is moving computation to the edge.

A "dumb" packet forwarder is no longer enough. By utilizing the decoding power of your industrial LoRaWAN gateway, you simplify your entire downstream architecture. You remove the dependency on cloud functions and deliver clean, actionable data to your application the moment it arrives.

Frequently Asked Questions (FAQ)

Q1: Does decoding slow down the LoRaWAN gateway?

A1: Negligible impact. The conversion from Hex to JSON takes a few milliseconds. An industrial LoRaWAN gateway typically has a 500MHz+ processor, which is more than capable of decoding thousands of packets per second without introducing noticeable latency.

Q2: Can I decode multiple different sensors?

A2: Yes. The decoder script can use the port number or the sensor's DevEUI to switch logic. For example: if (port === 2) { decode_soil_sensor() } else if (port === 3) { decode_tracker() }. This allows a single LoRaWAN gateway to handle a mixed fleet of devices.

Q3: What if the sensor sends proprietary/encrypted formats?

A3: Some sensors use proprietary encoding that isn't public. In this case, you cannot decode it on the LoRaWAN gateway unless the manufacturer provides the private key or codec. However, 95% of commercial LoRaWAN sensors use open payload formats documented in their user manuals.