Table of Contents
CoAP on PC/RaspberryPi/Server
Copper (Cu) Mozilla Browser
After installing Copper, Firefox supports coap: urls. Browse to one of your nodes, e.g. coap:[2001:0db8::221:2eff:ff00:264e]:5683
Use DISCOVER to fetch all resource information.
Set CoAP-Version to 13 (right upper corner)
Or go directly to the well-known core
coap://[2001:0db8::221:2eff:ff00:264e]:5683/.well-known/core
Click on GET to list all resources, select any of them and use GET to load the content of the resource.
Use POST/PUT to send the content of the Outgoing tab to the resource.
SMCP CoAP client
Command line tool written in C
libcoap
libcoap contains an command line client called coap-client in the example folder.
Current version of Contiki-OS uses CoAP-13.
Build libcoap
wget http://downloads.sourceforge.net/project/libcoap/coap-18/libcoap-4.0.3.tar.gz tar -xzf libcoap-4.0.3.tar.gz rm libcoap-4.0.3.tar.gz cd libcoap-4.0.3 autoconf ./configure make sudo cp examples/coap-client /usr/local/bin/ # or copy to ~/bin and set it to your syspath
Get battery voltage:
coap-client -m Get coap://[2001:db8:c001:f00d:221:2eff:ff00:3372]:5683/sensors/battery
Toggle LED:
echo -n "mode=on" | coap-client -m put coap://[2001:db8:c001:f00d:221:2eff:ff00:2704]:5683/actuators/leds?color=r -f - # same with text option for pingtheplug coap-client -m put coap://[2001:db8:c001:f00d:221:2eff:ff00:2704]:5683/actuators/led1 -e "mode=on" echo -n "mode=off" | coap-client -m put coap://[2001:db8:c001:f00d:221:2eff:ff00:2704]:5683/actuators/leds?color=r -f - coap-client -m put coap://[2001:db8:c001:f00d:221:2eff:ff00:2704]:5683/actuators/led1 -e "mode=off"
CoAP using NodeJS
CoAP using Python
coapy
outdated, only supports CoAP-08
aiocoap
Python3 lib using asyncio for coap support without hard dependencies
txThings
https://github.com/siskin/txThings
- based on twisted
- asyncio version in near future
- server and client
- define own CoAP resources easily
- not python3 compatible (yet)
- install twisted from repository or pip
git clone https://github.com/siskin/txThings.git
Server
Preconditions
To accept commands from a node we need a CoAP server providing resources.
txThings provides a example server.py, create a copy to avoid conflicts.
In the current version server.py only listens to IPv4. To change it to IPv6 change the line on the bottom to:
reactor.listenUDP(coap.COAP_PORT, coap.Coap(endpoint), '::')
Afterwards just start the server.py file.
Get your IPv6 address:
ip addr
You should see two IPv6 addresses, one fe80:: and one with our prefix provided by the border router (e.g. abbb::) Use this address to test your server with copper or libcoap.
Short description of the server
class TestResource (resource.CoAPResource): def __init__(self): # init stuff resource.CoAPResource.__init__(self) self.visible = True # some description shown in core self.addParam(resource.LinkParam("title", "Test resource")) def render_GET(self, request): # build response message response = coap.Message(code=coap.CONTENT, payload='response msg') # send it back as success return defer.succeed(response) def render_POST(self, request): # like GET # Get payload message from request (e.g. mode=on) req = request.payload # do some magic with the command # or just echo it response = coap.Message(code=coap.CONTENT, payload=req) return defer.succeed(response)
# create a / resource root = resource.CoAPResource() # subdirectory well_known = resource.CoAPResource() # add subdirectory to / root.putChild('.well-known', well_known) # magically create core info resource from your tree core = CoreResource(root) well_known.putChild('core', core) # create own subdirectory /other/ other = resource.CoAPResource() root.putChild('other', other) # add our test resource as echo to /other/ test = TestResource() other.putChild('echo', test) # start server endpoint = resource.Endpoint(root) reactor.listenUDP(coap.COAP_PORT, coap.Coap(endpoint), '::') reactor.run()
Examples
- quite some hacks
- no warranty ;)
WakeOnLan
from subprocess import Popen class WOLResource(resource.CoAPResource): def __init__(self): resource.CoAPResource.__init__(self) self.visible = True def render_POST(self, request): query = request.payload Popen(('wol', query)) response = coap.Message(code=coap.CONTENT) return defer.succeed(response)
Weather forecast
import requests class WeatherResource (resource.CoAPResource): def __init__(self): resource.CoAPResource.__init__(self) self.visible = True self.addParam(resource.LinkParam("title", "Weather resource")) def render_GET(self, request): # payload used as location name query = request.payload response = coap.Message(code=coap.CONTENT) # Get forecast from openweatherservice r = requests.get('http://api.openweathermap.org/data/2.5/weather?q=%s' % query) # extract temperature from response and convert it from Kelvin to Celsius response.payload = str(float(r.json()['main']['temp'])-273.15) return defer.succeed(response)