This module implements a CoAp Server and Client, operating as both at the same time.
from network import WLAN from network import Coap import uselect import _thread # The callback that handles the responses generated from the requests sent to a CoAp Server def response_callback(code, id_param, type_param, token, payload): print("Code: {}".format(code)) # The ID can be used to pair the requests with the responses print("ID: {}".format(id_param)) print("Type: {}".format(type_param)) print("Token: {}".format(token)) print("Payload: {}".format(payload)) # Thread handling the sockets def socket_thread(p, coap_socket): while True: # Wait for any socket to become available sockets = p.poll() for s in sockets: sock = s[0] event = s[1] if(event & uselect.POLLIN): # Check if the socket belongs to the CoAp module if(sock == coap_socket): # Call Coap.read() which parses the incoming CoAp message Coap.read() # Connect to the network wlan = WLAN(mode=WLAN.STA) wlan.connect('your-ssid', auth=(WLAN.WPA2, 'your-key')) # Initialise the CoAp module Coap.init(str(wlan.ifconfig()[0]), service_discovery=True) # Add a resource with a default value and a plain text content format r = Coap.add_resource("resource1", media_type=Coap.MEDIATYPE_TEXT_PLAIN, value="default_value") # Add an attribute to the resource r.add_attribute("title", "resource1") # Add an attribute to the resource r.add_attribute("ct", str(Coap.MEDIATYPE_TEXT_PLAIN)) # Configure the possible operations on the resource r.callback(Coap.REQUEST_GET | Coap.REQUEST_POST | Coap.REQUEST_PUT, True) # Add a resource without default value, XML content format and E-Tag enabled r = Coap.add_resource("resource2", media_type=Coap.MEDIATYPE_APP_XML, etag=True) # Configure the possible operations on the resource r.callback(Coap.REQUEST_GET | Coap.REQUEST_POST | Coap.REQUEST_PUT | Coap.REQUEST_DELETE, True) # Register the response handler for the requests that the module initiates as a CoAp Client Coap.register_response_handler(response_callback) # Get the UDP socket created for the CoAp module coap_server_socket = Coap.socket() # Create a new poll object p = uselect.poll() # Register the CoAp module's socket to the poll p.register(coap_server_socket, uselect.POLLIN | uselect.POLLHUP | uselect.POLLERR) # Start a new thread which will handle the sockets of "p" poll _thread.start_new_thread(socket_thread, (p, coap_server_socket)) # Send a request to a CoAp server id = Coap.send_request("192.168.0.234", Coap.REQUEST_GET, uri_port=5683, uri_path=".well-known/core", payload="payload", token="token1", include_options=True) print(id)
Initialize the CoAp module.
The arguments are:
address
port
service_discovery
Returns with the socket assigned to the given address and port during Coap.init() (= assigned to the CoAp module).
Creates a resource object and adds it to the CoAp module to operate as a server.
uri
media_type
max_age
value
etag
Media-type argument is one of the standard defined values that is available via CoAp module’s constants.
Entity tag calculation is a simple counter increment between value 1-65535 with overflow, it doesn’t include the value 0. It is incremented each time and the value of the resource is changed.
Removes the resource defined by the uri argument.
Returns with the resource defined by uri argument.
Must be called when a packet is received on the socket assigned to the CoAp module. This function passes on the incoming request, whilst also composing and sending out the response if needed.
Registers a callback function which will be called when a remote CoAp Server responses to the local CoAp client’s request.
callback
code
id_param
type_param
token
payload
Creates and sends a request to a CoAp server.
uri_host
method
Coap.REQUEST_GET
Coap.REQUEST_PUT
Coap.REQUEST_POST
Coap.REQUEST_DELETE
uri_port
uri_path
content_format
Coap.MEDIATYPE_TEXT_PLAIN
Coap.MEDIATYPE_APP_LINK_FORMAT
Coap.MEDIATYPE_APP_XML
Coap.MEDIATYPE_APP_OCTET_STREAM
Coap.MEDIATYPE_APP_RDF_XML
Coap.MEDIATYPE_APP_EXI
Coap.MEDIATYPE_APP_JSON
Coap.MEDIATYPE_APP_CBOR
include_options
The resource class represents a resource in the scope of the CoAp module when acting as a server. A new resource can only be created with the Coap.add_resource function.
Coap.add_resource
Adds a new attribute to the resource. Attributes are used to explain the resource during service discovery.
name
During service discovery, GET request to “.well-know/core”, the attributes are returned with the relevant values. E.g. using the “libcoap’s” command line coap-client to fetch the resource from our server:
coap-client -m get coap://<Coap-Server’s address>/.well-known/core
< /resource2>,< /resource1>;ct=0;title=resource1
Updates or fetches the value of the resource.
To enable or disable a specific operation (GET, PUT, POST, DELETE) on the resource.
operation
enable
During a GET request, only the first occurrence of an ETAG or Accept option is passed on and interpreted; others of the same type are dropped (if any).
During a PUT request, only the first occurrence of an If-Match option is passed on and interpreted; others of the same type are dropped (if any).
Due to limitations of the underlying ESP-IDF/libcoap library, new resources cannot be added via PUT or POST requests.