Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenjrobinson/43eae378f20e45e701a7d3079cca12ab to your computer and use it in GitHub Desktop.
Save darrenjrobinson/43eae378f20e45e701a7d3079cca12ab to your computer and use it in GitHub Desktop.
Send IoT Events from Device to Cloud over HTTPS and REST. Associated Blogpost https://blog.darrenjrobinson.com/sending-events-from-iot-devices-to-azure-iot-hub-using-https-and-rest/
# Send Message Device to IoTHub over HTTPS via Rest
# IoT DeviceID
$deviceID = "MyIoTDevice"
# Iot Hub Name
$IoTHubName = "MyIoTHub"
$IOTHubDeviceURI= "$($IoTHubName).azure-devices.net/devices/$($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 sr=MyIoTHub.azure-devices.net%2Fdevices%2FMyIoTDevice&sig=gqitFLvPA9AghisfjsTYUSDFGF8cx8cBOXCXOfwwd3qT8E%3D&se=1560379444"
# 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
@salinijoseph
Copy link

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

@darrenjrobinson
Copy link
Author

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