Connecting to Azure IoT Hub
Step-by-step guide to connect your M2M Gateway to Microsoft Azure IoT Hub for cloud-based monitoring and management.
Connecting M2M Gateway to Azure IoT Hub
Seamless integration with Microsoft Azure IoT Hub for enterprise-grade cloud connectivity
Video Tutorial: Complete Azure Setup
Prerequisites
- CONPROSYS M2M Gateway device
- Azure account with active subscription
- IoT Hub resource created in Azure Portal
Azure Portal showing IoT Hub configuration dashboard
Step 1: Create IoT Hub
- Login to Azure Portal
- Click "Create a resource"
- Search for "IoT Hub"
- Configure:
- Subscription: Your subscription
- Resource Group: Create new or use existing
- Region: Select nearest region
- IoT Hub Name: Unique name
- Tier: S1 (recommended for production)
Pricing Tiers Comparison
| Tier | Messages/Day | Cost/Month | Use Case |
|---|---|---|---|
| F1 | 8,000 | Free | Development & testing |
| S1 | 400,000 | $25 | Production - small scale |
| S2 | 6,000,000 | $250 | Production - medium scale |
| S3 | 300,000,000 | $2,500 | Production - large scale |
Step 2: Register Device
Using Azure Portal
- Go to your IoT Hub
- Click "Devices" under "Device management"
- Click "Add Device"
- Enter Device ID (e.g., gateway-001)
- Leave authentication as "Symmetric key"
- Click "Save"
Using Azure CLI
Open PowerShell and run:
bash# Install Azure CLI (if not installed)
winget install Microsoft.AzureCLI
# Login to Azure
az login
# Create IoT Hub device
az iot hub device-identity create \
--hub-name your-iot-hub \
--device-id gateway-001
# Get connection string
az iot hub device-identity connection-string show \
--hub-name your-iot-hub \
--device-id gateway-001 \
--output table
Step 3: Configure M2M Gateway
Web-based configuration interface for cloud connectivity
Access Web Interface
- Open browser to gateway IP address (default: 192.168.1.1)
- Login with admin credentials
- Navigate to "Cloud" > "Azure IoT Hub"
Connection Configuration Code
python# Python example for programmatic configuration
import requests
import json
GATEWAY_IP = "192.168.1.1"
ADMIN_USER = "admin"
ADMIN_PASS = "your-password"
# Azure IoT Hub connection string
CONNECTION_STRING = "HostName=your-hub.azure-devices.net;DeviceId=gateway-001;SharedAccessKey=your-key"
def configure_azure_connection():
config = {
"enabled": True,
"connectionString": CONNECTION_STRING,
"transmissionInterval": 60, # seconds
"batchSize": 100,
"retryEnabled": True,
"bufferSize": 1000,
"tlsEnabled": True
}
response = requests.post(
f"http://{GATEWAY_IP}/api/cloud/azure",
json=config,
auth=(ADMIN_USER, ADMIN_PASS)
)
if response.status_code == 200:
print(" Azure IoT Hub configured successfully")
print(json.dumps(response.json(), indent=2))
else:
print(f" Configuration failed: {response.text}")
configure_azure_connection()
Configure Data Transmission
- Interval: 60 seconds (adjustable 1-3600s)
- Batch Size: 100 messages (max 500)
- Retry Logic: Enabled with exponential backoff
- Buffer Size: 1000 messages (disk-backed)
- Compression: Optional gzip compression
Step 4: Verify Connection
Real-time connection monitoring and telemetry dashboard
Check Gateway Status
- Status indicator should show "Connected" (green)
- View message count in dashboard
- Check last transmission timestamp
- Monitor queue depth
Check Azure Portal
- Go to IoT Hub resource
- Click on your device ID
- View "Messages" tab
- Should see incoming telemetry with timestamps
Monitoring Script
javascript// Node.js script to monitor Azure IoT Hub messages
const { EventHubConsumerClient } = require("@azure/event-hubs");
const connectionString = "YOUR_EVENT_HUB_COMPATIBLE_CONNECTION_STRING";
const consumerGroup = "$Default";
async function monitorMessages() {
const client = new EventHubConsumerClient(consumerGroup, connectionString);
console.log(" Monitoring Azure IoT Hub messages...");
const subscription = client.subscribe({
processEvents: async (events, context) => {
for (const event of events) {
console.log(" Message received:");
console.log(" Device:", event.systemProperties["iothub-connection-device-id"]);
console.log(" Time:", event.systemProperties["iothub-enqueuedtime"]);
console.log(" Body:", event.body);
console.log("---");
}
},
processError: async (err, context) => {
console.error(" Error:", err);
}
});
// Run for 60 seconds
await new Promise(resolve => setTimeout(resolve, 60000));
await subscription.close();
await client.close();
}
monitorMessages();
Step 5: View Data
Using Azure IoT Explorer
- Download Azure IoT Explorer
- Add your IoT Hub connection string
- Select your device from the list
- View telemetry in real-time with charts
Using Azure Portal
- Go to IoT Hub
- Click "Monitor" > "Metrics"
- Add metric: "Telemetry messages sent"
- View in line charts with time range selector
Message Format
Default JSON format sent to Azure:
json{
"deviceId": "gateway-001",
"timestamp": "2025-11-18T10:30:00Z",
"location": "Factory-A",
"values": {
"temperature": 25.5,
"humidity": 60.0,
"pressure": 1013.25,
"vibration": 0.05,
"status": "running"
},
"metadata": {
"firmware": "2.4.1",
"signalStrength": -65
}
}
Advanced Configuration
Custom Message Properties
javascript// Add custom properties for Azure IoT Hub routing
const message = {
body: telemetryData,
properties: {
location: "Factory-A",
equipment: "Compressor-01",
alarm: "false",
priority: "normal"
}
};
Device Twin Configuration
json{
"desired": {
"telemetryInterval": 60,
"alertThresholds": {
"temperature": 80,
"pressure": 10
},
"enabled": true
},
"reported": {
"telemetryInterval": 60,
"firmwareVersion": "2.4.1",
"lastReboot": "2025-11-18T08:00:00Z"
}
}
Download Resources
- Azure IoT Hub Setup Guide - Complete walkthrough (4.2MB)
- Security Best Practices - Production deployment guide (2.8MB)
- Sample Code Repository - Python, Node.js, C# examples (3.5MB)
- Troubleshooting Checklist - Common issues resolved (1.2MB)
Troubleshooting
Connection Failed
- Verify connection string is correct (no spaces)
- Check firewall allows port 8883 (MQTT over TLS)
- Ensure gateway has internet access
- Verify DNS resolution works
- Check system time is synchronized (NTP)
Messages Not Arriving
- Check gateway buffer usage in web UI
- Verify Azure IoT Hub quotas not exceeded
- Review message size limits (256KB max per message)
- Confirm device is not throttled
- Check Azure service health status
Authentication Errors
- Regenerate device keys in Azure Portal
- Update connection string in gateway
- Verify device is enabled in IoT Hub
- Check clock sync on gateway (±15 min tolerance)
- Ensure SAS token not expired
Security Best Practices
End-to-end security with TLS encryption and certificate authentication
- Use TLS/SSL: Always enabled by default (port 8883)
- Rotate Keys: Change device keys every 90 days
- Monitor Access: Review device activity logs daily
- Use DPS: Device Provisioning Service for zero-touch provisioning
- Certificate Auth: Use X.509 certificates for production
- Network Isolation: Use VPN or private endpoints
- RBAC: Implement Role-Based Access Control
Next Steps
Download Resources
Azure IoT Hub Setup Guide
Complete walkthrough
4.2 MB
Security Best Practices
Production deployment guide
2.8 MB
Sample Code Repository
Python, Node.js, C# examples
3.5 MB
Troubleshooting Checklist
Common issues resolved
1.2 MB
Azure IoT Hub Setup Guide
Complete walkthrough
4.2 MB
Security Best Practices
Production deployment guide
2.8 MB
Sample Code Repository
Python, Node.js, C# examples
3.5 MB
Troubleshooting Checklist
Common issues resolved
1.2 MB
