Quickstart

CoAP Quickstart: Read an LwM2M Sensor

Run a library LwM2M sensor, read its temperature over CoAP and verify a setpoint change.

Before you begin

Install SPX with CoAP/LwM2M and SPX UI, and have a reachable LwM2M server and a CoAP client. This quickstart uses the ready-made environment sensor model. SPX Setup can run Leshan, or you can use your own server.

Run the test

1. Start the sensor

  • Choose Env.EnvSensor.Lwm2m. If it is missing, import the linked YAML through Models → Load model from file. Create a new instance using the environment sensor.
  • Set lwm2m_server_host to the server address reachable from SPX and set lwm2m_local_port to 56830 for this example. Start the instance and verify registration on the LwM2M server.
  • The server's UDP 5683 is the registration endpoint. The sensor's UDP 56830 is the separate endpoint for the resource requests below.
SPX UI showing the LwM2M adapter, device endpoint and temperature chart.
Check the registration server and device endpoint in Communication.

Screenshot preview

Check the registration server and device endpoint in Communication. Open original image

2. Read and write resources

  • Send GET to coap://SPX_HOST:56830/3303/0/5700 to read the simulated temperature in °C.
  • Send PUT with text/plain payload 24.0 to /3308/0/5900. The device returns 2.04 Changed with an empty body.
  • Send a separate GET to the same resource to verify the target is 24.0.

3. Observe and restore

  • In SPX UI, select the temperature and target attributes to observe the model response.
  • Write 22.0 to the target, verify the readback and stop the test instance.
SPX UI chart showing a temperature target change and simulated response.
The target is changed and restored while the sensor temperature responds.

Screenshot preview

The target is changed and restored while the sensor temperature responds. Open original image

Check the result

A successful write acknowledgement and a subsequent read are separate checks. For a client outside Docker, use the reachable SPX host and publish the device's UDP port; the Docker-only server hostname may not resolve on your computer.

Protocol communication

Use these settings and messages with the library’s LwM2M environment sensor model.

Device URI
coap://SPX_HOST:56830 — chosen local instance UDP port
Registration server
LWM2M_SERVER_HOST:5683; endpoint generic-lwm2m-env-001; separate from the device port
Payload format
text/plain (CoAP Content-Format 0); numeric values as text

Replace host placeholders with reachable addresses and use your published ports.

Swipe the table to see requests and replies.

Addresses, requests and expected replies
Address / messageOperation / payloadExpected reply
/3/0/0GET — manufacturer2.05 Content; Hammerheads Engineers
/3303/0/5700GET — temperature2.05 Content; numeric °C text, starting at 22.5 then changing
/3304/0/5700GET — humidity2.05 Content; numeric percentage text
/3308/0/5900GET / PUT — target; send text 24.0PUT: 2.04 Changed; subsequent GET: 2.05 with 24.0
/3308/0/5903GET / PUT — HVAC mode; send text autoPUT: 2.04 Changed; GET: 2.05 with auto

Values describe this model’s nominal setup unless noted. Live measurements change as the simulation runs.

Example request and reply

Read the manufacturer, PUT 24.0 to /3308/0/5900 with Content-Format 0, then GET it. Verify 24.0 and follow the changing temperature. Restore target 22.0.

GET coap://SPX_HOST:56830/3/0/0
→ 2.05 Content: Hammerheads Engineers
PUT coap://SPX_HOST:56830/3308/0/5900
Content-Format: 0 (text/plain)
Payload: 24.0
→ 2.04 Changed (empty payload)
GET coap://SPX_HOST:56830/3308/0/5900
→ 2.05 Content: 24.0
No reply or an unexpected value?
  • The linked model uses the lwm2m adapter: it registers with a server and exposes CoAP resources. It differs from a standalone coap_server model.
  • 56830 is an explicit local-port override; default 0 selects a runtime port. Publish the chosen UDP port. Device resource reads go to that port, not the registration server’s 5683.
  • PUT returns an empty 2.04 Changed response, not an echoed value. GET to verify. Check registration and UDP reachability before debugging paths.

Ask your AI agent

Open your SPX workspace in a local AI agent. We tested this prompt with GPT-6 Luna · Medium using a temporary isolated Leshan server:

Prompt for your AI agent

Create one temporary instance from the `Env.EnvSensor.Lwm2m` model. Start a temporary Leshan LwM2M server and the instance on an isolated Docker network with no ports published to the host. Using a real CoAP/LwM2M client, verify registration, GET the temperature setpoint at `/3308/0/5900`, PUT a new value, and GET again to confirm the update. Report actual endpoints, responses, and any failures. Then remove only the instance and temporary test containers you created.

Tested with GPT-6 Luna · Medium

Estimated API cost · $0.0169 per run

In our test, Leshan listed the sensor on UDP 56830. CoAP returned 2.05 Content for the initial setpoint of 22.0 °C. Writing 23.5 returned 2.04 Changed, and the next read returned 23.5. The cost includes retrying with a Java compatibility setting for the temporary Leshan container.

Next steps

Create a custom CoAP device with an AI agent. For more adapter settings, see the LwM2M guide.