<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Valeriia Savenko</title>
    <description>The latest articles on DEV Community by Valeriia Savenko (@valesavenko).</description>
    <link>https://dev.to/valesavenko</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F265798%2F384e9f98-b06e-46e2-a8ae-1a87d51ae9e1.jpeg</url>
      <title>DEV Community: Valeriia Savenko</title>
      <link>https://dev.to/valesavenko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/valesavenko"/>
    <language>en</language>
    <item>
      <title>IoT Edge module twin reported properties C# 
 </title>
      <dc:creator>Valeriia Savenko</dc:creator>
      <pubDate>Tue, 28 Jan 2020 02:01:49 +0000</pubDate>
      <link>https://dev.to/valesavenko/iot-edge-module-twin-reported-properties-c-4hkh</link>
      <guid>https://dev.to/valesavenko/iot-edge-module-twin-reported-properties-c-4hkh</guid>
      <description>&lt;p&gt;This is a small guide that builds on top of existing IoT edge C# sample. You might have across &lt;a href="https://docs.microsoft.com/en-us/azure/iot-edge/tutorial-csharp-module"&gt;IoT Edge C# sample&lt;/a&gt;.  The IoT Edge module that you create in that tutorial filters the temperature data that's generated by your device. It only sends messages upstream if the temperature is above a specified threshold. This type of analysis at the edge is useful for reducing the amount of data that's communicated to and stored in the cloud. 😃&lt;/p&gt;

&lt;p&gt;You can control/change the temperature from the cloud using module twins. Module twin's desired and reported properties are used to synchronize module configuration or conditions. Check &lt;a href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-module-twins"&gt;this&lt;/a&gt; out to understand how to use module twins for IoT Hub.&lt;/p&gt;

&lt;p&gt;The function &lt;strong&gt;OnDesiredPropertiesUpdate()&lt;/strong&gt; from the sample is invoked when there is a change to the desired properties, however, it does not update the reported properties.&lt;/p&gt;

&lt;p&gt;To update the reported properties we will make a few changes to the sample.&lt;/p&gt;

&lt;p&gt;Declare &lt;strong&gt;ioTHubModuleClient&lt;/strong&gt; as a global variable to the Program class. &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   // Declare ModuleClient 
   private static ModuleClient ioTHubModuleClient = null;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Find the &lt;strong&gt;Init&lt;/strong&gt; function. This function creates and configures a &lt;strong&gt;ModuleClient&lt;/strong&gt; object, which allows the module to connect to the local Azure IoT Edge runtime to send and receive messages. Make the following changes: &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   // Open a connection to the Edge runtime
        ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
        await ioTHubModuleClient.OpenAsync();
        Console.WriteLine("IoT Hub module client initialized.");
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Navigate to &lt;strong&gt;onDesiredPropertiesUpdate&lt;/strong&gt; method to the Program class and make the following changes to try-catch: &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        try
        {
            Console.WriteLine("Desired property change:");
            Console.WriteLine(JsonConvert.SerializeObject(desiredProperties));

            var reportedProperties = new TwinCollection();

            if (desiredProperties["TemperatureThreshold"]!=null) {

                temperatureThreshold = desiredProperties["TemperatureThreshold"];
                reportedProperties["TemperatureThreshold"] = temperatureThreshold;
            }

            if (reportedProperties.Count &amp;gt; 0)
            {
                ioTHubModuleClient.UpdateReportedPropertiesAsync(reportedProperties).ConfigureAwait(false);
            }

        }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here we use &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.devices.client.moduleclient.updatereportedpropertiesasync?view=azure-dotnet"&gt;ModuleClient.UpdateReportedPropertiesAsync Method&lt;/a&gt; to update the repoted properties. &lt;/p&gt;

</description>
      <category>iotedge</category>
      <category>azure</category>
      <category>iot</category>
    </item>
  </channel>
</rss>
