Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use warnings;
  3. use Time::localtime;
  4. #`cd ~/Meteo`;
  5. $h_dir="//home//xenomorph//Meteo//";
  6. $m_dir="MeasurArch";
  7. $tm=localtime;
  8.  
  9. &check_modules;
  10. &get_device_IDs;
  11. &get_device_name;
  12.  
  13. $time_stamp=sprintf("[%02d.%02d.%04d %02d.%02d.%02d]>",$tm->mday,$tm->mon+1,$tm->year+1900,$tm->hour,$tm->min,$tm->sec);
  14.  
  15. $res_str=$time_stamp;
  16.  
  17. foreach $device (@deviceIDs)
  18. {
  19. $reading=&read_device($device);
  20. if($reading != "9999")
  21. {
  22. $dev_name=$device;
  23. foreach $t_name(@deviceName)
  24. {
  25. if(index($t_name,$device )!=-1)
  26. {
  27. $dev_name=$t_name;
  28. $dev_name=~s/.*($device)//;
  29. }
  30. }
  31. $res_str = $res_str.$dev_name;
  32. $res_str = $res_str."=".$reading;
  33. }
  34. }
  35.  
  36. print $res_str."\n";
  37. $dir_str=sprintf("%s%s//%04d",$h_dir,$m_dir,$tm->year+1900);
  38. mkdir $h_dir.$m_dir;
  39. mkdir $dir_str;
  40. $fn=sprintf("%s%s//%04d//%04d_%02d_%02d.dat",$h_dir,$m_dir,$tm->year+1900,$tm->year+1900,$tm->mon+1,$tm->mday);
  41. open (FILE,'>>',$fn);
  42. print FILE $res_str."\n";
  43. close (FILE);
  44.  
  45. sub check_modules
  46. {
  47. $mods=`cat /proc/modules`;
  48. if($mods=~/w1_gpio/ && $mods=~/w1_therm/)
  49. {
  50. # print "w1 modules alrady loaded \n";
  51. }
  52. else
  53. {
  54. # print "loading w1 modules \n";
  55. `sudo modprobe w1-gpio`;
  56. `sudo modprobe w1-therm`;
  57. }
  58. }
  59.  
  60. sub get_device_IDs
  61. {
  62. open(FILE, "/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves") or die ("Unable to open file");
  63. chomp(@deviceIDs=<FILE>);
  64.  
  65. close(FILE);
  66. }
  67.  
  68. sub get_device_name
  69. {
  70. open(FILE, $h_dir."sensor_name");
  71. chomp(@deviceName=<FILE>);
  72. close(FILE);
  73. }
  74.  
  75. sub read_device
  76. {
  77. $readcommand="cat /sys/bus/w1/devices/".$_[0]."/w1_slave 2>&1";
  78. $readcommand=~ s/\R//g;
  79. $sensor_temp=`$readcommand`;
  80. if($sensor_temp !~ /No such file or directory/)
  81. {
  82. if($sensor_temp !~ /NO/)
  83. {
  84. $sensor_temp =~ /t=(\D*\d+)/i;
  85. $ret=(($1/1000));
  86. }
  87. else
  88. {
  89. $ret="9999";
  90. }
  91. }
  92. else
  93. {
  94. $ret="9999";
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement