Added device registration doc and functional description to station_example_main.c

This commit is contained in:
advait 2025-04-17 20:14:46 -07:00
parent a9a32d4899
commit 807b2eb041
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,35 @@
# Device Registration and SAS Token Generation for Azure IoT Hub
## 🔧 Prerequisites
- Azure Subscription
- Azure IoT Hub created
- Azure CLI installed
- Device created in IoT Hub
---
## 1⃣ Register a Device
```bash
az login
az account set --subscription "<your-subscription-id>"
az iot hub device-identity create --hub-name <your-hub-name> --device-id <your-device-id>
2⃣ Get Device Connection String
az iot hub device-identity show-connection-string --hub-name <your-hub-name> --device-id <your-device-id>
3⃣ Generate SAS Token
import base64, hmac, hashlib, time, urllib.parse
def generate_sas_token(uri, key, expiry=3600):
ttl = int(time.time()) + expiry
sign_key = f"{urllib.parse.quote_plus(uri)}\n{ttl}"
signature = base64.b64encode(hmac.new(base64.b64decode(key), sign_key.encode(), hashlib.sha256).digest())
return f"SharedAccessSignature sr={uri}&sig={urllib.parse.quote_plus(signature.decode())}&se={ttl}"
4⃣ Use SAS Token in Firmware
Set the Authorization header in MQTT/TLS to the token string.

View File

@ -1,3 +1,20 @@
/*
* station_example_main.c
*
* Description:
* This ESP32 app sets a static IP, updates a DuckDNS record,
* and sends messages to Azure IoT Hub using SAS authentication.
*
* Functional Overview:
* - Connects to Wi-Fi using static IP
* - Sends updates to DuckDNS periodically
* - Connects to Azure IoT Hub via MQTT over TLS
* - Uses SAS tokens for secure device authentication
*
* Author: Advait Achanta
*/
#include <stdio.h>
#include <string.h>
#include "esp_wifi.h"