Arduino Project Hub
Project tutorial

Arduino PC Monitor © GPL3+

I'll show you, how to monitor some hardware variables.

  • 7,969 views
  • 5 comments
  • 12 respects

Components and supplies

A000057 featured
Arduino Leonardo
or other Arduino device or even Atmega with UART converter
×1
LCD 16x2
×1
11026 02
Jumper wires (generic)
×1

About this project

Introduction

The idea for this project was born actually from the need of fun and knowledge. This project is easy to make and has many applications. We can use it, for example, to control, store and display some hardware variables.

Let's get started

First of all, we need to connect our LCD (I was using I2C converter) :

When we did this, we can move to the next part...

Let's write some code...

Set 16x2 LCD with address 0x3F - address of I2C bus device can be found from scanning via simple sketch available on Arduino Playground.

#include <Wire.h>  
#include <LiquidCrystal_I2C.h> 
LiquidCrystal_I2C lcd(0x3F,16,2); 

Now we have to declare a String variable to store our Serial input and setup rest of stuff.

String inData; 
void setup() 
{ 
   lcd.init();  
   Serial.begin(9600); 
   lcd.backlight(); 
} 

If we would like to send data directly by a string variable it could be problematic, because of about one-second latency based on string size. To avoid this use char variable and add it to our inData .

char recieved = Serial.read(); 
inData += recieved;  

Ok, everything's fine but when receiving data stop coming? There is solution:

if (recieved == '*') 
       { ... }

When last of char will be * Arduino will know when to proceed next part, Now we have to handle our message. We need to get rid of this * char so:

inData.remove(inData.length() - 1, 1); 

And finally:

lcd.setCursor(0,0); 
lcd.print(inData);  
inData = ""; // Clear buffer 

But... We have second line unused... Here is the solution

if (recieved == '#') 
   {   
           inData.remove(inData.length() - 1, 1); 
           lcd.setCursor(0,1); 
           lcd.print(inData); 
           inData = "";  
       } 
   } 

When last of char will be # Arduino will set the second line of our LCD and print other data.

Configuration

Why C#, not C or C++ ? C# is easy and fast (fast... I mean in writing)

First of all, we need to download additional library for easy access to our hardware variables.

Open Hardware Monitor

After unpacking, we have our library: OpenHardwareMonitorLib.dll

Create new project -> Windows Forms Application -> Right click on References in Solution Explorer -> Browse -> OpenHardwareMonitorLib.dll and don't forget to check it.

Appearance

Prepare our form:

Now code...

Some directives...

using System.IO.Ports; 
using OpenHardwareMonitor.Hardware; 

And declarations...

SerialPort port = new SerialPort();
Computer c = new Computer() 
{ 
     GPUEnabled = true, 
     CPUEnabled = true  
}; 
float value1, value2;  

c is Open Hardware Monitor object. We have to enable CPU and GPU. In Form Load event put this:

 c.Open(); 

In form constructor:

public Form1()  

add:

Init(); 

and then, anywhere:

private void Init() 
       { 
           try 
           { 
               notifyIcon1.Visible = false; 
               port.Parity = Parity.None; 
               port.StopBits = StopBits.One; 
               port.DataBits = 8; 
               port.Handshake = Handshake.None; 
               port.RtsEnable = true;              
               string[] ports = SerialPort.GetPortNames(); 
               foreach (string port in ports) 
               { 
                   comboBox1.Items.Add(port); 
               } 
               port.BaudRate = 9600; 
           } 
           catch (Exception ex) 
           { 
               MessageBox.Show(ex.Message); 
           } 
       }  

Simple... Initialize declared variables, scan for open ports and add it to the comboBox1

Now major function to store and send variables:

private void Status() 
       { 
           foreach (var hardwadre in c.Hardware) 
           { 
               if (hardwadre.HardwareType == HardwareType.GpuNvidia) 
               { 
                   hardwadre.Update(); 
                   foreach (var sensor in hardwadre.Sensors) 
                       if (sensor.SensorType == SensorType.Temperature) 
                       { 
                           value1 = sensor.Value.GetValueOrDefault(); 
                       } 
               } 
               if (hardwadre.HardwareType == HardwareType.CPU) 
               { 
                   hardwadre.Update(); 
                   foreach (var sensor in hardwadre.Sensors) 
                       if (sensor.SensorType == SensorType.Temperature) 
                       { 
                           value2 = sensor.Value.GetValueOrDefault(); 
                       } 
               } 
           } 
           try 
           { 
               port.Write(value1 + "*" + value2 + "#"); 
           }catch(Exception ex) 
           { 
               timer1.Stop(); 
               MessageBox.Show(ex.Message); 
               toolStripStatusLabel1.Text = "Arduino's not responding..."; 
           } 

Don't forget to add Status() function to timer tick event.

Now connect button:

try 
          { 
               if (!port.IsOpen) 
               { 
                   port.PortName = comboBox1.Text; 
                   port.Open();  
                   timer1.Interval = Convert.ToInt32(comboBox2.Text);
                   timer1.Enabled = true;  
                   toolStripStatusLabel1.Text = "Sending data..."; 
                   label2.Text = "Connected"; 
               }      
           } 
           catch (Exception ex) 
           { 
               MessageBox.Show(ex.Message); 
           } 

We're checking if a port is already open, if not setting timer interval from comboBox2 and run everything up!

Disconnect button:

try 
           { 
               port.Write("DIS*"); 
               port.Close(); 
           } 
           catch (Exception ex) 
           { 
               MessageBox.Show(ex.Message); 
           }          
           label2.Text = "Disconnected"; 
           timer1.Enabled = false; 
           toolStripStatusLabel1.Text = "Connect to Arduino..."; 
           data = ""; 

Write last data to Arduino to said that we're done.

You can add some functions like for e.g. hide to tray etc. (Full code below)

Program has to be run with administrator privileges!

Schematics

Schematics
Lcduino gh44yjacqv

Code

Arduino CodeArduino
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2);
String inData;

void setup() {
    Serial.begin(9600);
    lcd.init();
    lcd.backlight();
}

void loop() {

    while (Serial.available() > 0)
    {
        char recieved = Serial.read();
        inData += recieved; 
        
        if (recieved == '*')
        {
            inData.remove(inData.length() - 1, 1);
            lcd.setCursor(0,0);
            lcd.print("GPU Temp.: " + inData + char(223)+"C ");
            inData = ""; 
            
            if(inData == "DIS")
            {   
              lcd.clear();
              lcd.setCursor(0,0);
              lcd.print("Disconnected!");
            }
        } 
        
        if (recieved == '#')
        {
            inData.remove(inData.length() - 1, 1);
            lcd.setCursor(0,1);
            lcd.print("CPU Temp.: " + inData + char(223)+"C ");
            inData = ""; 
        }
    }
}
C# ApplicationC#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using OpenHardwareMonitor.Hardware;

namespace Arduino_Controll
{
    public partial class Form1 : Form
    {
        static string data;
        Computer c = new Computer()
        {
            GPUEnabled = true,
            CPUEnabled = true 
        };

        float value1, value2;
        
        private SerialPort port = new SerialPort();
        public Form1()
        {
            InitializeComponent();
            Init();
        }


        private void Init()
        {
            try
            {
                notifyIcon1.Visible = false;
                port.Parity = Parity.None;
                port.StopBits = StopBits.One;
                port.DataBits = 8;
                port.Handshake = Handshake.None;
                port.RtsEnable = true;
                string[] ports = SerialPort.GetPortNames();
                foreach (string port in ports)
                {
                    comboBox1.Items.Add(port);
                }
                port.BaudRate = 9600;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

   
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                port.Write("DIS*");
                port.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }         
            label2.Text = "Disconnected";
            timer1.Enabled = false;
            toolStripStatusLabel1.Text = "Connect to Arduino...";
            data = "";
        }
        

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                if (!port.IsOpen)
                {
                    port.PortName = comboBox1.Text;
                    port.Open();
                    timer1.Interval = Convert.ToInt32(comboBox2.Text);
                    timer1.Enabled = true;
                    toolStripStatusLabel1.Text = "Sending data...";
                    label2.Text = "Connected";
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            Status();         
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            c.Open();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                notifyIcon1.Visible = true;
                try
                {
                    notifyIcon1.ShowBalloonTip(500, "Arduino", toolStripStatusLabel1.Text, ToolTipIcon.Info);

                }catch(Exception ex)
                {

                }
                this.Hide();
            }
               
                      
        }


        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            
            this.Show();
            this.WindowState = FormWindowState.Normal;
            notifyIcon1.Visible = false;
        }


        private void Status()
        {
            foreach (var hardwadre in c.Hardware)
            {

                if (hardwadre.HardwareType == HardwareType.GpuNvidia)
                {
                    hardwadre.Update();
                    foreach (var sensor in hardwadre.Sensors)
                        if (sensor.SensorType == SensorType.Temperature)
                        {
                           
                            value1 = sensor.Value.GetValueOrDefault();
                        }

                }

                if (hardwadre.HardwareType == HardwareType.CPU)
                {
                    hardwadre.Update();
                    foreach (var sensor in hardwadre.Sensors)
                        if (sensor.SensorType == SensorType.Temperature)
                        {
                            value2 = sensor.Value.GetValueOrDefault();

                        }

                }

            }
            try
            {
                port.Write(value1 + "*" + value2 + "#");
            }catch(Exception ex)
            {
                timer1.Stop();
                MessageBox.Show(ex.Message);
                toolStripStatusLabel1.Text = "Arduino's not responding...";
            }
        }
 
    }
}

    

Comments

Similar projects you might like

3D-Printed Prank Vibrating Cup

Project tutorial by Alex Wulff

  • 1,474 views
  • 2 comments
  • 14 respects

The Magic Display With Arduino - The Mind Reader

Project tutorial by LAGSILVA

  • 1,643 views
  • 2 comments
  • 6 respects

Very First Hands On Arduino - Voice Activated LED

Project tutorial by Liren Yeo

  • 959 views
  • 1 comment
  • 9 respects

Get Connected! WiFi Registration Using AP Mode!

Project tutorial by Brian Carbonette

  • 1,656 views
  • 0 comments
  • 16 respects

The Soldering Iron Controller for Hakko T12 Tips

Project tutorial by Alexander

  • 8,993 views
  • 4 comments
  • 27 respects

Voltmeter Using Arduino

by Team Next Tech Lab

  • 3,334 views
  • 8 comments
  • 45 respects
Add projectSign up / Login