This folder contains simple samples showing how to use the various features of the Microsoft Azure IoT Hub service from a device running Java code.
- Simple send sample: Shows how to connect and send messages to IoT Hub, passing the protocol of your choices as a parameter.
- Simple send/receive sample: Shows how to connect then send and receive messages to and from IoT Hub, passing the protocol of your choices as a parameter.
- Simple send serialized messages sample: Shows how to connect and send serialized messages to IoT Hub, passing the protocol of your choices as a parameter.
- Simple sample handling messages received: : Shows how to connect to IoT Hub and manage messages received from IoT Hub, passing the protocol of your choices as a parameter.
- Android sample: Shows how to connect to IoT Hub send and receive messages to and from IoT Hub.
Note that the samples for Windows and Linux use Maven.
In order to run the device samples on Linux or Windows, you will first need the following prerequisites:
Prepare your platform following the instructions here to install Java and Maven.
-
Clone the repository or download the sample (the one you want to try) project's folder on your device.
-
In a command line, navigate to the sample folder and build:
{sample root}/>mvn install -DskipTests
-
Navigate to the folder containing the executable JAR file for the sample and run the sample as follows:
For example, the executable JAR file for sending and receving event, send-receive-sample can be found at:
{sample root}/target/send-event-{version}-with-deps.jar
Navigate to the directory containing the jar. Run the sample using the following command:
java -jar ./send-receive-sample-{version}-with-deps.jar "{connection string}" "{number of requests to send}" "{https or amqps or mqtt or amqps_ws }"
Note that the double quotes around each argument are required, but the braces '{' and '}' should be removed.
Samples use the following command line arguments:
- [Device connection string] -
HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>
- [Number of requests to send]: For example, 5
- [
https | amqps | mqtt | amqps_ws
]: For example, amqps_ws (AMQP over WebSocket) - [Path to certificate to enable 1-way authentication]: For example,
certs\ms.der
optional argument
Path to certificate is an optional argument and would be needed in case you want to point it to the local copy of the Server side certificate. Please note that this option is used by client for validating Root CA sent by Azure IoT Hub Server as part of TLS handshake. It is for 1-way TLS authentication and is not for specifying client side certificate (2-way TLS authentication).
- Download and install Android Studio
- Load and build the sample located in device\samples\android-sample in Android Studio (you will need to clone or copy the repository)
- Sample has dependency on the remote library
iot-device-client
. It is currently set to use the latest version of the library. If you want to choose a different version, please updatedevice\iot-device-samples\android-sample\app\build.gradle
file to point to the version you want to use. For list of available versions search [Maven Repository][maven-repository]
Java client SDK uses Apache Log4j Java logging framework. To enable logging in the Azure IoT Java client SDK via your application, you can set log4j.properties to the appropriate values of your choice. As an example, sample log4j.properties file is located at iot-device-samples\send-receive-sample\src\main\resources.