Skip to content

Commit 6eeca4b

Browse files
committed
examples: add Send UDP Data example
It is located in the 'communication/ip' category. Signed-off-by: Ruben Moral <Ruben.Moral@digi.com>
1 parent 7711386 commit 6eeca4b

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Introduction
2+
------------
3+
This sample Java application shows how to send UDP data from an IP device to
4+
another one connected to the Internet using the XBee Java Library.
5+
6+
The application sends UDP data to the specified IP address and port number.
7+
8+
NOTE: This example uses the NB-IoT device (NBIoTDevice) class, but it can be
9+
applied to other Internet capable XBee device classes such as
10+
CellularDevice.
11+
12+
13+
Files
14+
-----
15+
* com.digi.xbee.api.sendudpdata.MainApp.java:
16+
Main application class. It instantiates a NB-IoT device, establishes a
17+
serial connection with it and sends the UDP data to the specified IP
18+
address and port. Finally it prints out the result of the sent operation.
19+
20+
21+
Requirements
22+
------------
23+
To run this example you will need:
24+
25+
* One XBee NB-IoT radio in API mode and its corresponding carrier board
26+
(XBIB or equivalent).
27+
* The XCTU application (available at www.digi.com/xctu).
28+
29+
30+
Compatible protocols
31+
--------------------
32+
* Cellular
33+
* Cellular NB-IoT
34+
* Wi-Fi
35+
36+
37+
Example setup
38+
-------------
39+
1) Plug the XBee radio into the XBee adapter and connect it to your
40+
computer's USB or serial ports.
41+
42+
2) Ensure that the module is in API mode and connected to the Internet.
43+
For further information on how to perform this task, read the
44+
'Configuring Your XBee Modules' topic of the Getting Started guide.
45+
46+
3) Set the port and baud rate of the XBee radio in the MainApp class.
47+
If you configured the module in the previous step with XCTU, you will
48+
see the port number and baud rate in the 'Port' label of the device
49+
on the left view.
50+
51+
4) Set the destination IP address and port number in the MainApp class.
52+
53+
54+
Running the example
55+
-------------------
56+
First, build and launch the application. As soon as the application is
57+
executed, it will send the UDP packet to the specified IP address and port
58+
number. If the transmission was sent succesfully, the following message will
59+
be printed out in the console:
60+
61+
Sending data to 192.168.1.2:9750 >> 48 65 6C 6C 6F 20 58 42 65 65 21 | Hello XBee!... Success
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>send-udp-data-sample</artifactId>
11+
<packaging>jar</packaging>
12+
13+
<name>Send UDP 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/ip/SendUDPDataSample</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.sendudpdata.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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.sendudpdata;
17+
18+
import java.net.Inet4Address;
19+
import java.net.UnknownHostException;
20+
21+
import com.digi.xbee.api.NBIoTDevice;
22+
import com.digi.xbee.api.exceptions.XBeeException;
23+
import com.digi.xbee.api.models.IPProtocol;
24+
import com.digi.xbee.api.utils.HexUtils;
25+
26+
/**
27+
* XBee Java Library Send UDP Data sample application.
28+
*
29+
* <p>This example sends UDP data to the specified IP address and port number.
30+
* </p>
31+
*
32+
* <p>For a complete description on the example, refer to the 'ReadMe.txt' file
33+
* included in the root directory.</p>
34+
*/
35+
public class MainApp {
36+
37+
/* Constants */
38+
39+
// TODO Replace with the serial port where your module is connected to.
40+
private static final String PORT = "COM1";
41+
// TODO Replace with the baud rate of your module.
42+
private static final int BAUD_RATE = 9600;
43+
// TODO Replace with the destination IP address.
44+
private static final String DEST_IP_ADDRESS = "192.168.1.2";
45+
// TODO Replace with the destination port number (in decimal format).
46+
private static final int DEST_PORT = 9750;
47+
48+
private static final IPProtocol PROTOCOL = IPProtocol.UDP;
49+
50+
private static final String DATA_TO_SEND = "Hello XBee!";
51+
52+
/**
53+
* Application main method.
54+
*
55+
* @param args Command line arguments.
56+
*/
57+
public static void main(String[] args) {
58+
System.out.println(" +------------------------------------------+");
59+
System.out.println(" | XBee Java Library Send UDP Data Sample |");
60+
System.out.println(" +------------------------------------------+\n");
61+
62+
// For XBee Cellular modules, use the CellularDevice class instead.
63+
NBIoTDevice myDevice = new NBIoTDevice(PORT, BAUD_RATE);
64+
byte[] dataToSend = DATA_TO_SEND.getBytes();
65+
66+
try {
67+
myDevice.open();
68+
69+
if (!myDevice.isConnected()) {
70+
System.err.println(">> Error: the device is not connected to the network");
71+
return;
72+
}
73+
74+
System.out.format("Sending data to %s:%d >> %s | %s... ", DEST_IP_ADDRESS, DEST_PORT,
75+
HexUtils.prettyHexString(HexUtils.byteArrayToHexString(dataToSend)),
76+
new String(dataToSend));
77+
78+
myDevice.sendIPData((Inet4Address) Inet4Address.getByName(DEST_IP_ADDRESS),
79+
DEST_PORT, PROTOCOL, dataToSend);
80+
81+
System.out.println("Success");
82+
83+
} catch (XBeeException | UnknownHostException e) {
84+
System.out.println("Error");
85+
e.printStackTrace();
86+
System.exit(1);
87+
} finally {
88+
myDevice.close();
89+
}
90+
}
91+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<module>examples/communication/ip/ReceiveIPDataSample</module>
8787
<module>examples/communication/ip/SendIPDataSample</module>
8888
<module>examples/communication/ip/KnockKnockSample</module>
89+
<module>examples/communication/ip/SendUDPDataSample</module>
8990
<module>examples/configuration/ManageCommonParametersSample</module>
9091
<module>examples/configuration/ResetModuleSample</module>
9192
<module>examples/configuration/SetAndGetParametersSample</module>

0 commit comments

Comments
 (0)