1.. zephyr:code-sample:: ot-coap 2 :name: OpenThread CoAP client and server application 3 :relevant-api: openthread 4 5 Build a Full Thread Device (FTD) CoAP server and client. 6 7Overview 8******** 9 10This sample demonstrates how to use OpenThread CoAP API. 11It can be built to work as a server or as a client. 12 13By running a client and server on two boards, a local Thread network can be created. 14To create the network, OpenThread uses the network key provided with Kconfig. 15Once the boards have been flashed, the network will be 16automatically created and configured. 17 18Once the network is operational, then the client could start interacting with 19the server. 20Every time the user presses the button, the LED on server should toggle. 21 22The source code for this sample application can be found at: 23:zephyr_file:`samples/net/openthread/coap`. 24 25.. note:: 26 This sample uses the OpenThread CoAP API whereas Zephyr has its own CoAP API. 27 So, why are we using the OpenThread CoAP API here ? 28 29 * OpenThread uses it internaly to implement many of its services. 30 * OpenThread CoAP API has a more direct access to radio. 31 32 So by using OpenThread CoAP API instead of Zephyr one, 33 we could expect less overhead although this makes the application less portable. 34 35Building and Running 36******************** 37 38Build the OpenThread FTD CoAP server sample application like this: 39 40.. zephyr-app-commands:: 41 :zephyr-app: samples/net/openthread/coap 42 :board: <board to use> 43 :west-args: -T sample.net.openthread.ftd.coap.server 44 :goals: build 45 :compact: 46 47Build the OpenThread FTD CoAP client sample application like this: 48 49.. zephyr-app-commands:: 50 :zephyr-app: samples/net/openthread/coap 51 :board: <board to use> 52 :west-args: -T sample.net.openthread.ftd.coap.client 53 :goals: build 54 :compact: 55 56Example building CoAP server for the cc1352p7 launchpad: 57 58.. zephyr-app-commands:: 59 :zephyr-app: samples/net/openthread/coap 60 :host-os: unix 61 :board: cc1352p7_lp 62 :west-args: -T sample.net.openthread.ftd.coap.server 63 :goals: build flash 64 :compact: 65 66Example building CoAP client for the cc1352p7 launchpad: 67 68.. zephyr-app-commands:: 69 :zephyr-app: samples/net/openthread/coap 70 :host-os: unix 71 :board: cc1352p7_lp 72 :west-args: -T sample.net.openthread.ftd.coap.client 73 :goals: build flash 74 :compact: 75 76Checking Thread network state 77***************************** 78 79Open a console on both server and client boards then check the sate: 80 81.. code-block:: console 82 83 server:~$ ot state 84 router 85 Done 86 87A valid state could be child, router or leader. 88 89Once Thread network is operational, you could start using client. 90 91Controlling server board's LED using a button on client board 92************************************************************* 93 94There is nothing to do once Thread network is operational. 95Just press the button sw0 on the client and you should see led0 toggling. 96 97The client uses a broadcast address to request CoAP server to toggle the LED. 98It does not know the address of the server so if there is a second server 99on the network, then the LED of the second board will toggle too. 100 101Controlling server board's LED from a computer 102********************************************** 103 104Although we use OpenThread CoAP API, we could interact with any CoAP client 105or server available on network. In this example, we are going to control the 106LED from a computer that is not in the Thread network. 107This requires an `OpenThread Border Router`_ with NAT64 support enabled on the same network. 108 109First, check that the server (or the client) is connected to the otbr and 110can use NAT64: 111 112.. code-block:: console 113 114 server:~$ ot netdata show 115 router 116 Done 117 Prefixes: 118 fd6f:cb3a:802:1::/64 paos low dc00 119 Routes: 120 fc00::/7 sa med dc00 121 fd6f:cb3a:802:2:0:0::/96 sn low dc00 122 Services: 123 44970 01 14000500000e10 s dc00 0 124 44970 5d fd78b9ce54779c6eb5484d062c3b5b22d120 s dc00 1 125 Contexts: 126 fd6f:cb3a:802:1::/64 1 c 127 Commissioning: 128 11426 - - - 129 Done 130 131Prefixes show the IPv6 prefies that could be used by device outside the 132Thread network to contact devices on Thread network. 133 134We should have an IPv6 address using the prefix: 135 136.. code-block:: console 137 138 server:~$ ot ipaddr 139 fd78:b9ce:5477:9c6e:0:ff:fe00:a800 140 fd6f:cb3a:802:1:f0ec:c1e2:c1bb:744 141 fd78:b9ce:5477:9c6e:75b8:386c:1f79:1013 142 fe80:0:0:0:50d1:bed5:6e6e:ad75 143 Done 144 145fd6f:cb3a:802:1:f0ec:c1e2:c1bb:744 is the IPv6 address that could be used 146to contact the CoAP server outside of the Thread network. 147 148We could also check that we could access internet from Thread network: 149 150.. code-block:: console 151 152 server:~$ ot ping 8.8.8.8 153 Pinging synthesized IPv6 address: fd6f:cb3a:802:2:0:0:808:808 154 16 bytes from fd6f:cb3a:802:2:0:0:808:808: icmp_seq=1 hlim=114 time=36ms 155 1 packets transmitted, 1 packets received. Packet loss = 0.0%. Round-trip min/avg/max = 36/36.0/36 ms. 156 Done 157 158If everything is working, then, we could start controlling the LED from a computer. 159To do that, let's use aiocoap-client, a tool written in python. 160First, install it: 161 162.. code-block:: shell 163 164 pip install aiocoap 165 166 167Then, send a request to the server to toggle the LED: 168 169.. code-block:: shell 170 171 aiocoap-client -m PUT --payload '{"led_id":0,"state":2}' coap://[fd6f:cb3a:802:1:f0ec:c1e2:c1bb:744]/led 172 173The LED state should have changed. 174 175 176.. _OpenThread Border Router: https://openthread.io/codelabs/openthread-border-router-nat64 177 178Controlling server board's LED using shell command 179************************************************** 180 181The example also provides a shell command to control the LED on the server from the client. 182 183To toggle the LED: 184 185.. code-block:: 186 187 $client:~$ ot_coap led set 0 toggle 188 189The LED state should have changed. 190 191Same as for the button, this uses the broadcast address by default. 192To control the LED of a specific server, we can use it IPv6 address: 193 194.. code-block:: 195 196 $client:~$ ot_coap led set 0 toggle fd6f:cb3a:802:1:f0ec:c1e2:c1bb:744 197