pine64 guinea pig habitat comfort control

pine64 and perl guinea pig habitat environment automation
pine64 and perl guinea pig habitat environment automation

We’ve moved our guinea pig Monster out to the garage.  He’s in no danger of overheating (trust me, he is spoiled rotten!) but it does get a little warm out there.  I used my pine64 to turn on a 20W fan when it rises above a chosen temperature.  Temperature samples are taken with an analog TMP36 sensor which is read by an MCP3004 analog to digital converter.  It also gets pitch dark with the lights out at night, so there is also a photo resistor that is read by the MCP3004 ADC.  When it drops below a certain threshold, it turns on an LED night light for Monster.  I think he likes it.  He has been laying in front of the fan.

Once the ADC reading is taken of the TMP36 sensor, this is how it is converted into degrees Fahrenheit.

my $temperature = ((($voltage_t * 1000) - 500) / 10) * 1.8 + 32;

I am running armbian on my pine64 and have written my own gpio control script in perl.  Here is the perl script that controls Monster’s fan and night light.

#!/usr/bin/perl 
use strict;
require "./MCP3004.pl";

gpio_enable(17, 'out');
gpio_enable(13, 'out');

#get thermostat arg
my $thermostat = $ARGV[0];
if($thermostat eq ''){
	$thermostat = 85;
}#end thermostat


#channels: first bit is set to single, not differential
#5 bits because the first is the start bit
my @ch0 = (1,1,0,0,0);
my @ch1 = (1,1,0,0,1);
my @ch2 = (1,1,0,1,0);
my @ch3 = (1,1,0,1,1);
#differential
my @ch01 = (1,0,0,0,0);

init3004(18,19,24,25);

#loop global variables
my $tmp10 = 0;
my $tavg = 0;
my $bin10 = 0;
my $binavg = 0;
my $fan_f = 0;
my $lt_f = 0;

for(;;){
	$tmp10 = 0;
	$tavg = 0;
	$bin10 = 0;
	$binavg = 0;

	for(my $n=0;$n<10;$n++){ #photoresisitor reading------------------------------------------- my ($reading, $binval, $voltage ) = read3004(\@ch0, 50, 5.01); print "binval: $binval\t$voltage vdc"; $bin10 += $binval; #print RD "$s\n$voltage\n"; #photocell end---------------------------------------------------- #tmp36 temperature reading---------------------------------------- print "\n---------------------------------------\n"; my ($reading_t, $binval_t, $voltage_t ) = read3004(\@ch1, 50, 5.01); #print "binval_tmp: $binval_t\tvoltage_tmp: $voltage_t vdc\n"; my $temperature = ((($voltage_t * 1000) - 500) / 10) * 1.8 + 32; $temperature = sprintf("%5.2f", $temperature); print "temperature: $temperature deg F\n"; #accumulate temp $tmp10 +=$temperature; print "---------------------------------------\n"; #tmp36 end-------------------------------------------------------- #0.5 pause between samples usleep(500000); }#end for 10 #calculate temperature averages $tavg = $tmp10 / 10; if($tavg > $thermostat){
		#turns fan on
		gpio_write(17, 1);
		$fan_f = 1;
	}#end if
	else{
		gpio_write(17, 0);
		$fan_f = 0;
	}#end else

	#calculate light intensity averages
	$binavg = $bin10 / 10;
	if($binavg > 610){
		#turns light on
		gpio_write(13, 0);
		$lt_f = 1;
	}#end if
	else{
		gpio_write(13, 1);
		$lt_f = 0;
	}#end else

	print "\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n";
	print "avg temp: $tavg stat:$fan_f\tavg light: $binavg stat:$lt_f";
	print "\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n";

	#wait 1 minute
	sleep(60);
}#end outer for

That’s a lot of processing power just to control a light and a fan.  I plan to migrate the function to an arduino nano soon.

pine64 and perl guinea pig habitat environment controls
pine64 and perl guinea pig habitat environment controls
pine64 guinea pig night light
pine64 guinea pig night light
per & pine64 guinea pig automation
perl & pine64 guinea pig automation

Leave a Reply

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