Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions examples/configuration/ConnectToAccessPointSample/ReadMe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Introduction
------------
This sample Java application shows how to scan, connect to an access point and
configure an XBee Wi-Fi module using the XBee Java Library.

NOTE: This example uses the Wi-Fi device (WiFiDevice) class as it is the only
device able to connect to access points.


Files
-----
* com.digi.xbee.api.connecttoaccesspoint.MainApp.java:
Main application class. It instantiates a Wi-Fi device, establishes a
serial connection with it, connects to the specified access point and
prints some addressing information.


Requirements
------------
To run this example you will need:

* One XBee Wi-Fi radio in API mode and its corresponding carrier board (XBIB
or XBee Development Board).
* The XCTU application (available at www.digi.com/xctu).
* An access point to connect to.


Example setup
-------------
1) Plug the Wi-Fi radio into the XBee adapter and connect it to your
computer's USB or serial port.

2) Ensure that the module is in API mode.
For further information on how to perform this task, read the
'Configuring Your XBee Modules' topic of the Getting Started guide.

3) Set the port and baud rate of the Wi-Fi radio in the MainApp class.
If you configured the module in the previous step with XCTU, you will see
the port number and baud rate in the 'Port' label of the device on the
left view.

4) Set the SSID and password of the access point you are going to connect to
in the 'SSID' and 'PASSWORD' variables of the MainApp class.


Running the example
-------------------
First, build and launch the application. To test the functionality, check
that the following message is printed out in the console of the launched
application:

">> Successfully connected to '<SSID>'"

That message indicates that the module is connected to the access point
correctly. Other addressing information should be printed out as well.
50 changes: 50 additions & 0 deletions examples/configuration/ConnectToAccessPointSample/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.digi.xbee</groupId>
<artifactId>xbjlib-parent</artifactId>
<version>1.1.1</version>
<relativePath>../../../pom.xml</relativePath>
</parent>
<artifactId>connect-to-access-point-sample</artifactId>
<packaging>jar</packaging>

<name>Connect to Access Point Sample</name>

<properties>
<rxtx.native.libs.dir>rxtx-native-libs</rxtx.native.libs.dir>
</properties>

<build>
<sourceDirectory>src</sourceDirectory>
<directory>../../../target/examples/configuration/ConnectToAccessPointSample</directory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec.maven.version}</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Djava.library.path=${project.build.directory}/../../../${rxtx.native.libs.dir}</argument>
<argument>-classpath</argument>
<!-- automatically creates the classpath using all project dependencies,
also adding the project build directory -->
<classpath/>
<argument>com.digi.xbee.api.connecttoaccesspoint.MainApp</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.digi.xbee</groupId>
<artifactId>xbjlib</artifactId>
<version>${project.version}</version>
<type>jar</type>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright (c) 2016 Digi International Inc.,
* All rights not expressly granted are reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
* =======================================================================
*/
package com.digi.xbee.api.connecttoaccesspoint;

import com.digi.xbee.api.WiFiDevice;
import com.digi.xbee.api.exceptions.XBeeException;
import com.digi.xbee.api.models.AccessPoint;
import com.digi.xbee.api.models.IPAddressingMode;

/**
* XBee Java Library Connect to Access Point sample application.
*
* <p>This example configures the Wi-Fi module to connect to a specific access
* point and reads its addressing settings.</p>
*
* <p>For a complete description on the example, refer to the 'ReadMe.txt' file
* included in the root directory.</p>
*/
public class MainApp {

/* Constants */

// TODO Replace with the serial port where your module is connected.
private static final String PORT = "COM1";
// TODO Replace with the baud rate of your module.
private static final int BAUD_RATE = 9600;
// TODO Fill with the SSID of the access point you want to connect to.
private static final String SSID = "";
// TODO Fill with the password of the access point you want to connect to.
private static final String PASSWORD = "";

/**
* Application main method.
*
* @param args Command line arguments.
*/
public static void main(String[] args) {
System.out.println(" +----------------------------------------------------+");
System.out.println(" | XBee Java Library Connect to Access Point Sample |");
System.out.println(" +----------------------------------------------------+\n");

WiFiDevice myDevice = new WiFiDevice(PORT, BAUD_RATE);

try {
myDevice.open();

myDevice.disconnect();

myDevice.setIPAddressingMode(IPAddressingMode.DHCP);

AccessPoint accessPoint = myDevice.getAccessPoint(SSID);

if (accessPoint == null) {
System.err.format(">> Error: could not find any access point with SSID '%s'\n", SSID);
return;
}

if (myDevice.connect(accessPoint, PASSWORD))
System.out.format(">> Successfully connected to '%s'\n\n", SSID);
else {
System.err.format(">> Error: could not connect to '%s'\n", SSID);
return;
}

System.out.format(" - IP addressing mode: %s\n", myDevice.getIPAddressingMode());
System.out.format(" - IP address: %s\n", myDevice.getIPAddress().getHostAddress());
System.out.format(" - IP address mask: %s\n", myDevice.getIPAddressMask().getHostAddress());
System.out.format(" - Gateway IP address: %s\n", myDevice.getGatewayIPAddress().getHostAddress());
System.out.format(" - DNS address: %s\n\n", myDevice.getDNSAddress().getHostAddress());

} catch (XBeeException e) {
e.printStackTrace();
System.exit(1);
} finally {
myDevice.close();
}
}

}