Azure Sphere – Azure IoT

This sample demonstrates how to use the Azure IoT SDK C APIs in an Azure Sphere application to communicate with Azure IoT Hub or Azure IoT Central.

IMPORTANT: Before you can compile and run the sample, you must configure an Azure IoT Hub application, and customize the sample to connect to it. Carefully follow the instructions in this README and in its linked topics.

Overview

This sample application implements an example thermometer that works with Azure IoT Hub as follows:

  1. Sends simulated temperature telemetry data at regular intervals.
  2. Sends a simulated thermometer moved telemetry event when button B is pressed.
  3. Reports a read-only string serial number device twin.
  4. Synchronizes a read/write boolean Thermometer Telemetry Upload Enabled device twin.
    • This status is reflected by one of the LEDs on the MT3620 development board.
    • This status can be turned on/off via the cloud or on the device itself by pressing button A.
  5. Implements a display alert direct method. For example, a cloud solution could call this method when it receives a temperature reading that is higher than a given threshold.
  6. Declares that it implements the Azure Sphere Example Thermometer model, consisting of this telemetry, device twin, and direct method by sending its Azure IoT Plug and Play (PnP) model ID upon connection.

Application libraries

This sample uses the following Azure Sphere application libraries:

Library Purpose
log Displays messages in the Device Output window during debugging
networking Determines whether the device is connected to the internet
gpio Manages buttons A and B and LED 4 on the device
storage Opens the certificate file that is used to authenticate to the IoT Edge device
EventLoop Invoke handlers for timer events

Contents

File Description
app_manifest.json Application manifest file, which describes the resources.
CMakeLists.txt CMake configuration file, which Contains the project information and is required for all builds.
CMakeSettings.json JSON file for configuring Visual Studio to use CMake with the correct command-line options.
launch.vs.json JSON file that tells Visual Studio how to deploy and debug the application.
LICENSE.txt The license for this sample application.
main.c Main C source code file. README.md This README file.
READMEStartwithIoTCentral.md Instructions for customizing the sample to run with Azure IoT Central.
READMEAddIoTEdge.md Instructions for customizing the sample to run with an Azure IoT Edge device.
READMEStartwithIoTHub.md Instructions for customizing the sample to run with Azure IoT Hub.
.vscode Folder containing the JSON files that configure Visual Studio Code for building, debugging, and deploying the application.
HardwareDefinitions Folder containing the hardware definition files for various Azure Sphere boards.

Prerequisites

This sample requires the following software and hardware:

Preparation

Complete the following steps to prepare your environment:

  1. Set up your Azure Sphere device and development environment as described in Azure Sphere documentation.

  2. Clone the Azure Sphere samples repository and find the AzureIoT sample in the AzureIoT folder or download the zip file from the Microsoft samples browser.

  3. Connect your Azure Sphere device to your computer by USB.

  4. Enable a network interface on your Azure Sphere device and verify that it is connected to the internet.

  5. Open the Azure Sphere command-line tool and enable application development on your device if you have not already done so by entering the following command:

    azsphere device enable-development

Use Ethernet instead of Wi-Fi

By default, this sample runs over a Wi-Fi connection to the internet. To use Ethernet instead, follow the Ethernet setup instructions.

Run the sample

You can use either Azure IoT Hub or Azure IoT Central to configure the sample:

  1. Run the sample by connecting to an Azure IoT Hub.

  2. Run the sample by connecting to an Azure IoT Central application.

Next steps