The server requires the development version of aiocoap and a port of tatobari/hx711py to Python 3. Remember that the resource directory must run on the well-known CoAP port so choose a different port for the CoAP server. The server returns CBOR data. It is very important to set the ep name. The resource directory differentiates nodes based on it.
git clone https://github.com/chrysn/aiocoap
cd aiocoap
pip3 install --user .
cd ..
cd hx711py
pip3 install --user .
cd ..
pip3 install --user cbor2
chmod +x server.py
./server.py -h # look at the extra options
./server.py --ep rpi --dout X --pd_sck Y --rd coap://rd-ip --port PORT # GPIO pins X, Y
To test the server
pip3 install --user pygments
pip3 install --user termcolor
aiocoap-client --observe --pretty-print --quiet coap://localhost:PORT/weight
To tare the load cell
aiocoap-client -m POST coap://localhost:PORT/weight
Going to use aiocoap's implementation. Details: CoRE Resource Directory. Note: This is hardcoded to run on the well-known CoAP port.
aiocoap-rd
To test the RD (once CoAP server has registered)
pip3 install --user LinkHeader
aiocoap-client coap://localhost/resource-lookup/?rt=weight
The WebSocket server queries the resource directory using the specified query URI, and observes all found resources. Readings received from these resources are made available to incoming WebSocket connections. Incomming connections may tare a scale by sending the title string of the scale, this informs the server to send a CoAP POST to the weight resource.
pip3 install --user LinkHeader
./websocket.py --port 5688 coap://<rd-ip>/resource-lookup/?rt=weight
python3 -m websockets ws://localhost:port/
When we get to the UI, cbor-js can decode CBOR data sent from the WebSocket server.
Note: This is a slight simplification. The registration process begins with a GET /.well-known/core to the RD, then the server chooses to POST to /resourcedirectory because its Resource-Type is core.rd
Provides /weight
resource and returns CBOR data same as CoAP server. Defaults to localhost port 80 unless you use --bind
/--port
.
pip3 install --user bottle
./httpserver.py --ref-unit 441 --dout 23 --pd_sck 24
Request weight from the command line
curl -s http://localhost/weight | python3 -m cbor2.tool
See response header
curl -s -i http://localhost/weight
Tare scale
curl -X POST http://localhost/weight