Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
sudo modprobe w1-gpio
sudo modprobe w1_therm
w1-gpio
w1_therm
#!/usr/bin/perl
use warnings;
#use diagnostics;
&check_modules;
&get_device_IDs;
foreach $device(@deviceIDs) {
$reading = & read_device($device);
if ($reading != "9999") {
push(@temp_readings, $reading);
push(@temp_readings, $reading);
}
}
#update the database
`/usr/bin/rrdtool update /home/RRD/multirPItemp.rrd N:$temp_readings[0]:$temp_readings[1]`;
#`/usr/bin/rrdtool update /home/RRD/multirPItemp.rrd N:$temp_readings[0]`;
print "Temp 1 = $temp_readings[0]\n";
print "Temp 2 = $temp_readings[1]\n";
########################################################################
#Дополнения для narodmon.ru.По материалам habrahabr.ru/post/166373
# open(FILE, ">/home/RRD/pi/temperature/temp_out");#
#print FILE "$temp_readings[0]";#
#close(FILE);#
#open(FILE, ">/home/RRD/temp_in");#
#print FILE "$temp_readings[1]";#
#close(FILE);
#######################################################################
sub check_modules {
$mods = `cat /proc/modules`;
if ($mods =~ /w1_gpio/ && $mods =~ /w1_therm/) {
print "w1 modules already loaded \n";
} else {
print "loading w1 modules \n";
`sudo modprobe w1-gpio`;
`sudo modprobe w1-therm`;
}
}
sub get_device_IDs {#
#The Hex IDs off all detected 1 - wire devices on the bus are stored in the file
# "w1_master_slaves"
#open file
open(FILE, "/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves") or die("Unable to open file");
#read file into an array
@deviceIDs = <FILE>;
#close file
close(FILE);
}
sub read_device {
#takes one parameter — a device ID
#returns the temperature
#if we have something like valid conditions
#else we return "9999" for undefined
$readcommand = "cat /sys/bus/w1/devices/".$_[0]."/w1_slave 2>&1";
$readcommand =~ s/\R//g;
$sensor_temp = `$readcommand`;
if ($sensor_temp!~/No such file or directory/) {
if ($sensor_temp!~/NO/) {
$sensor_temp =~ /t=(\d+)/i;
$sensor_temp =~ /t=(\D*\d+)/i;
$temperature = (($1 / 1000));
} else {
$ret = "9999";
}
} else {
$ret = "9999";
}
}
История взаимодействия «чайника» и DS18B20 посредством Raspberry Pi с отправкой данных на narodmon.ru