Use iothub-explorer to send and receive messages between your device and IoT Hub
[!NOTE] Before you start this tutorial, make sure you’ve completed Setup your device. By Setup your device, you set up your IoT device and IoT hub, and deploy a sample application to run on your device. The application sends collected sensor data to your IoT hub.
iothub-explorer has a handful of commands that makes IoT Hub management easier. This tutorial focuses on how to use iothub-explorer to send and receive messages between your device and your IoT hub.
What you will learn
You learn how to use iothub-explorer to monitor device-to-cloud messages and to send cloud-to-device messages. Device-to-cloud messages could be sensor data that your device collects and then sends to your IoT hub. Cloud-to-device messages could be commands that your IoT hub sends to your device to blink an LED that is connected to your device.
What you will do
- Use iothub-explorer to monitor device-to-cloud messages.
- Use iothub-explorer to send cloud-to-device messages.
What you need
- Tutorial Setup your device completed which covers the following requirements:
- An active Azure subscription.
- An Azure IoT hub under your subscription.
- A client application that sends messages to your Azure IoT hub.
- iothub-explorer. (Install iothub-explorer)
Monitor device-to-cloud messages
To monitor messages that are sent from your device to your IoT hub, follow these steps:
- Open a console window.
Run the following command:
iothub-explorer monitor-events <device-id> --login "<IoTHubConnectionString>"
[!Note] Get
<device-id>
and<IoTHubConnectionString>
from your IoT hub. Make sure you've finished the previous tutorial. Or you can try to useiothub-explorer monitor-events <device-id> --login "HostName=<my-hub>.azure-devices.net;SharedAccessKeyName=<my-policy>;SharedAccessKey=<my-policy-key>"
if you haveHostName
,SharedAccessKeyName
andSharedAccessKey
.
Send cloud-to-device messages
To send a message from your IoT hub to your device, follow these steps:
- Open a console window.
Start a session on your IoT hub by running the following command:
iothub-explorer login <IoTHubConnectionString>
Send a message to your device by running the following command:
iothub-explorer send <device-id> <message>
The command blinks the LED that is connected to your device and sends the message to your device.
[!Note] There is no need for the device to send a separate ack command back to your IoT hub upon receiving the message.
Next steps
You’ve learned how to monitor device-to-cloud messages and send cloud-to-device messages between your IoT device and Azure IoT Hub.