This folder contains simple samples showing how to use the various features of the Microsoft Azure IoT Hub service from a back-end application running Java code.
- Device manager sample: Shows how to work with the device ID registry of IoT Hub.
- Service client sample: Shows how to send Cloud to Device messages through IoT Hub.
Note that the below samples use Maven.
In order to run the device samples on Linux or Windows, you will first need the following prerequisites:
- Setup your IoT hub
- After creating the IoT Hub, retreive the iothubowner connection string (mentioned in instructions to create the hub).
Prepare your platform following the instructions here to install Java and Maven
You need to first clone the repository or download the sample project folder on your machine.
-
Preparing the Device Manager Sample application:
- Navigate to the main sample file for Device Manager It can be found at: {IoT Java SDK root}\service\iot-service-samples\device-manager-sample\src\main\java\samples\com\microsoft\azure\sdk\iot\service\sdk\SampleUtils.java
- Locate the following code in the file:
public static final String iotHubConnectionString = "[sample iot hub connection string goes here]"; public static final String storageConnectionString = "[sample storage connection string goes here]"; public static final String deviceId = "[Device name goes here]"; public static final String exportFileLocation = "[Insert local folder here - something like C:\\foldername\\]";
- Replace "[sample iot hub connection string goes here]" with the connection information for iothubowner user (see Prerequisites)].
- Replace "[Device name goes here]" with the name of the device you want to create, read. update or delete. Note: The storageConnectionString and exportFileLocation values are only used by the import and export samples.
- Locate the main function in the DeviceManagerSample.java file:
Notice there are function calls implemented for each CRUD operation and they called from the main function in order.
public static void main(String[] args) throws IOException, URISyntaxException, Exception
- Pick the operations you want to run, and comment out the others. Notice that the add operation requires you to provide some keys to associate with the device.
-
Building the Device Manager Sample application:
To build the Device Manager Sample application using Maven, at a command prompt navigate to the service\iot-service-samples\device-manager-sample folder. Then execute the following command and check for build errors:
mvn clean package
-
Running the Device Manager Sample application:
To run the Device Manager Sample application using Maven, execute the following command.
mvn exec:java -Dexec.mainClass="DeviceManagerSample"
You can verify the result of your operation by using Device Explorer or iothub-explorer tool.
-
Preparing the Service Client Sample application:
- Navigate to the main sample file for Service Client. It can be found at: {IoT SDK root}\service\iot-service-samples\service-client-sample\src\main\java\samples\com\microsoft\azure\sdk\iot\service\sdk\ServiceClientSample.java
- Locate the following code in the file:
private static final String connectionString = "[Connection string goes here]"; private static final String deviceId = "[Device name goes here]";
- Replace "[Connection string goes here]" with the connection information for iothubowner user (see Prerequisites)].
- Replace "[Device name goes here]" with the name of the device you are using.
- Locate the main function:
public static void main(String[] args) throws IOException, URISyntaxException, Exception
- Update the value of the local variable "commandMessage" to contain the message you want to send to the device.
-
Building the Service Client Sample application:
To build the Service Client Sample application using Maven, at a command prompt navigate to the \service\iot-service-samples\service-client-sample folder. Then execute the following command and check for build errors:
mvn clean package
-
Running the Service Client Sample application:
To run the Service Client Sample application using Maven, execute the following command.
mvn exec:java -Dexec.mainClass="ServiceClientSample"
You can verify the result of your operation by using Device Explorer or iothub-explorer tool.