Translate a local CoAP reading into an HTTP record
Map a local CoAP reading to an HTTP record, reject malformed data, and replay an outage queue with a stable idempotency key.

Gateway Gus
Gateway Gus: I want you to preserve source meaning through mapping, failure, and replay.
Predict the reading, then compare it with the measurement.
Python 3 + aiocoap
PythonDownload the three files, create a Python virtual environment, install requirements.txt, and run main.py.
Open the Python run guide (new tab)Get the files
Download all three prepared files into one folder. No account or paid service is required.
- Put main.py, requirements.txt, and README.md in the same folder.
- Create and activate a Python virtual environment, then install requirements.txt.
- Run python3 main.py and compare its output with each observed result below.
Steps
Step 1
- Do
- In the terminal window panel, run
python3 main.py --step 1to GET the local CoAP temperature reading. - You will see
- The CoAP response is
2.05 Content, content format JSON, and the actual payload bytes and UTF-8 record are printed.
- The CoAP response is
- Why it matters
- A bridge needs a source payload and resource contract before mapping to HTTP.

Step 1 · Python 3 + aiocoap; numbered callout added to a real capture. Step 2
- Do
- In the terminal window panel, run
python3 main.py --step 2to map the reading and POST it to the local HTTP receiver. - You will see
- The HTTP receiver sees
POST /measurements HTTP/1.1, a stableIdempotency-Key, mapped JSON body, and status201with one receipt.
- The HTTP receiver sees
- Why it matters
- The request line and body are bytes received by the server, proving the translated delivery and its unit and timestamp.

Step 2 · Python 3 + aiocoap; numbered callout added to a real capture. Step 3
- Do
- Run
python3 main.py --step 3after the sensor sends a string in its numericcelsiusfield. - You will see
- Validation reports
sensor contract rejected: event_id, numeric celsius, UTC timestamp required; HTTP receipts remain1.
- Validation reports
- Why it matters
- Rejecting malformed source meaning before POST prevents a false measurement from entering the destination.

Step 3 · Python 3 + aiocoap; numbered callout added to a real capture. Step 4
- Do
- In the terminal window panel, run
python3 main.py --step 4while the script closes the HTTP listener before POST. - You will see
- The POST gets
ConnectionRefusedError; queue depth becomes1with the mapped record and idempotency key.
- The POST gets
- Why it matters
- The outage is visible and a valid record is retained for bounded local replay.

Step 4 · Python 3 + aiocoap; numbered callout added to a real capture. Step 5
- Do
- In the terminal window panel, run
python3 main.py --step 5to restart the HTTP receiver and replay the queued record. - You will see
- The retry returns HTTP
201, queue depth0, and stored receipts2.
- The retry returns HTTP
- Why it matters
- The replay moves one retained record across the recovered boundary.

Step 5 · Python 3 + aiocoap; numbered callout added to a real capture. Step 6
- Do
- In the terminal window panel, run
python3 main.py --step 6to resend the same record and key once more. - You will see
- The duplicate returns HTTP
200; stored receipts remain2andsame key stored once=True.
- The duplicate returns HTTP
- Why it matters
- The receiver uses the idempotency key to avoid a second stored side effect on duplicate delivery.

Step 6 · Python 3 + aiocoap; numbered callout added to a real capture.
Chapter checks
These questions refer to the chapter’s examples. Use the return links to review their answers.
Why is a protocol bridge more than converting bytes from one protocol to another?
Return to the chapter’s knowledge checkA gateway polls a legacy meter every 10 seconds and publishes results to an event stream. The meter stops responding for one minute. What should the gateway do?
Return to the chapter’s knowledge check
Caution
Before you start
Read this before you start.
The sensor data and UTC timestamp are fictional. Both endpoints are on 127.0.0.1; the queue and receipt table are in memory and vanish on exit. This demonstrates a local mapping and replay contract, not a durable or secure production gateway.
Technical details
- Version tested
- Version tested: Python 3.9.21; aiocoap 0.4.7; local UDP and standard-library HTTP; Xfce Terminal 1.0.4 on Xvfb. Date: 2026-10-08.
https://iotclass.org/labs/coap-http-gateway.html