Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.