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.
This sample application implements an example thermometer that works with Azure IoT Hub as follows:
- Sends simulated temperature telemetry data at regular intervals, with a timestamp.
- Sends a simulated thermometer moved telemetry event (with timestamp) when button B is pressed.
- Reports a read-only string serial number device twin.
- 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.
- 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.
- 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.
The sample uses the following Azure Sphere libraries.
Library | Purpose |
---|---|
eventloop | Invokes handlers for timer events. |
gpio | Manages buttons A and B and LED 4 on the device. |
log | Displays messages in the Device Output window during debugging. |
networking | Determines whether the device is connected to the internet. |
storage | Opens the certificate file that is used to authenticate to the IoT Edge device. |
Contents
File/folder | 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. |
CMakePresets.json |
CMake presets file, which contains the information to configure the CMake project. |
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. |
READMEAddDPS.md |
Instructions for changing the device authentication method to use Azure IoT Hub device provisioning service instead of direct authentication. |
READMEAddIoTEdge.md |
Instructions for adding an IoT Edge device that provides a filtering and data processing layer between your Azure Sphere device and IoT Hub. |
READMEAddWebProxy.md |
Instructions for adding web proxy support to the sample |
READMEStartwithIoTCentral.md |
Instructions for customizing the sample to run with Azure IoT Central. |
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 deploying and debugging the application. |
HardwareDefinitions |
Folder containing the hardware definition files for various Azure Sphere boards. |
Prerequisites
This sample requires the following software and hardware:
An Azure Sphere development board that supports the Sample Appliance hardware requirements.
Note: By default, the sample targets the Reference Development Board design, which is implemented by the Seeed Studios MT3620 Development Board. To build the sample for different Azure Sphere hardware, change the value of the TARGET_HARDWARE variable in the
CMakeLists.txt
file. For detailed instructions, see the Hardware Definitions README file.Azure Sphere SDK version 24.03 or higher. At the command prompt, run
az sphere show-sdk-version
to check your SDK version. Install the latest Azure Sphere SDK, if necessary.An Azure subscription. If your organization does not already have a subscription, you can set up a free trial subscription.
Setup
- Set up your Azure Sphere device and development environment as described in Azure Sphere documentation.
- 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.
- Connect your Azure Sphere device to your computer by USB.
- Enable a network interface on your Azure Sphere device and verify that it is connected to the internet.
- Open a command-line interface and enable application development on your device if you have not already done so by entering the
az sphere device enable-development
command. - Configure networking on your device. You must either set up WiFi or set up Ethernet on your development board, depending on the type of network connection you are using.
Build and run the sample
You can build this sample to run with either either Azure IoT Hub or Azure IoT Central:
Follow the Azure IoT Hub instructions, beginning at step 2, to build the sample and run it with Azure IoT Hub.
To change your device authentication method to use the device provisioning service instead of direct authentication, see Connect via Azure IoT Hub device provisioning service.
To add an IoT Edge device that provides a filtering and data processing layer between your Azure Sphere device and IoT Hub, see Connect via Azure IoT Edge.
Follow the Azure IoT Central instructions to build the sample and run it with Azure IoT Central.
Further reference
You may also be interested in the following related projects on the Azure Sphere Gallery:
- AzureIoT_StoreAndForward | This project shows how to add telemetry store and forward capability to the AzureIoT sample.
- AzureIoTMessageWithProperties | A project that shows how to add custom properties to an Azure IoT Hub telemetry message, which can then be used for message routing.
Note: The files in this project can be reused. For an example, please view the DeviceToCloud sample.
Next steps
- To learn more about Azure Sphere application development, see Overview of Azure Sphere applications.
- To access a collection of unmaintained scripts, utilities, and functions, see the Azure Sphere Gallery.