Skip to content

Latest commit

 

History

History
 
 

iot-service-samples

Samples for the Azure IoT Service SDK for Java

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.

List of samples

How to run the samples on Linux or Windows

Note that the below samples use Maven.

Prerequisites

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).

Setup environment

Prepare your platform following the instructions here to install Java and Maven

Get and run the samples

You need to first clone the repository or download the sample project folder on your machine.

Build and run the Device Manager Sample application:

  1. Preparing the Device Manager Sample application:

    1. 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
    2. 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\\]";
    1. Replace "[sample iot hub connection string goes here]" with the connection information for iothubowner user (see Prerequisites)].
    2. 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.
    3. Locate the main function in the DeviceManagerSample.java file:
       public static void main(String[] args) throws IOException, URISyntaxException, Exception
      Notice there are function calls implemented for each CRUD operation and they called from the main function in order.
    4. 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.
  2. 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
    
  3. 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.

Build and run the Service Client Sample application:

  1. Preparing the Service Client Sample application:

    1. 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
    2. 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]";
    1. Replace "[Connection string goes here]" with the connection information for iothubowner user (see Prerequisites)].
    2. Replace "[Device name goes here]" with the name of the device you are using.
    3. Locate the main function:
    public static void main(String[] args) throws IOException, URISyntaxException, Exception
    1. Update the value of the local variable "commandMessage" to contain the message you want to send to the device.
  2. 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
    
  3. 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.