In this project, you will create an Azure IoT Hub, connect IoT DevKit to it, and collect the temperature and humidity data from sensors and send the data to the IoT hub.

What you learn

  • How to create an IoT Hub and register a device for MXChip IoT DevKit.
  • How to collect sensor data by running a sample application on MXChip IoT DevKit.
  • How to send the sensor data to your IoT hub.

What you need

Finish the Getting Started Guide to:

  • Have your IoT evKit connected to Wi-Fi
  • Prepare the development environment

An active Azure subscription. If you do not have one, you can register via one of the methods:

Use Visual Studio Code

Visual Studio Code is the primary development tool for IoT DevKit. If this is the first time you learn about Visual Studio Code, there are a series of Getting Started tutorials are dedicated for it.

Step 1. Open the project folder

A. Start VS Code

Make sure your IoT DevKit is not connected. Start VS Code first and connect the IoT DevKit to your computer. VS Code automatically finds the IoT DevKit and opens an introduction page:

Introduction page

Notice: Occasionally, when you launch VS Code, you are prompted with error that cannot find Arduino IDE or related board package. Close VS Code, launch Arduino IDE once again, and VS Code should locate Arduino IDE path correctly.

B. Open the Arduino Examples folder

Expand left side ARDUINO EXAMPLES section, browse to Examples for MXCHIP AZ3166 > AzureIoT, and select GetStarted. This will open a new VS Code window with project folder in it.

Arduino Examples tab

If you happen to close the pane, you can reopen it. Use Ctrl+Shift+P (macOS: Cmd+Shift+P) to open the command palette, type Arduino, and then find and select Arduino: Examples.

Step 2. Provision Azure services

  1. In the solution window, run your task through Ctrl+P (macOS: Cmd+P) by entering task cloud-provision: In the VS Code terminal, an interactive command line guides you through provisioning the required Azure services:

    Interactive command line

    Notice: If the page hangs in the loading status when trying to sign in to Azure, please check this FAQ steps to solve it.

  2. When a list of Azure subscriptions display in the terminal, use the up and down arrow keys to select the Azure subscription that you will use to provision the Azure IoT Hub. Then press Enter to confirm the selected subscription.

    Select Azure subscription

  3. Provision IoT Hub

    • If you’ve provisioned IoT Hub(s) before, a list of IoT Hubs associated with the subscription appears in the terminal. Please use up and down arrow keys to select one from the list and press Enter to confirm.

      Select an existing IoT Hub

      Select existing Free IoT Hub

      Wait several minutes to finish ARM Template deployment and IoT Hub Device provision. Then press any key to exit the terminal.

      Select an existing IoT Hub done

      Finish selecting IoT Hub

    • If you don’t have any IoT Hub, please follow the steps to create a free IoT Hub:

      • Resource group provision: Use up and down arrow keys to choose a resource group you’ve created before or select Create New… to provision a new resource group. Complete the resource group provision according to the command line prompt.

        Select or create Azure resource group

        If you choose to create a new resource group:

        • Use up and down arrow keys to select region for a new resource group provision, and then press Enter to confirm.

          Select region for a new resource group

          Select region for resource group provision

        • Input a name for new resource group.

          Input name for resource group

          Input a name for resource group provision

      • IoT Hub provision:

        Enter a name for your new IoT Hub, and press Enter to confirm.

        Input a name for IoT Hub

        Input a name for IoT Hub provision

        Wait several minutes to finish ARM Template deployment and IoT Hub Device provision. Then press any key to exit the terminal.

        Provision Azure IoT Hub done

        Finish cloud provision

Step 3. Build and upload Arduino sketch

Windows

  1. Use Ctrl+P to run task config-device-connection.

  2. The terminal will ask you whether you want to use connection string that retrieves from task cloud-provision step. You could also input your own device connection string by clicking ‘Create New…’

  3. The terminal prompts you to enter configuration mode. To do so, hold down button A, then push and release the reset button. The screen displays the DevKit id and ‘Configuration’. Verification and upload of the Arduino sketch

  4. After task config-device-connection finished, click F1 to load VS Code commands and select Arduino: Upload, then VS Code starts verifying and uploading the Arduino sketch: Verification and upload of the Arduino sketch

The DevKit reboots and starts running the code.

Notice: Occasionally, you get error “Error: AZ3166: Unknown package”. This is due to the board package index is not refreshed. Check this FAQ steps to solve it.

macOS

  1. Put DevKit into configuration mode: Hold down button A, then push and release the reset button. The screen displays ‘Configuration’.

  2. Use Cmd+P to run task config-device-connection.

  3. The terminal will ask you whether you want to use connection string that retrieves from task cloud-provision step. You could also input your own device connection string by clicking ‘Create New…’ device-upload

  4. After task config-device-connection finished, click Cmd + shift + p to load VS Code commands and select Arduino: Upload, then VS Code starts verifying and uploading the Arduino sketch: device-upload

The DevKit reboots and starts running the code.

Notice: Occasionally, you get error “Error: AZ3166: Unknown package”. This is due to the board package index is not refreshed. Check this FAQ steps to solve it.

Test the project

In VS Code, following these steps to open and set up the Serial Monitor:

  1. Click the COM[X] on the status bar to set the right COM port with STMicroelectronics: com-port

  2. Click power plug icon on the status bar to open the Serial Monitor: serial-monitor

The sample application is running successfully when you see the following results:

  • The Serial Monitor displays the same information as the content in the screenshot below.
  • The LED on MXChip IoT DevKit is blinking.

Final output in VS Code

Check data in IoT Hub

In VS Code, you can use Azure IoT Toolkit to monitor device to cloud (D2C) messages in IoT Hub.

  1. Log in Azure portal.

  2. Find the IoT Hub you just created. azure-portal-iot-hub

  3. In the Shared access policies pane, click the iothubowner policy, and note down the Connection string of your IoT hub. azure-portal-iot-hub-conn-string

  4. In VS Code, click the Extensions icon in the Activity Bar: vscode-extensions-icon

  5. Search for Azure IoT Toolkit and install it. azure-iot-toolkit-installing

  6. Expand IoT Hub Devices on the bottom left corner. azure-iot-toolkit-iot-hub-devices

  7. Click Set IoT Hub Connection String in context menu. azure-iot-toolkit-iot-hub-conn-string

  8. Click IoT: Start monitoring D2C message in context menu.

  9. In OUTPUT pane, you can see the incoming D2C messages to IoT Hub. azure-iot-toolkit-output-console

Problems and feedback

If you encounter problems, you can find FAQs if you encounter problems or reach out to us from the channels below.

Next steps

You have successfully connected an MXChip IoT DevKit to your IoT hub, and you have sent the captured sensor data to your IoT hub.

To continue getting started with Azure IoT Hub and to explore other IoT scenarios, see: