Skip to content

Test: CoAP (the web, shrunk for sensors)

Networking & IoT

CoAP: The Web, Shrunk for Sensors

Why CoAP? The web, shrunk for sensors

MQTT puts a broker in the middle. CoAP (the Constrained Application Protocol) takes the other road: every node is a small web server. Node 1 offers resources such as /temp and /humidity, the fan controller offers /fan, and a client asks for them with the web’s methods (GET, PUT, POST, DELETE). Answers come back with codes you know from HTTP, packed into one byte: 2.05 Content for 200 OK, 4.04 Not Found. It all runs over UDP, so there is no connection to set up or keep alive, and the access point in the middle just forwards datagrams. The price is that a client must know each node’s address, and each node must be reachable. Send some requests from the phone.

Challenge: Switch the fan on with a PUT, then read /fan back with a GET to check it.

Requests: the web’s methods

GET reads a resource, PUT replaces it, POST creates or triggers, DELETE removes. The URI names the node and the resource.

GET /tempPUT /fan onPOSTDELETE

Responses: HTTP’s codes in one byte

Class.detail: 2.xx success, 4.xx the client’s mistake, 5.xx the server’s. 2.05 Content is HTTP’s 200, 4.04 is 404.

2.05 Content2.04 Changed4.04 Not Found4.05 Not Allowed
Wi-Fi access pointforwards UDP datagramsno broker, no stateSensor node 1192.168.1.21, CoAP serverlistening on 5683Sensor node 2192.168.1.22, CoAP serverlistening on 5683Fan controller192.168.1.23, CoAP serverfan offPhone app192.168.1.60, CoAP clienton the Wi-Fi
CON (confirmable)NON (non-confirmable)ACKRST✕ lost↻ retransmission · each leg slowed to 260 ms, timers at real speed

What the phone received, newest first

Nothing yet.

← Start here
Speed
t = 0.0 s

The phone sends a request (CON)

Server
Method
coap://192.168.1.21

Last request

-

Answer

-

Every node runs its own server, and the phone needs each one’s address: here they are fixed IPv4 addresses on the greenhouse Wi-Fi. Nothing stands in the middle that could keep a value for later, which is why a reading is always the node’s current one.

Message log, newest first

Nothing on the wire yet.