Skip to content

Commit b8075f8

Browse files
committed
thread: add usage examples
- Included Receive CoAP Data example. - Included Send CoAP Data example. - Included Receive IPv6 Data example. - Included Send IPv6 Data example. Signed-off-by: Héctor González <hector.gonzalez@digi.com>
1 parent 8602abc commit b8075f8

File tree

14 files changed

+1008
-0
lines changed

14 files changed

+1008
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
Introduction
2+
------------
3+
This sample Java application shows how CoAP data messages are received from
4+
another XBee device connected to a network.
5+
6+
The application prints the received CoAP data to the standard output in ASCII
7+
and hexadecimal formats after the sender's IPv6 address.
8+
9+
NOTE: This example uses the Thread device (ThreadDevice) class.
10+
11+
12+
Files
13+
-----
14+
* com.digi.xbee.api.receivecoapdata.MainApp.java:
15+
Main application class. It instantiates a Thread device and establishes a
16+
serial connection with it.
17+
18+
* com.digi.xbee.api.receivecoapdata.MyCoAPDataReceiveListener.java:
19+
Class that handles the received CoAP data messages.
20+
21+
22+
Requirements
23+
------------
24+
To run this example you will need:
25+
26+
* At least two XBee Thread radios in API mode and their corresponding carrier
27+
boards (XBIB or equivalent).
28+
* The XCTU application (available at www.digi.com/xctu).
29+
30+
31+
Compatible protocols
32+
--------------------
33+
* Thread
34+
35+
36+
Example setup
37+
-------------
38+
1) Plug the XBee radios into the XBee adapters and connect them to your
39+
computer's USB or serial ports.
40+
41+
2) Ensure that the modules are in API mode and connected to the same network.
42+
For further information on how to perform this task, read the
43+
'Configuring Your XBee Modules' topic of the Getting Started guide.
44+
45+
3) Set the port and baud rate of the receiver XBee radio in the MainApp
46+
class.
47+
If you configured the modules in the previous step with the XCTU, you
48+
will see the port number and baud rate in the 'Port' label of the device
49+
on the left view.
50+
51+
52+
Running the example
53+
-------------------
54+
First, build and launch the application. Then, you need to send a data frame
55+
to the receiver (local) module from another device. Follow the steps below to
56+
do so:
57+
58+
1) Launch the XCTU application.
59+
60+
2) Add the sender (remote) XBee module to the XCTU, specifying its port
61+
settings.
62+
63+
3) Once the module is added, change to the 'Consoles' working mode and
64+
open the serial connection.
65+
66+
4) Create and add a frame using the 'Frames Generator' tool with the
67+
following parameters:
68+
69+
- Protocol: Select the protocol of your device.
70+
- Frame type: 0x1C - CoAP Tx Request
71+
- Frame ID: 01
72+
- Transmit options: 02
73+
- Method PUT [03]
74+
- IPv6 128-bit dest. address: The IPv6 address ('MY', 'GA' or 'LA') of
75+
the receiver module in hexadecimal format.
76+
- URI length: 05
77+
- URI: XB/TX
78+
- Payload (ASCII): Hello XBee!
79+
80+
5) Send this frame by selecting it and clicking the 'Send selected Frame'
81+
button.
82+
83+
When the CoAP data frame is sent, verify that a line with the IPv6 address and
84+
the data included in the 'Payload' field is printed out in the console of the
85+
launched application:
86+
87+
CoAP data from XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX >> 48 65 6C 6C 6F 20 58 42 65 65 21 | Hello XBee!
88+
89+
- Where XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX is the IPv6 address of the
90+
remote XBee device that sent the IPv6 data frame.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.digi.xbee</groupId>
6+
<artifactId>xbee-java-library-parent</artifactId>
7+
<version>1.2.0</version>
8+
<relativePath>../../../../pom.xml</relativePath>
9+
</parent>
10+
<artifactId>receive-coap-data-sample</artifactId>
11+
<packaging>jar</packaging>
12+
13+
<name>Receive CoAP Data Sample</name>
14+
15+
<properties>
16+
<rxtx.native.libs.dir>rxtx-native-libs</rxtx.native.libs.dir>
17+
</properties>
18+
19+
<build>
20+
<sourceDirectory>src</sourceDirectory>
21+
<directory>../../../../target/examples/communication/coap/ReceiveCoAPDataSample</directory>
22+
<plugins>
23+
<plugin>
24+
<groupId>org.codehaus.mojo</groupId>
25+
<artifactId>exec-maven-plugin</artifactId>
26+
<version>${exec.maven.version}</version>
27+
<configuration>
28+
<executable>java</executable>
29+
<arguments>
30+
<argument>-Djava.library.path=${project.build.directory}/../../../${rxtx.native.libs.dir}</argument>
31+
<argument>-classpath</argument>
32+
<!-- automatically creates the classpath using all project dependencies,
33+
also adding the project build directory -->
34+
<classpath/>
35+
<argument>com.digi.xbee.api.receivecoapdata.MainApp</argument>
36+
</arguments>
37+
</configuration>
38+
</plugin>
39+
<!-- This is needed to skip this submodule from staging -->
40+
<plugin>
41+
<groupId>org.sonatype.plugins</groupId>
42+
<artifactId>nexus-staging-maven-plugin</artifactId>
43+
<version>${nexus.staging.maven.plugin.version}</version>
44+
<extensions>true</extensions>
45+
<configuration>
46+
<skipNexusStagingDeployMojo>true</skipNexusStagingDeployMojo>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
<dependencies>
53+
<dependency>
54+
<groupId>com.digi.xbee</groupId>
55+
<artifactId>xbee-java-library</artifactId>
56+
<version>${project.version}</version>
57+
<type>jar</type>
58+
</dependency>
59+
</dependencies>
60+
</project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2017, Digi International Inc.
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, you can obtain one at http://mozilla.org/MPL/2.0/.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
package com.digi.xbee.api.receivecoapdata;
17+
18+
import com.digi.xbee.api.ThreadDevice;
19+
import com.digi.xbee.api.exceptions.XBeeException;
20+
21+
/**
22+
* XBee Java Library Receive IPv6 Data sample application.
23+
*
24+
* <p>This example registers a listener to manage the received IPv6 data.</p>
25+
*
26+
* <p>For a complete description on the example, refer to the 'ReadMe.txt' file
27+
* included in the root directory.</p>
28+
*/
29+
public class MainApp {
30+
31+
/* Constants */
32+
33+
// TODO Replace with the serial port where your receiver module is connected.
34+
private static final String PORT = "COM1";
35+
// TODO Replace with the baud rate of you receiver module.
36+
private static final int BAUD_RATE = 9600;
37+
38+
/**
39+
* Application main method.
40+
*
41+
* @param args Command line arguments.
42+
*/
43+
public static void main(String[] args) {
44+
System.out.println(" +----------------------------------------------+");
45+
System.out.println(" | XBee Java Library Receive CoAP Data Sample |");
46+
System.out.println(" +----------------------------------------------+\n");
47+
48+
ThreadDevice myDevice = new ThreadDevice(PORT, BAUD_RATE);
49+
50+
try {
51+
myDevice.open();
52+
53+
if (!myDevice.isConnected()) {
54+
System.err.println(">> Error: the device is not connected to the network");
55+
return;
56+
}
57+
58+
myDevice.addIPDataListener(new MyCoAPDataReceiveListener());
59+
60+
System.out.println("\n>> Waiting for data...");
61+
62+
} catch (XBeeException e) {
63+
e.printStackTrace();
64+
myDevice.close();
65+
System.exit(1);
66+
}
67+
}
68+
}
69+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2017, Digi International Inc.
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, you can obtain one at http://mozilla.org/MPL/2.0/.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
package com.digi.xbee.api.receivecoapdata;
17+
18+
import com.digi.xbee.api.listeners.IIPDataReceiveListener;
19+
import com.digi.xbee.api.models.IPMessage;
20+
import com.digi.xbee.api.utils.HexUtils;
21+
22+
/**
23+
* Class to manage the CoAP received data that was sent by other modules.
24+
*
25+
* <p>Acts as an IP data listener by implementing the
26+
* {@link IIPDataReceiveListener} interface, and is notified when new IP data
27+
* for the module is received.</p>
28+
*
29+
* @see IIPDataReceiveListener
30+
*
31+
*/
32+
public class MyCoAPDataReceiveListener implements IIPDataReceiveListener {
33+
/*
34+
* (non-Javadoc)
35+
* @see com.digi.xbee.api.listeners.IIPDataReceiveListener#ipDataReceived(com.digi.xbee.api.models.IPMessage)
36+
*/
37+
@Override
38+
public void ipDataReceived(IPMessage ipMessage) {
39+
System.out.format("%s data from %s >> %s | %s%n",
40+
ipMessage.getProtocol().getName(), ipMessage.getHostAddress(),
41+
HexUtils.prettyHexString(HexUtils.byteArrayToHexString(ipMessage.getData())),
42+
ipMessage.getDataString());
43+
}
44+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
Introduction
2+
------------
3+
This sample Java application shows how to send CoAP data from an IPv6 device to
4+
another one connected to a network using the XBee Java Library.
5+
6+
The application sends CoAP data to another Thread device on the network with a
7+
specific IPv6 address and port number.
8+
9+
NOTE: This example uses the Thread device (ThreadDevice) class.
10+
11+
12+
Files
13+
-----
14+
* com.digi.xbee.api.sendcoapdata.MainApp.java:
15+
Main application class. It instantiates a Thread device, establishes a
16+
serial connection with it and sends the CoAP data to the XBee device with
17+
the IPv6 address and port specified. Finally it prints out the result of the
18+
sent operation.
19+
20+
21+
Requirements
22+
------------
23+
To run this example you will need:
24+
25+
* At least two XBee Thread radios in API mode and their corresponding carrier
26+
boards (XBIB or equivalent).
27+
* The XCTU application (available at www.digi.com/xctu).
28+
29+
30+
Compatible protocols
31+
--------------------
32+
* Thread
33+
34+
35+
Example setup
36+
-------------
37+
1) Plug the XBee radios into the XBee adapters and connect them to your
38+
computer's USB or serial ports.
39+
40+
2) Ensure that the modules are in API mode and connected to the same network.
41+
For further information on how to perform this task, read the
42+
'Configuring Your XBee Modules' topic of the Getting Started guide.
43+
44+
3) Set the port and baud rate of the sender (local) XBee radio in the
45+
MainApp class.
46+
If you configured the modules in the previous step with the XCTU, you
47+
will see the port number and baud rate in the 'Port' label of the device
48+
on the left view.
49+
50+
4) Set the destination IPv6 address in the MainApp class. You can find it
51+
by reading the 'MY' ('GA' or 'LA').
52+
53+
54+
Running the example
55+
-------------------
56+
First, build the application. Then, you need to set up XCTU to see the data
57+
received by the remote XBee device. Follow these steps to do so:
58+
59+
1) Launch the XCTU application.
60+
61+
2) Add the remote XBee module to the XCTU, specifying its port settings.
62+
63+
3) Switch to the 'Consoles' working mode and open the serial connection
64+
so you can see the data when it is received.
65+
66+
Finally, launch the sample application, some CoAP data is sent to the configured
67+
remote XBee device. When that happens, a line with the result of the operation
68+
is printed to the standard output:
69+
70+
Sending CoAP data to XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX >> 48 65 6C 6C 6F 20 58 42 65 65 21 | Hello XBee!... Success
71+
72+
- Where XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX is the IPv6 address address
73+
of the remote XBee device.
74+
75+
Verify that in the XCTU console a new RX IPv6 frame has been received by the
76+
remote XBee device. Select it and review the details, some of the details
77+
will be similar to:
78+
79+
- Start delimiter: 7E
80+
- Length: Variable
81+
- Frame type: 9A (IPv6 Rx Response)
82+
- Destination address: The XBee receiver's IP address.
83+
- Source address: The XBee sender's IP address.
84+
- Destination port: The configured port number.
85+
- Source port: A random port chosen by the sender module.
86+
- Protocol: 03 (CoAP)
87+
- Status: 00 (Reserved)
88+
- RF data: 48 65 6C 6C 6F 20 58 42 65 65 21
89+
Hello XBee!

0 commit comments

Comments
 (0)