Blog

This article describes a method to incorporate XBee® / ZigBee® sensor data into an IoT network by implementing a small CoAP proxy server.

ZigBeeCoAPProxyToCloud1

CoAP Proxy Server for XBee / ZigBee sensors

A lot of the equipment used in implementing IoT Wireless Sensor Networks (WSNs) have limited memory, program space, and computing power. These constraints heavily influence the capabilities of the protocols that can be used on these devices. In one of our previous articles Using Arduinos to prototype IoT Solutions → we described the device classes defined by the IETF. It is generally held that Class 0 devices are not able to run a secure IP stack due to resource constraints. A Class 1 device is still resource challenged to run protocols such as HTTP over TLS. Even Class 2 devices which are more normally connected to the Internet, could benefit from a protocol aimed at optimizing memory, data, and processing requirements.

The Constrained Application Protocol (CoAP) is a new binary Web protocol running primarily over UDP, that is focused on making it possible to connect resource-constrained devices (like Class 1) to the Web in all manner of M2M and IoT applications. It has some RESTful patterns familiar to HTTP, like GET, PUT, POST, DELETE, as well as new concepts such as publish/subscribe to observe resource value changes. Devices are addressed using the familiar universal resource identifier (URI) construct currently used in Web applications. CoAP servers are usually devices that generate data, and CoAP clients are generally applications or processes that consume the data.  CoAP can be run securely using Datagram Transport Layer Security (DTLS).
XBeeCoAP

The main standardization work on the CoAP protocol is being done by the Internet Engineering Task Force (IETF) Constrained RESTful Environments (CoRE) Working Group. This includes efforts to put standards in place to make it easier for automatic M2M resource discovery, which will make it possible to integrate a wide variety of devices from different manufacturers based on common metadata.

In this article we describe how to integrate XBee / ZigBee sensors into a CoAP framework by writing a small daemon that uses the serial interface to a ZigBee node to receive the sensor data from all of the devices in the Personal Area Network (PAN) and implements a CoAP server (see Figure #1).

The benefit of this approach is that the ZigBee network based sensor nodes are left unchanged.  In addition, since the daemon also acts as a cache for the sensor data, the ZigBee sensors can be configured to sleep to maximize battery life, without affecting the responsiveness to CoAP clients.

ZigbeeCoapNetworkDiagram

Figure 1: Network Diagram

Another approach would have been to have the proxy server use the conventional HTTP protocol to communicate to the Web, but we wanted to use CoAP for two reasons : 1) We wanted this subsystem to be able to fit within a broader CoAP framework, and 2) we wanted to be able to run the daemon on constrained hardware if required.

Figure 2: zigbee_coap_proxy_server.c

Figure #2 shows the simplified main source code for the C daemon.  It uses the libcoap → library to implement a CoAP server and is modeled on the server.c sample code written by Olaf Bergmann in the libcoap/examples folder.  For clarity, this example demonstrates a simple CoAP GET without the extended ‘observe option’.

The main process is responsible for spinning up a thread (ZigbeeSerialProcessor) to handle the ZigBee interactions and then monitoring and processing the CoAP server message queue.

Figure #4 shows the C code used to handle the interactions with the  XBee ZigBee node  using serial communications over USB.  This has been configured using the Digi XCTU configuration utility to be in ZIGBEE COORDINATE API mode (see Figure #3).

To help recognize what ZigBee devices are on the network, it initially issues a Node Discover (ND) command, and then reads and processes incoming API frames looking for IO Data Sample Receive Packets.

The raw ADC data from the ZigBee sensors is then translated using the appropriate formula and stored along with the time of the measurement and the sensor ID (16 Hex ASCII ZigBee address).

Coap-coordinator

Figure 3: Digi XCTU Configuration for XBee Coordinator

Figure 4: zigbee_data_processor.c

Our daemon and libcoap are implemented as C code so there is a wide variety of platforms that it can be run on.  These can be embedded devices or conventional PCs equipped with an USB port and Ethernet interfaces.

XBeeRaspberryPI

Figure 5: Raspberry PI with XBee Coordinator

RaspberryPIZigBeeCoapProxy

Figure 6: ZigBeeCoAPProxyServer running on Raspbian with sensor logs

In this example we implemented our ZigBee CoAP Proxy Server on a Raspberry PI (Figure #5), running Raspbian (Figure #6) which is a Debian-based version of Linux optimized for it.

A useful tool to use when working with CoAP is the Copper (Cu) add-on for Mozilla Firefox →. It allows you to interact with CoAP servers from within the browser using CoAP URIs.

CoAPWellKnownCore

Figure 7: Firefox Copper (Cu) view showing .well-known core

coap-copper-sensor-list

Figure 8: Firefox Copper (Cu) view showing sensor list

coap-copper-sensor-id

Figure 9: Firefox Copper (Cu) view showing sensor data

Figure #7 shows the response when we perform a GET to our ZigBee Proxy CoAp Server with the .well-known/core URI.  The information returned is a list of the URIs that can be used to access the CoAP server.

Figure #8 shows the response when we perform a GET with the  /sensor URI.  If no query value (?id=) is specified, our CoAP server returns a list of the sensors in the network in XML format.

Figure #9 shows the response when we perform a GET with the  /sensor/id=sensorID  URI.  In this case our CoAP server returns all of the sensor data for the specified ID, as well as the time of the last measurement.

Application development for CoAP can be done with the C libcoap library, or if you would prefer a Java-based framework, you can use Californium (Cf) →.

A search of the Internet will provide you with an up to date list of available CoAP tools.

References

The “XBee” name is a registered trademark of Digi International Inc. The word “ZigBee” is a registered trademark of the ZigBee Alliance. “Raspberry Pi” is a trademark of the Raspberry Pi Foundation.

Leave a Reply

You must be logged in to post a comment.