OpenSprinkler Forums Hardware Questions controlling an unused zone Reply To: controlling an unused zone

#46417

jjrushford
Participant

I have it running and I left it in the sequential state as this works out pretty well. I ended up installing this in a outside electronics enclosure with a rain hood on the side of my house. The fan mount was for an 80mm fan so, I bought a 12vdc 80mm fan and a small power supply, 24vac input with 12vdc output, to power the fan when the 24vac is switched on by zone 8. The fan lowers the temp during the day by about 10c. I wrote the following perl script that is run from cron every 5 minutes. It checks the cpu temp on the pi and starts the fan using zone 8 to keep the pi cool.

chkRpiTemp:


#!/usr/bin/perl
#
# Checks the raspberry pi cpu temperature and turns on a fan 
# connected to one of the opensprinkler zones.
#
# Uses OpenSprinkler Firmware 2.1.7 API for station i/o.
#

use LWP::Simple;
use JSON qw(decode_json);
use POSIX qw(strftime);
use Data::Dumper;  # for debugging.
use strict;
use warnings;

my $base_url = "http://127.0.0.1/";  # base api url.
my $duration = 600;   # run the fan for this number of seconds.
my $fan_station = 7;  # fan is controlled by this station, stations are 0 to 7.
my $hi_temp = 45;     # start the fan when the cpu temp is this or higher.
my $pw = "f6d2aec0621aa5110240ca144520bdea";  # MD5 hash of opensprinkler password.
my $temp_file = '/sys/devices/virtual/thermal/thermal_zone0/temp';

# log messages.
sub logMessage {
  my $_msg = shift;
  my $_tm = strftime('%b %d %C%y %H:%M:%S', localtime());
  print "$_tm - $_msg\n";
}

#
# Reads and returns the raspberry pi cpu temperature
sub getTemp {
  my $_centigrade = 0;
  my $_fahrenheit = 0; 

  open(my $fh, '<:encoding(UTF-8)', $temp_file)
    or die "Could not open file '$temp_file' $!";
  
  while(my $_t = <$fh>) {
    chomp $_t;
    $_centigrade = $_t / 1000.00;
    $_fahrenheit = (($_centigrade * 1.8) + 32);
  }
  close($fh);

  return (sprintf("%.2f",$_centigrade), sprintf("%.2f",$_fahrenheit));
}

# read the state of the station that controls the fan.
sub getFan {
  my $_url = $base_url . "js?pw=$pw";
  my $_json = get($_url);
  my $_decoded_json = decode_json($_json);
  return $_decoded_json->{'sn'}[$fan_station];
}

# set the fan station on or off.
sub setFan {
  my $_state = @_;
  my $_url = $base_url;

  if ($_state == 0) {
    $_url = $_url . "cm?pw=$pw&sid=$fan_station&en=0";
  }
  elsif ($_state == 1) {
    $_url = $_url . "cm?pw=$pw&sid=$fan_station&en=1&t=$duration";
  }
  else {
    logMessage("setFan(): invalid state $_state");
    return;
  }
  my $_json = get($_url);
  my $_decoded_json = decode_json($_json);

  return $_decoded_json->{'result'};
}

my ($temp_c,$temp_f)  = getTemp();

# if the cpu temp has reached the hi temperature setting,
# turn on the fan.
my $fan = getFan();
if ($temp_c >= $hi_temp) {

  if ($fan == 0) {
    my $result = setFan(1);
    if ($result == 1) {
      logMessage("cpu temperature is ${temp_c}C (${temp_f}F), fan started.");
    } else {
      logMessage("cpu temperature is ${temp_c}C (${temp_f}F), error starting fan.");
    }
  } else {
    logMessage("cpu temperature is ${temp_c}C (${temp_f}F), fan is already running.");
  }
} else {
  if ($fan == 0) {
    logMessage("cpu temperature is ${temp_c}C (${temp_f}F), the fan is off.");
  } elsif ($fan == 1) {
    logMessage("cpu temperature is ${temp_c}C (${temp_f}F), the fan is running.");
  }
}