IoT -Temperature Monitor in Raspberry Pi using .NET Core

IoT Temperature Monitor in Raspberry Pi using NET Core

In our previous articles Create First .NET Core application in Raspberry Pi we learned the basics on how to create a ‘Hello World’ application.

We learned how to  Set up Raspberry Pi – Step by step and also Configure .NET Core IoT App on Raspberry Pi.

Today’s article we will learn to create a simple application for CPU Temperature monitoring in Raspberry Pi using .NET Core application.

Prerequisites:

Monitor CPU Temperature application

Remember since we have .NET Core SDK installed, we can now create .NET Core app right within Raspberry Pi.

Let quickly check the .NET Core version on Pi,

dotnet --info
IoT NET Core

Due to space limitation on Pi, You can create, build, and compile the project on your Laptop\Desktop. Then copy it to Pi and execute it on Pi.

Let’s create a console application using the below command in a Raspberry Pi terminal window.

dontet new console
IoT Temperature Monitor NET Core

Above command shall add .csproj file and Program.cs file.

Add below code to the Main() method in Program.cs as below,

using System;
using Iot.Device.CpuTemperature;
using System.Threading;

namespace IOT
{
    class Program
    {
        static CpuTemperature temperature = new CpuTemperature();
        static void Main(string[] args)
        {
            while (true)
            {
                if (temperature.IsAvailable)
                {
                    Console.WriteLine($"The CPU temperature in Celsius is 
                           {temperature.Temperature.Celsius}" +"C");
                    Console.WriteLine($"The CPU temperature in Fahrenheit is 
                           {temperature.Temperature.Fahrenheit}" + "F");
                }

                Thread.Sleep(2000); // sleep for 2000 milliseconds, 2 seconds
            }
        }
    }
}

Please make sure to add below namespace to your file,

using Iot.Device.CpuTemperature;

Project file or .csproj file will look like as below,

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Iot.Device.Bindings" Version="0.1.0-prerelease*" />
  </ItemGroup>

</Project>

Sample Nuget.config will look like as below,

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="myget.org" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>

Let’s run our .NET Core app using below command,

dotnet run [app-path]

The app will show the current CPU temperature in Celsius and Fahrenheit successfully.

IOT Device CPU Temperature Monitor

Summary

Today in this article, we dive into further ( beyond our ‘Hello World’ application) and learned to create an application for Temperature monitoring in Raspberry Pi using .NET Core application.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



2 thoughts on “IOT Device CPU Temperature Monitor

Leave a Reply

Your email address will not be published. Required fields are marked *