Learn how to send telemetry data from your IoT devices to the cloud.
This tutorial guides you through the process of sending messages from an IoT device to your Azure IoT Hub. This is a fundamental concept for building robust IoT solutions, enabling real-time monitoring, data analysis, and triggering actions based on device data.
We will cover:
Before you begin, ensure you have the following:
First, you need to provision an IoT Hub instance in Azure. This service acts as the central message hub for your IoT devices.
Once deployed, navigate to your IoT Hub resource. You will need the Hostname and the Primary connection string for the service later.
Each device needs to be registered with your IoT Hub to establish a secure connection. We'll register a simulated device for this tutorial.
my-sample-device).Note down the Device ID and the Primary connection string for this device. This string contains credentials for your device to authenticate with IoT Hub.
We'll use Node.js to send messages. If you don't have it installed, download it from nodejs.org.
Create a new project directory and initialize a Node.js project:
mkdir iot-device-sender
cd iot-device-sender
npm init -y
Install the Azure IoT device SDK:
npm install azure-iot-device azure-iot-device-mqtt
Create a file named sendTelemetry.js and add the following code. Replace the placeholder connection string with the one you copied in Step 2.
const Client = require('azure-iot-device').Client;
const Protocol = require('azure-iot-device-mqtt').Mqtt;
// Replace with your device's primary connection string
const connectionString = 'HostName=YOUR_IOT_HUB_HOSTNAME;DeviceId=YOUR_DEVICE_ID;SharedAccessKey=YOUR_DEVICE_KEY';
const client = Client.fromConnectionString(connectionString, Protocol);
function sendTelemetryData() {
const temperature = 20 + (Math.random() * 10); // Simulate temperature between 20 and 30
const humidity = 50 + (Math.random() * 20); // Simulate humidity between 50 and 70
const message = {
temperature: temperature.toFixed(2),
humidity: humidity.toFixed(2)
};
const messageString = JSON.stringify(message);
client.sendEvent(messageString, function (err) {
if (err) {
console.error('Failed to send message: ' + err.toString());
} else {
console.log('Message sent: ' + messageString);
}
});
}
// Connect the client
client.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Client connected');
// Send telemetry data every 5 seconds
setInterval(sendTelemetryData, 5000);
}
});
Note: Remember to replace 'YOUR_IOT_HUB_HOSTNAME;DeviceId=YOUR_DEVICE_ID;SharedAccessKey=YOUR_DEVICE_KEY' with your actual device connection string.
Open your terminal in the iot-device-sender directory and run the script:
node sendTelemetry.js
You should see output indicating that messages are being sent.
You can monitor the incoming messages to your IoT Hub using various methods. For this tutorial, we'll use the Azure CLI.
az loginaz account set --subscription "Your Subscription Name"YOUR_IOT_HUB_NAME with the name of your IoT Hub.az iot hub monitor-events --hub-name YOUR_IOT_HUB_NAME
As your device sends messages, you will see them appear in the Azure CLI output, formatted as JSON.
Alternatively, you can use tools like IoT Explorer from the Visual Studio Code Marketplace for a more graphical interface to monitor messages and device twins.
Congratulations! You have successfully set up an Azure IoT Hub, registered a device, and sent telemetry data from a simulated device to the cloud. This forms the foundation for building more complex IoT solutions, such as real-time dashboards, device management, and cloud-to-device messaging.
From here, you can explore:
Continue your journey with Azure IoT and unlock the full potential of your connected devices!