Flipp3rix

Serial comuncation & Arduino

4 posts in this topic

Hi! I'm new with AutoIt and i have a little problem... I want make a little program that, when GPU's fans reach a RPM limit, my arduino changes rgb led color...Now...i get gpu fan rpm using open hardware monitor(i saw the code in this forum). My problem is...when RPM change my led don't change color...WHY? D:

This is the AutoIt Script:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include 'CommMG.au3'


Global $CMPort = 5
Global $CmBoBaud = 9600
Global $sportSetError = ''
Global $CmboDataBits = 8
Global $CmBoParity = "none"
Global $CmBoStop = 1
Global $setflow = 2
_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow)
If @error Then
MsgBox(16,"Error!","Can't connect to Arduino on port - "&$CMPort)
Exit
EndIf
_CommSetRTS(0)
_CommSetDTR(0)
If not ProcessExists("OpenHardwareMonitor.exe") Then
Msgbox (16, "Error", "Please start OpenHardwareMonitor.exe")
Exit
EndIf
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\OpenHardwareMonitor")

GUICreate ("CPU & GPU Info 2 Arduino",400,200)

GUICtrlCreateLabel ("------------------CPU------------------", 10, 5, 300,20)
GUICtrlCreateLabel ("Temperature", 10, 30, 100,20)
GUICtrlCreateLabel ("Load", 10, 50, 100,20)
$CPUTemp = GUICtrlCreateLabel("", 130, 30, 50, 20, $SS_RIGHT)
$CPULoad = GUICtrlCreateLabel("", 130, 50, 50, 20, $SS_RIGHT)
GUICtrlCreateLabel ("------------------GPU------------------", 10, 75, 300,20)
GUICtrlCreateLabel ("Temperature", 10, 100, 100,20)
GUICtrlCreateLabel ("Fan", 10, 120, 100,20)
GUICtrlCreateLabel ("Load", 10, 140, 100,20)
$GpuAtiTemp = GUICtrlCreateLabel("", 130, 100, 50, 20, $SS_RIGHT)
$GpuAtiFan = GUICtrlCreateLabel("", 79, 120, 130, 20, $SS_RIGHT)
$GpuAtiL= GUICtrlCreateLabel("", 130, 140, 50, 20, $SS_RIGHT)
GUISetState()

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch

$colItems = $objWMIService.ExecQuery("SELECT * FROM Sensor", "WQL",$wbemFlagReturnImmediately + $wbemFlagForwardOnly)
$Output=""
$Power=""
$Load=""
For $objItem in $colItems
if $objItem.SensorType = 'Temperature' and StringInStr($objItem.Parent, 'cpu') Then
If StringInStr($objItem.Name , "Core") Then
_GuiCtrlSetData($CPUTemp, $objItem.Value & " °C")
EndIf
EndIf

if $objItem.SensorType = 'Load' and StringInStr($objItem.Parent, 'cpu') Then
If StringInStr($objItem.Name , "Total") Then
_GuiCtrlSetData($CPULoad, Round($objItem.Value,1) & " %")
EndIf
EndIf

if $objItem.SensorType = 'Temperature' and StringInStr($objItem.Parent, 'gpu') Then
If StringInStr($objItem.Name , "Core") Then
_GuiCtrlSetData($GpuAtiTemp, Round($objItem.Value,1) & " °C")
EndIf
EndIf

if $objItem.SensorType = 'Fan' and StringInStr($objItem.Parent, 'gpu') Then
If StringInStr($objItem.Name , "GPU Fan") Then
_GuiCtrlSetData($GpuAtiFan, Round($objItem.Value,1) & "  RPM")
EndIf
EndIf

if $objItem.SensorType = 'Load' and StringInStr($objItem.Parent, 'gpu') Then
If StringInStr($objItem.Name , "GPU") Then
_GuiCtrlSetData($GpuAtiL, Round($objItem.Value,1) & "  %")
EndIf
EndIf

Select
Case $GpuAtiFan>2137
     LEDa()

  Case $GpuAtiFan<2137
     LEDs()
Endselect

Next
WEnd

Func LEDs()
_CommSendByte(1)
EndFunc

Func LEDa0()
_CommSendByte(2)
EndFunc

Func _GUICtrlSetData($iCtrlID, $sData)
If GUICtrlRead($iCtrlID, 1) <> $sData Then GUICtrlSetData($iCtrlID, $sData)
EndFunc ;==>_GUICtrlSetData

Arduino Code:

char val = '0 ';

int redPin = 5;
int greenPin =4;
int bluePin = 3;

void setup ()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT); 
  Serial.begin (9600);
}
 
void loop ()
{
  val = Serial.read ();
  if (val == 1) {
    setColor(150, 0, 255);
    Serial.println ("Acceso");
  }
  if (val == 2) {
    setColor(0, 150, 255);
    Serial.println ("Spento");
  }
}

void setColor(int red, int green, int blue)
{
  #ifdef COMMON_ANODE
    red = 255 - red;
    green = 255 - green;
    blue = 255 - blue;
  #endif
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

 

Share this post


Link to post
Share on other sites



$GpuAtiFan is the control id of the label. You need to read out the text value of the label using GuiCtrlRead().

And function LEDa0() should be LEDa().


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites
 
Hi, I'm trying to make a temperature monitor for my PC with an arduino and a 20x4 LCD screen, and your script sounds great to me as a basis for my project! But I have the following problem does not detect my arduino board in my case is in the COM9, I leave a video of what I'm doing, you may have some error, the truth just handle some arduino and this is way above my level . Thank you.

https://youtu.be/kSXSdyDMtic

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now