Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Last active March 19, 2018 22:39
Show Gist options
  • Save darrenjrobinson/32464c44ee1f1ef55097bdde1351f5aa to your computer and use it in GitHub Desktop.
Save darrenjrobinson/32464c44ee1f1ef55097bdde1351f5aa to your computer and use it in GitHub Desktop.
Send MQTT Message from Azure IoT to Mongoose IoT Device
# Azure IoT
Import-Module AzureIoT
Import-Module AzureRM
Login-AzureRmAccount
# IoT Hub RG, Location, Keyname and the target Device
$RGName = "MyMongooseIoTHub"
$IoTHubName = "MyMongooseIoTHub"
$deviceID = 'esp8266_12345'
$IoTKeyName = "iothubowner"
# Get IoT Hub and Key
$IoTHub = Get-AzureRmIotHub -Name $IoTHubName -ResourceGroupName $RGName
$IoTHubKey = Get-AzureRmIotHubKey -ResourceGroupName $iothub.Resourcegroup -Name $IoTHubName -KeyName $IoTKeyName
# IoT ConnectionString
$IoTConnectionString = "HostName=$($IoTHubName).azure-devices.net;SharedAccessKeyName=$($IoTKeyName);SharedAccessKey=$($IoTHubKey.PrimaryKey)"
# Build Hub Device URI
$IOTHubDeviceURI= "$($IoTHubName).azure-devices.net/devices/$($deviceID)"
$deviceParams = @{
iotConnString = $IoTConnectionString
deviceId = $deviceID
}
# Device Keys
$deviceKeys = Get-IoTDeviceKey @deviceParams
# Get Device
$device = Get-IoTDeviceClient -iotHubUri $IOTHubDeviceURI -deviceId $deviceID -deviceKey $deviceKeys.DevicePrimaryKey
# Azure IoT Cloud Client
$CloudClientParams = @{
iotConnString = $IoTConnectionString
}
$cloudClient = Get-IoTCloudClient @CloudClientParams
# Send message from Cloud
$cloudMessageParams = @{
deviceId = $deviceID
messageString = "Hello from the Cloud via Powershell"
cloudClient = $cloudClient
}
Send-IoTCloudMessage @cloudMessageParams
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment