Skip to content

Instantly share code, notes, and snippets.

@hsiboy
Last active September 5, 2015 10:55
Show Gist options
  • Save hsiboy/9352879 to your computer and use it in GitHub Desktop.
Save hsiboy/9352879 to your computer and use it in GitHub Desktop.
Perl Thermostat - DS18B20 Raspberry Pi
#!/usr/bin/perl
use strict;
use warnings;
use Device::BCM2835;
# must be run as root, in order to access the BCM 2835 GPIO address space!
# call set_debug(1) to do a non-destructive test on non-Raspberry Pi hardware
#Device::BCM2835::set_debug(1);
Device::BCM2835::init() || die "Could not init library";
# Raspberry Pi pin 7 is used for the DS18B20 "1-wire" temperature sensors!!
# Set Raspberry Pi pin 11 to be an OUTPUT
Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_GPIO_P1_11, &Device::BCM2835::BCM2835_GPIO_FSEL_OUTP);
# Set Raspberry Pi pin 16 to be an OUTPUT
Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_GPIO_P1_16, &Device::BCM2835::BCM2835_GPIO_FSEL_OUTP);
# Set Raspberry Pi pin 15 to be an INPUT / with PULL Up Resistor
Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_GPIO_P1_15, &Device::BCM2835::BCM2835_GPIO_FSEL_INPT);
while (1)
{
my $line;
my $temperature;
# Temperature Setpoint
my $tSetpoint = 21;
opendir(DH, "/sys/bus/w1/devices/");
my @files = readdir(DH);
closedir(DH);
foreach my $file (@files)
{
# skip . and ..
next if($file =~ /^\.$/);
next if($file =~ /^\.\.$/);
# $file is the file used on this iteration of the loop
}
open (FH, "<$file/w1_slave") || die "Problem reading Temp from DS18B20 !\n";
while ($line = <FH>) {
chop $line;
if ($line !~ /t\=/) {next;}
$line =~ s/(.+)=(\d+)/$2/;
$temperature = $line;
}
close DS;
if ($temp >= $tSetpoint) {
# Turn Boiler Off
Device::BCM2835::gpio_write(&Device::BCM2835::RPI_GPIO_P1_11, LOW);
}
if ($temp <= $tSetpoint) {
# Turn Boiler On
Device::BCM2835::gpio_write(&Device::BCM2835::RPI_GPIO_P1_11, HIGH);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment