Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use warnings;
- use Time::localtime;
- #`cd ~/Meteo`;
- $h_dir="//home//xenomorph//Meteo//";
- $m_dir="MeasurArch";
- $tm=localtime;
- &check_modules;
- &get_device_IDs;
- &get_device_name;
- $time_stamp=sprintf("[%02d.%02d.%04d %02d.%02d.%02d]>",$tm->mday,$tm->mon+1,$tm->year+1900,$tm->hour,$tm->min,$tm->sec);
- $res_str=$time_stamp;
- foreach $device (@deviceIDs)
- {
- $reading=&read_device($device);
- if($reading != "9999")
- {
- $dev_name=$device;
- foreach $t_name(@deviceName)
- {
- if(index($t_name,$device )!=-1)
- {
- $dev_name=$t_name;
- $dev_name=~s/.*($device)//;
- }
- }
- $res_str = $res_str.$dev_name;
- $res_str = $res_str."=".$reading;
- }
- }
- print $res_str."\n";
- $dir_str=sprintf("%s%s//%04d",$h_dir,$m_dir,$tm->year+1900);
- mkdir $h_dir.$m_dir;
- mkdir $dir_str;
- $fn=sprintf("%s%s//%04d//%04d_%02d_%02d.dat",$h_dir,$m_dir,$tm->year+1900,$tm->year+1900,$tm->mon+1,$tm->mday);
- open (FILE,'>>',$fn);
- print FILE $res_str."\n";
- close (FILE);
- sub check_modules
- {
- $mods=`cat /proc/modules`;
- if($mods=~/w1_gpio/ && $mods=~/w1_therm/)
- {
- # print "w1 modules alrady loaded \n";
- }
- else
- {
- # print "loading w1 modules \n";
- `sudo modprobe w1-gpio`;
- `sudo modprobe w1-therm`;
- }
- }
- sub get_device_IDs
- {
- open(FILE, "/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves") or die ("Unable to open file");
- chomp(@deviceIDs=<FILE>);
- close(FILE);
- }
- sub get_device_name
- {
- open(FILE, $h_dir."sensor_name");
- chomp(@deviceName=<FILE>);
- close(FILE);
- }
- sub read_device
- {
- $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*\d+)/i;
- $ret=(($1/1000));
- }
- else
- {
- $ret="9999";
- }
- }
- else
- {
- $ret="9999";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement