A six-byte sensor payload: encode, decode and test every field
A sensor message is useful only when the device and the receiving application agree on exactly what its bytes mean. A value that looks plausible on a dashboard can still be wrong: the decoder may have reversed two bytes, treated a negative temperature as unsigned, or applied the scale twice. Start with a small written contract, then test that contract with values chosen to expose mistakes.

This workshop builds an illustrative six-byte message containing a version, temperature, relative humidity and an application sequence number. Sigfox’s payload documentation allows up to 12 bytes of application data in an uplink and up to 8 in a downlink. Those limits do not define your sensor schema, transmission allowance or delivery guarantee. The format below is our teaching example, not a standard supplied by Sigfox.
1. Define the message before writing the decoder
| Byte positions | Field | Representation | Validation |
|---|---|---|---|
| 0 | Schema version | Unsigned 8-bit integer | Must equal 1 |
| 1–2 | Temperature | Signed 16-bit integer, in hundredths of a degree Celsius | Encoded integer from −32768 to 32767 |
| 3 | Relative humidity | Unsigned 8-bit integer, in whole percentage points | 0 through 100 |
| 4–5 | Application sequence | Unsigned 16-bit integer | 0 through 65535 |
“Big-endian” means the more significant byte comes first. The scale is part of the contract: an encoded temperature of 2150 means 21.50°C. A hundredth-degree encoding step does not establish hundredth-degree sensor accuracy. Measurement accuracy, calibration and the sensor’s operating range belong to the device specification.
The signed representation can express −327.68°C to 327.67°C. That is a mathematical encoding range, not a physically credible operating range for the chosen sensor. The ingestion service should apply a second, documented plausibility check appropriate to its device and use. Keep the raw observation when it fails that check so the cause can be investigated.
2. Read one message all the way through
Take 0108662D002A. Hexadecimal uses two digits per byte, so 12 hex digits represent six bytes. Split it according to the schema, not into arbitrary pairs of decimal numbers:
01 | 08 66 | 2D | 00 2A
v | temp | RH | sequence
The version is 1. The temperature integer is 0x0866: 8 × 256 + 102 = 2150, which becomes 21.50°C after division by 100. The humidity byte 0x2D is 45. The sequence 0x002A is 42. Neither the temperature unit nor the humidity unit is transmitted as text; the receiving software knows them from version 1 of the schema.
Now test 01FDDA2D002A. Reading 0xFDDA as unsigned gives 64986. In this signed 16-bit field, subtract 65536 to obtain −550, then divide by 100 to obtain −5.50°C. A decoder that displays 649.86°C has failed the signed-number contract even though it produced a number without an exception.
3. Use explicit types, byte order and rejection rules
Python’s struct module provides a compact implementation. In the format >BhBH, > selects big-endian standard sizes with no alignment padding; B, h, B and H represent the four fields. The downloadable implementation validates types and ranges before packing, and validates the exact input spelling before decoding.
FRAME = struct.Struct(">BhBH")
version, temperature_centi, humidity_pct, sequence = (
FRAME.unpack(bytes.fromhex(payload_hex))
)
temperature_c = temperature_centi / 100
This short fragment shows the representation step; it is not the complete input validator. Use the complete Python encoder and decoder for the accompanying checks. You can also try the same contract in the browser payload calculator. The calculator performs local calculations and does not send a radio message.
The encoder accepts an integer count of hundredths, such as 2150, rather than an arbitrary decimal temperature. That keeps rounding out of the transport function. Decide in the sampling layer how a reading such as 21.505°C becomes an integer, including how negative halfway values are treated. Document that decision and test it separately; silently mixing rounding conventions makes cross-language comparisons harder.
The text decoder requires exactly 12 hexadecimal characters. Lowercase letters are accepted; spaces, a 0x prefix, odd lengths and non-hexadecimal characters are rejected. This strict spelling is an application-interface choice. If an upstream system supplies another representation, normalize it explicitly at that boundary instead of making the core decoder guess.
4. Test values that distinguish a good decoder from a lucky one
| Input | Expected result | What it checks |
|---|---|---|
0108662D002A | 21.50°C; 45%; sequence 42 | Ordinary byte order and scaling |
01FDDA2D002A | −5.50°C; 45%; sequence 42 | Signed decoding |
010000000000 | 0.00°C; 0%; sequence 0 | Valid zeros are not missing values |
01086665002A | Reject humidity 101 | A value can fit a byte yet violate the schema |
0208662D002A | Reject version 2 | Unknown layouts are not silently interpreted as version 1 |
0108662D002 | Reject the text length | Truncated messages do not produce partial readings |
Test both directions.
- Encoding 2150, 45 and 42 must produce the first message.
- Decoding that message must recover the same integers.
- Include the minimum and maximum representable temperature integers and both sequence boundaries.
- Separately test invalid encoder inputs: a boolean, a fractional humidity, a negative sequence and an out-of-range temperature must not be silently converted into acceptable values.
A round trip alone is insufficient. An encoder and decoder can share the same wrong byte order and still agree with each other. The fixed byte strings in the table provide an independent reference. Keep these vectors with the schema so another implementation can be checked without copying the first implementation’s assumptions.
5. Decide what happens when a measurement is unavailable
Version 1 has no validity flag or special missing-reading value. Zero humidity is a valid number in the representation and cannot also mean “sensor unavailable.” If the sensor cannot provide a reading, the sampling system must record that failure separately or use a new, explicitly defined schema. Do not invent an undocumented sentinel halfway through a deployment.
Likewise, an invalid received message should become a visible ingestion exception, not a plausible substitute reading. Preserve its device identity, arrival time, raw payload and rejection reason in a restricted diagnostic record. The operator’s display should distinguish “last valid reading was 21.50°C at this time” from “a new valid reading has arrived.” Otherwise an old value can look reassuringly current.
6. Give the application counter a limited, clear job
The final two bytes are our application counter, not a claim about a Sigfox protocol counter. For this example, increment it once for each new application observation and wrap 65535 back to 0. If the application sends the same observation again, retain its counter. A different policy is possible, but the receiver must know which policy was chosen.
Seeing 65534, 65535 and 0 can therefore be normal. Seeing 100 and then 103 may indicate missing observations, out-of-order arrival, filtering elsewhere or a device restart policy that has not been explained. The number alone cannot establish which explanation is correct.
Six bytes leave no room for a boot identifier or acquisition timestamp in this particular design. If the counter resets on restart, the application needs another way to distinguish boot sessions before treating repeated values as duplicates. At minimum, scope any duplicate check to the device and a defined observation window; preserve arrival order and do not assume a counter uniquely identifies an observation forever. If precise acquisition time is required, revise the schema or its accompanying metadata contract.
7. Hand over a reproducible contract
A complete handover includes the field table, units, signedness, byte order, scaling and rounding policy; the valid and invalid vectors; the reboot and counter rules; and the behavior for unknown versions or missing measurements. Name the schema version in both the device release and the receiving application’s configuration.
Then run a separate end-to-end trial: create a known observation, retain its encoded bytes, inspect the received record and compare the displayed value. Repeat after a device restart and an application interruption. Passing the codec tests establishes representation correctness. The field trial must establish whether the required observations reach the intended people within the time their task requires.