nRF5 SDK for Thread and Zigbee v4.1.0
Thread thethings.iO Cloud CoAP Client Example

Table of Contents

This information applies to the following SoCs: nRF52833 and nRF52840.

This example demonstrates the interaction between a network node and a remote cloud service. The interaction is managed using the Nordic Border Router Internet connectivity feature. The Constrained Application Protocol (CoAP) is used to exchange the data between the cloud and the client node.

As part of this example, the client node manages the following resources in the cloud:

The example uses Button 3 and Button 4 on the nRF52 SoC. Each time they are pressed:

  1. The client node decreases (Button 3) or increases (Button 4) the value the counter resource.
  2. The client node uploads new counter resource value to the could.

The chip temperature value is automatically updated in the cloud if the chip temperature value changes between measurements done at 5-second intervals.

The example supports the OpenThread CLI interface. This allows the user to manually configure the client node and troubleshoot connectivity. For more information on the CLI interface, see OpenThread CLI reference.

Note
The following testing procedure uses the nRF52840 device.

Setup

To successfully run the example, make sure you have the required hardware and software.

Required hardware

You need the following hardware to test the example:

Required software

You need the following software to test the example:

Before testing, the thethings.iO cloud service and and client node must be set up. See Configuring cloud and Configuring client node.

Button and LED assignments

Client LED assignments
The client uses LED assignments as described in Thread BSP LED and button reference for the following LEDs:
  • LED1 – Used to indicate the network connection state.
Client button assignments
The following buttons reserved for user application purposes are assigned in this example by the client:
  • Button 3 – Decrement the counter value and send it to the cloud.
  • Button 4 – Increment the counter value and send it to the cloud.

Configuring cloud

This setup describes the required configuration steps after you have created thethings.iO account (see thethings.iO Getting Started).

Note
The control panel of thethings.iO may change. Refer to thethings.iO Getting Started for latest information on how to activate a new Thing.

Activating a Thing

After you have created the account, complete the following steps:

  1. Log in to the main control panel.
  2. Go to the Things Manager page.
  3. Click Create a new product and set the following values:
    • Name: CoAP Cloud
    • Format: JSON
  4. Click Activate More Things to activate the Thing. The new thing will appear in the list on the webpage.

Copying the token

Copy the Thing ID value. You will use it to replace the {THINGS-TOKEN} placeholder string in the example source code when Configuring client node.

Note
Before the Temperature and Counter resources are visible in the Dashboard, push the first value using one of the following options:
  • using the nRF52840 Development Kit
  • manually – for example, with a curl request:
    curl -i -H "Accept: application/json" -H "Content-Type: application/json" -d '{"values":[{"key":"temp","value":"0"},{"key":"counter","value":"0"}]}' -X POST "https://api.thethings.io/v2/things/{THINGS-TOKEN} " -k

Adding widgets

To observe the resource values updated by the cloud example on thethings.iO webpage, add widgets to the Dashboard page:

  1. Go to the Dashboard page.
  2. Click Add Widget (+) and fill the following values:
    • Widget name: Temperature
      • Data source: Thing Resource
      • Resource: temp
      • Widget type: Gauge
      • Realtime checkbox checked
  3. Click Add Widget (+) again and fill the following values:
    • Widget name: Counter
      • Data source: Thing Resource
      • Resource: counter
      • Widget type: Gauge
      • Realtime checkbox checked
Note
Checking the Realtime checkbox is needed to immediately observe the changes of value on the Dashboard page.

Configuring client node

To set up the CoAP cloud client node, complete the following procedure:

  1. Replace the {THING-TOKEN} string in the example source code with the token generated when Configuring cloud.
  2. Build the example using the provided makefile.
  3. Flash the example to one of the nRF52840 development kits.
    • On startup, the nodes will automatically enter the network with default parameters.
      • The first device that was turned on will become a Leader of the network, while the rest of the nodes will become Children and Routers.
    • The CoAP cloud client node indicates the network state on LED1 (see Thread BSP LED and button reference).
  4. Connect to the CoAP client board using a serial terminal application PuTTy. The application uses the following UART settings:
    • Baud rate: 115200
    • 8 data bits
    • 1 stop bit
    • No parity
    • HW flow control: None
  5. Ping an IP address on the Internet to verify the Internet connectivity. Use the following command:
    > ping 64:ff9b::0101:0101
    16 bytes from 64:ff9b:0:0:0:0:101:101: icmp_seq=1 hlim=57 time=20ms
Note
The IP address in this command uses the Well Known Prefix (64:ff9b::) for translating between IPv4 and IPv6 addresses. The pinged IPv4 address in this case is 1.1.1.1. If the ping does not succeed, see Ping request issues.

After these steps, the CoAP client node is ready to send data to the cloud.

Ping request issues

If the ping request does not succeed, use one of the following methods.

Ensure that the CoAP cloud client device is attached to the network

Run the following command:

> state
router
Done

The printed state must be one of the following:

If the state is not one of these values and instead shows as disabled, use the following commands:

> channel 11
Done
> panid 0xabcd
Done
> ifconfig up
Done
> thread start
Done

Ensure that the Border Router has Internet connectivity

Use this option if the first method does not work.

  1. Run the following command:
    pi@raspberrypi:~$ ping www.google.com
    PING www.google.com(waw02s17-in-x04.1e100.net (2a00:1450:401b:807::2004)) 56 data bytes
    64 bytes from waw02s17-in-x04.1e100.net (2a00:1450:401b:807::2004): icmp_seq=1 ttl=54 time=45.1 ms
  2. Depending on the outcome of the command:
    • If the ping fails, there is no Internet connectivity from the Border Router: verify your Ethernet or WiFi connection.
    • If the ping succeeds and Border Router has Internet connectivity, ensure that both the CoAP cloud client and Border Router are connected to the same network:
      1. Type the following command on the CoAP cloud client node to view its network data:
        > dataset active get
        Active Timestamp: 0
        Channel: 11
        Channel Mask: 07fff800
        Ext PAN ID: dead00beef00cafe
        Mesh Local Prefix: fdde:ad00:beef:0/64
        Master Key: 00112233445566778899aabbccddeeff
        Network Name: OpenThread
        PAN ID: 0xabcd
        PSKc: 752c6d6379598257ee3b30e4adfb1b9b
        Security Policy: 672, onrcb
        Done
      2. Type the following command on Raspberry Pi:
        pi@raspberrypi:~$ sudo wpanctl status
        wpan0 => [
        "NCP:State" => "associated"
        "Daemon:Enabled" => true
        "NCP:Version" => "OPENTHREAD/20180926-00772-gaec0e0b2; NRF52840; Jul 26 2019 12:59:58"
        "Daemon:Version" => "0.08.00d (; Jun 11 2019 09:10:53)"
        "Config:NCP:DriverName" => "spinel"
        "NCP:HardwareAddress" => [F4CE36AB5DCD784D]
        "NCP:Channel" => 11
        "Network:NodeType" => "leader"
        "Network:Name" => "OpenThread"
        "Network:XPANID" => 0xDEAD00BEEF00CAFE
        "Network:PANID" => 0xABCD
        "IPv6:LinkLocalAddress" => "fe80::40c0:3d70:d901:2060"
        "IPv6:MeshLocalAddress" => "fdde:ad00:beef:0:20cf:d113:d24b:c25f"
        "IPv6:MeshLocalPrefix" => "fdde:ad00:beef::/64"
        "com.nestlabs.internal:Network:AllowingJoin" => false
        ]
      3. Compare the network data with the data on the Border Router, in particular:
        • Channel
        • PAN ID
        • Ext PAN ID (XPANID)
        • Network Name
      4. If the Channel and the PAN ID values differ, use the channel and panid CLI commands to set the exact values obtained from Border Router on the CoAP cloud client node:
        > thread stop
        Done
        > channel 11
        Done
        > panid 0xabcd
        Done
        > thread start
        Done
      5. If Ext PAN ID (XPANID) or Network Name differ, perform the Thread commissioning process. See Performing on-mesh commissioning.

Testing

Complete the following procedure after Configuring cloud and Configuring client node :

  1. Program the nRF52840 development kit with this Thread Cloud CoAP Client example.
  2. Run Nordic's Thread Border Router.
  3. Open the thethings.iO Dashboard page.
  4. Turn on the node and wait until LED1 stops blinking. The node will connect to the existing network.
  5. Press the Button 3 to decrease the counter value on the client node.
  6. Press the Button 4 to increase the counter value on the client node.
  7. Observe the Dashboard page to see that the value has changed.

Documentation feedback | Developer Zone | Subscribe | Updated