Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Send IoT Events from Device to Cloud over HTTPS and REST. Associated Blogpost
# Send Message Device to IoTHub over HTTPS via Rest
# IoT DeviceID
$deviceID = "MyIoTDevice"
# Iot Hub Name
$IoTHubName = "MyIoTHub"
$IOTHubDeviceURI= "$($IoTHubName)$($deviceID)"
# RestAPI Version
$iotHubAPIVer = "2018-04-01"
$iotHubRestURI = "https://$($IOTHubDeviceURI)/messages/events?api-version=$($iotHubAPIVer)"
# SAS Token Generated via Azure CLI or Device Explorer
$SASToken = "SharedAccessSignature"
# Headers
$Headers = @{"Authorization" = $SASToken; "Content-Type" = "application/json"}
# Message Payload
$datetime = get-date
$body = @{
datetime = $datetime
deviceClient = $deviceID
Message = "Message Device to Cloud"
$body = $body | ConvertTo-Json
# Send Message
Invoke-RestMethod -Uri $iotHubRestURI -Headers $Headers -Method Post -Body $body

This comment has been minimized.

Copy link

salinijoseph commented Mar 26, 2020

Can I use this for any device to send message to IoT Hub


This comment has been minimized.

Copy link
Owner Author

darrenjrobinson commented Mar 26, 2020

In this example any device with PowerShell support. I've linked the supporting blogpost to the GIST that also has a Python example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.