Azure IoT Code Samples
Explore a comprehensive collection of code samples and SDKs designed to help you build robust and scalable Internet of Things solutions on Microsoft Azure.
Getting Started: For a quick start, check out our "Hello World" examples for connecting a device to Azure IoT Hub.
Device SDK Samples
Samples demonstrating how to connect various devices (from microcontrollers to industrial gateways) to Azure IoT Hub using our official SDKs.
Python SDK Samples
- Connecting a Raspberry Pi to IoT Hub (Python)
- Sending telemetry from a Python application
- Receiving device-to-cloud messages (Python)
- Managing device twins with Python
# Example: Basic device connection in Python
from azure.iot.device import IoTHubDeviceClient
CONNECTION_STRING = "YOUR_DEVICE_CONNECTION_STRING"
def main():
client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
client.connect()
print("Device connected to IoT Hub")
client.disconnect()
if __name__ == "__main__":
main()
.NET SDK Samples
- Connecting an IoT device using C#
- Asynchronous telemetry sending (.NET)
- Handling cloud-to-device messages in C#
// Example: Basic device connection in .NET
using Azure.Messaging.IotHub.Device;
var connectionString = "YOUR_DEVICE_CONNECTION_STRING";
var client = new IoTHubDeviceClient(connectionString);
await client.ConnectAsync();
Console.WriteLine("Device connected to IoT Hub");
await client.DisconnectAsync();
Node.js SDK Samples
// Example: Basic device connection in Node.js
const { Client } = require('azure-iot-device');
const { Mqtt } = require('azure-iot-device-mqtt');
const connectionString = 'YOUR_DEVICE_CONNECTION_STRING';
const client = Client.fromConnectionString(connectionString, Mqtt);
client.open(err => {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Device connected to IoT Hub');
client.close();
}
});
Solution & Service Samples
Samples for building end-to-end IoT solutions, integrating with other Azure services, and implementing advanced IoT scenarios.
- Setting up Azure IoT Edge deployment (YAML)
- Building a real-time analytics dashboard with Azure Stream Analytics
- Provisioning devices using Azure IoT Hub Device Provisioning Service (DPS)
- Interacting with Azure Digital Twins API
- Creating IoT alerts with Azure Functions
Security Note: Always manage your device connection strings securely. Consider using Azure Key Vault or managed identities where appropriate.
Azure IoT Edge Samples
Samples for deploying and managing custom modules on Azure IoT Edge devices.