Search code examples
azureazure-iot-central

Manually Triggering Alerts for Simulated Devices


is there anyway we can manually trigger an alert for a simulated device instead of waiting for its value to trigger it? e.g. manually setting the value of the device.


Solution

  • You can update a setting to achieve this purpose. In Azure IoT Central portal you can edit a setting and update it like this:

    enter image description here

    After you click update button the device will receive a DesiredPropertyUpdate callback. In that callback you can configure the device takes an action. To complete this you need set the callback handler:

    Client.SetDesiredPropertyUpdateCallbackAsync(HandleSettingChanged, null).Wait();
    

    And the handler like this, for example:

        private static async Task HandleSettingChanged(TwinCollection desiredProperties, object userContext)
        {
            try
            {
                Console.WriteLine("Received settings change...");
                Console.WriteLine(JsonConvert.SerializeObject(desiredProperties));
    
                string setting = "fanSpeed";
                if (desiredProperties.Contains(setting))
                {
                    // Act on setting change, then
                    AcknowledgeSettingChange(desiredProperties, setting);
                }
                await Client.UpdateReportedPropertiesAsync(reportedProperties);
            }
    
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine("Error in sample: {0}", ex.Message);
            }
        }
    

    For detailed information, you can reference this tutorial: "Connect a Raspberry Pi to your Azure IoT Central application".