The REST APIs for IoT Hub offer programmatic access to the device, messaging, and job services, as well as the resource provider, in IoT Hub. With the Azure IoT Toolkit extension for Visual Studio Code, you could easily use IoT Hub REST APIs in VS Code, no extra toolchain needed! Let’s see how quick it is to send a device-to-cloud message to Azure IoT Hub!
Prerequisites
- If you do not have an IoT Hub, you could follow our tutorial to create IoT Hub and device.
- Azure IoT Toolkit extension to help you do all the things when develop with Azure IoT Hub
- REST Client extension to help you send HTTP request in VS Code
Prepare HTTP request
In VS Code, create a file called d2c.http with below CURL request, and replace {iothub-name} and {device-id}:
curl --request POST --url 'https://{iothub-name}.azure-devices.net/devices/{device-id}/messages/events?api-version=2018-06-30' --header 'authorization: {sas-token}' --data 'Hello IoT Hub!'
Generate SAS token
Right-click your device and select Generate SAS Token for Device. The SAS token is created and copied to clipboard, then replace {sas-token} d2c.http in the with the SAS token.
Send HTTP request
- Right-click your device and select Start Monitoring D2C Message to monitor the device-to-cloud message
- In d2c.http file, click ‘Send Request’ link to call Azure IoT Hub REST API to send a d2c message
- In right response area, you could see the HTTP response which is HTTP 204 meaning the message is sent successfuly
- In the Output Channel of Azure IoT Toolkit, you could see the IoT Hub receive the ‘Hello IoT Hub!’ message
Without any extra toolchain, you could do all the things to call Azure IoT REST APIs in Visual Studio Code. If you want to learn more about how Azure IoT Toolkit extension helps you to develop with Azure IoT Hub, checkout our Wiki Page to see the full features and tutorials.
Useful Resources:
- Channel 9 video: Walkthrough of Azure IoT Toolkit extension
- Use Azure IoT Toolkit to send and receive messages between your device and IoT Hub
- Handy Tool When You Develop With Azure IoT
- Use VS Code as IoT Hub Device Simulator
- Create and control an IoT device connected to an IoT hub (Node.js)
- Create and control an IoT device connected to an IoT hub (.NET)
Thanks for this post Jun. Can you help me understand if/how I can send a message from the cloud (app) to a device, with a similar curl request? As I understand it from this page https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-c2d there should be a /messages/devicebound endpoint to use? But how does one specify the device? (Currently I just want to try this in the terminal, not in VS code.)