AppleScript C Perl Shell Xcode

Practical DigiTemp on Mac OS X - hardware and software.

Post Reply
coding / perl     Views: 3184Prev .. Next
Practical DigiTemp on Mac OS X - hardware and software.Posted: Monday, July 15, 2013 [13:29:35] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 69
I wanted to buy a weather station for home use. Turned-out it's quite expensive. I basically was interested in simple data reading and collection for temperature and maybe atmospheric pressure and humidity.
Google brought unexpectedly large number of results on DigiTemp for Linux. Did read some articles thinking USB adapter should work on Mac. Purchased DS9490R from Digi-Key and a week later since I've got it - it did not work. I was unable to find a proper driver for it.
Then found the Indigo DigiTemp forum and bought DS9097U-S09 Serial adapter for One-Wire network. DS9097U-S09 did not work at first as I used Belkin F5U409 USB to Serial adapter. Belkin adapter did not work for this application.

Went on eBay and bought Smart USB to RS-232 with PL2303 Chipset with driver readily available for Mac OS X Lions and Leopards. Paid around $6 delivered.

Digi Key website for DS9097U-S09 http://www.digikey.com/product-detail/en/DS9097U-S09%23/DS9097U-S09%23-ND/1768830

I also bought three DS18S20+PAR-ND temperature sensors from Digi-Key. For some reason 2 out of 3 either went bad or were bad even though I was very careful soldering wires to them.

That is DS18S20+PAR-ND outdoor temperature sensor sealed with silicone and epoxy.

Digi Key website for DS18S20+PAR-ND http://www.digikey.com/product-detail/en/DS18S20%2BPAR/DS18S20%2BPAR-ND/1197288
That is pretty much hardware I am currently using.

It is all over the Internet - Mac compiled DigiTemp is available but it took me a while to find the proper version that works on my Macs:
http://www.perceptiveautomation.com/userforum/viewtopic.php?f=26&t=8016
download the DigiTemp plugin from the link provided on the forum and use the appropriate version for your Mac architecture. It does not have to be installed in any specific location as long as you provide the full path to the program and supporting files when calling it from the terminal or bash.
This is how I call the program on my Mac from Perl:
View Code`/http/digitemp/digitemp_i386 -q -c /http/digitemp/digitemp.conf -l /http/digitemp/temperature.txt -a`


Install PL2303 driver:
http://prolificusa.com/pl-2303hx-drivers
and run the following in your Terminal:
View Codels -la /dev

and make sure you have cu.usbserial listed
then check your digitemp:
View Code/papth_to_digitemp/digitemp_i386 -s /dev/cu.usbserial -w


you should see something similar to this:
View CodeDigiTemp v3.5.0 Copyright 1996-2007 by Brian C. Lane
GNU Public License v2.0 - http://www.digitemp.com
Turning off all DS2409 Couplers
.
Devices on the Main LAN
102F84350208004D : DS1820/DS18S20/DS1920 Temperature Sensor

If all goes well - you can start building your one-wire network by permanently soldering, sealing, running wires etc.

My live reports can be found here:
http://www.wd-co.com/digitemp
reports updated every 15 minutes.There's no place like ~
!MADS7!
RE: Practical DigiTemp on Mac OS X - hardware and software.Posted: Wednesday, July 17, 2013 [08:12:42] - 2
rootPosted by:rootMember Since:
June 16 2010
Posts: 69
Continuing with software side on my DigiTemp project.
I ran DigiTemp from crontab every 15 minutes. This is the program that doe the job:
View Code#!/usr/bin/perl
unless($ENV{'SECURITYSESSIONID'}) {
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local:/usr/local/bin:/opt/local/bin';
}
# LOGGING THE RESULT IN READABLE TXT FILE
open STDOUT, '>/http/digitemp/start.prog.result.txt';
$frtm=time;$frtm = localtime($frtm);
print "$frtm\t";

if(-f "/http/digitemp/digitemp_i386") {
print "digitemp_i386 exist\n";
my $program = "/http/digitemp/digitemp_i386";
my $foundit = 0;

open (IN, "ps axw |");
while (<IN>) {
if (/$program/) {
print "daemon was runnig - need to kill it\n";
$_ =~ s/(\d{1,6})\s/$&/;$pid=$1;$pid =~ s/\D//g;
`kill -9 $pid`;
$foundit = 1;
last;
}
}
close IN;
unless ($foundit) {
print "No $program was running - starting it\n";
$program = "/http/digitemp/digitemp_i386 -q -c /http/digitemp/digitemp.conf -l /http/digitemp/temperature.txt -a";
exec "$program";
}
$did = '';
} ## END IF FILE PRESENT
exit(0);

The reason I am running it this way is to have readable TXT log file in case something goes wrong.
Once output is written - the other Perl Daemon picks the process if it sees "/http/digitemp/temperature.txt" file present. It runs the actual processing Perl program that reads the last DigiTemp readings, logs them and creates graphs using Fly program:

View Code#!/usr/bin/perl
unless($ENV{'SECURITYSESSIONID'}) {
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local:/usr/local/bin:/opt/local/bin';
}
open STDOUT, '>/http/digitemp/last.run.txt' or die "Can't write to /http/digitemp/last.run.txt: $!";
## CREATE SUPPORTING DIRECTORIES IF NOT PRESENT
unless(-f "/http/digitemp/temperature.txt") {print "No file /http/digitemp/temperature.txt found - exit\n";exit(0);}
unless(-d "/http/digitemp/logs") {`mkdir -p /http/digitemp/logs`;}
unless(-d "/http/web/digitemp/maps") {`mkdir -p /http/web/digitemp/maps`;}
unless(-d "/http/web/digitemp/byday") {`mkdir -p /http/web/digitemp/byday`;}
unless(-d "/http/web/digitemp") {`mkdir -p /http/web/digitemp`;}

%weekDays = ('Sun','Sunday','Mon','Monday','Tue','Tuesday','Wed','Wednesday','Thu','Thursday','Fri','Friday','Sat','Saturday');
%let_month = ('Jan','January','Feb','February','Mar','March','Apr','April',
'May','May','Jun','June','Jul','July','Aug','August',
'Sep','September','Oct','October','Nov','November','Dec','December');
$kztme = time; $kztime = localtime($kztme);$kztime =~ tr/ //s;($wdK,$moK,$daK,$tiK,$ye) = split(/\s/,$kztime);$imchk = "$ye.$moK";
if($moK eq 'Jan' && $daK == 1) {
($chkHr,$chkMi,$chkSec) = split(/\:/,$tiK);
if($chkHr < 1 && $chkMi < 5) {$adye=$ye-1;}
}

$urlsendd = "http://Your_Server_URL/digitemp/upload.file.cgi";;

$d = `cat /http/digitemp/temperature.txt`;`mv /http/digitemp/temperature.txt /http/digitemp/temperature1.txt`;
@all = split(/\n/,$d);$d = '';
foreach $l (@all) {$l =~ s/\r//g;
#Jul 13 16:06:42 Sensor 0 C: 23.12 F: 73.62 - SAMPLE OF DATA FOUND IN DIGITEMP LOG FILE
$l =~ s#(.*?)Sensor (\d{1,2})\sC\: (\d{1,3}\.\d{1,2})\sF\: (\d{1,3}\.\d{1,2})#$1#;
$sensor=$2;$tempc=$3;$tempf=$4;
$l =~ s/\s+$//;
($mo,$da,$ti) = split(/ /,$l);
$daymatch=$da;
$byall{$mo}{$da}{$ti}{$sensor}{'C'}=$tempc;
$byall{$mo}{$da}{$ti}{$sensor}{'F'}=$tempf;
$lastTemp{$sensor}="$tempc °C       $tempf °F";
$ye1=$ye;
if($adye && $mo eq 'Dec') {$ye1=$adye;}
$donerun = "$let_month{$mo} $da, $ye1 at $ti EDT";
} ## FOREACH LINE END
@all=();

@months = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

## ADD SENSORS HERE TO DESCRIBE WHAT THE MEASURE AND WHERE - HUMAN READABLE DATA
%sensors = ('0','Outside Temperature');

$monmatch="$let_month{$moK}.$ye";
$dayfile="$moK.$daK.$ye";

open(TXT,">/http/digitemp/last.temps.txt");
foreach $sens (sort keys(%lastTemp)) {$blnk='';$blnke='';
unless($lnkwritten) {$lnkwritten=1;$blnk='<a href="/digitemp/">';$blnke='</a>';}
print TXT "$blnk$sensors{$sens}$blnke: <span style=\"font-size:1.8em;font-weight:bold;\">$lastTemp{$sens}</span>\n";
if($sensors{$sens} eq 'Outside Temperature') {$savetotop = $lastTemp{$sens};$savetotop =~ s#\°##g;$savetotop =~ s#\ ##g;}
} ##
print TXT "<span style=\"font-size:.9em;\">Updated: $donerun</span>\n";close(TXT);

foreach $mo (keys(%byall)) {$mnchk{$mo}=1;}
if($mnchk{'Dec'} && $mnchk{'Jan'}) {$decyear=$ye-1;$corryear{'Dec'}=$decyear;}%mnchk=();

foreach $mo (keys(%byall)) { ########### MO
if($corryear{$mo}) {$year=$corryear{$mo};} else {$year=$ye;}
$dorecs{"$year.$mo"}="$let_month{$mo} $year";
open(LOG,">>/http/digitemp/logs/$year.$mo.txt");
foreach $da (keys %{$byall{$mo}}) { ##### DA
foreach $ti (keys %{$byall{$mo}{$da}}) { ####### TI
foreach $sens (keys %{$byall{$mo}{$da}{$ti}}) { ###### SENSOR
$sensor = $sensors{$sens};
print LOG "$let_month{$mo} $year\t$da\t$ti\t$sensor";
foreach $ttype (keys %{$byall{$mo}{$da}{$ti}{$sens}}) { ###### TEMP TYPE
$val = $byall{$mo}{$da}{$ti}{$sens}{$ttype};
print LOG "\t$ttype $val";
} ## FOREACH TEMP TYPE END ################################### TEMP TYPE
print LOG "\n";
} ## FOREACH SENSOR END ############################## SENSOR
} ## FOREACH TIME END ########################## TI
} ## FOREACH DAY END #################### DA
} ## FOREACH MONTH END ################# MO

## DONE READING RECORDS ###################

## COLORS USED IN GRAPH IN FORMAT FLY WOULD UNDERSTAND (RGB)
%colors = ('Outside Temperature','232,25,25',
'Text','0,0,0',
'Grid','235,235,235',
'Box','99,99,99',
'Weekend','232,25,25'
);

%monDig = ('January','01','February','02','March','03','April','04','May','05','June','06','July','07','August','08','September','09','October','10','November','11','December','12');
$todayhigh=-100;$todaylow=200;
foreach $file (keys(%dorecs)) { ##
$maxtemp=-100;$mintemp=400;
$fstartx=45;$fstarty=650;$ymv=20;$xmv=200;%wkend=();

unless(-f "/http/digitemp/logs/$file.txt") {next;}
$datestring = $file;$datestring =~ s/\./ /;($rmon,$ryear) = split(/ /,$dorecs{$file});

## USE MAC OS X BUILT-IN CALENDAR TO FIND WEEKENDS FOR THE GRAPH
$calendar = `cal $rmon $ryear`;$calendar =~ s/\r//g;
$moshort=substr($rmon,0,3);
$calendar =~ s#\W+$##;unless($calendar =~ m/\d{1,2} \d{1,2} \d{1,2} \d{1,2} \d{1,2} \d{1,2} \d{1,2}$/) {$calendar .= ' ';} else {$calendar .= "\n";}
while($calendar =~ /(\d{1,2})(\n)/gs) {$wkend{$1}=1;}
while($calendar =~ /(\n)( \d{1,2}|\d{1,2})(\s)/gs) {$wkend{$2}=1;}
foreach $we (keys(%wkend)) {delete $wkend{$we};$we =~ s/\D//g;if(length($we) < 2) {$we = '0'.$we;}$wkend{$we}=1;}

## RUN SMALL BASH FILE TO FIND A NUMBER OF DAYS IN A MONTH
$daysread = `/http/digitemp/getdate.sh $ryear $monDig{$rmon}`;$daysread =~ s#(.*?)The last day is\s(\d{2})(.*?)##s;$daysread = $2;
$dawritte='';
open(FLY,">/http/ipLog/$file.flybuilt.txt");
if($monmatch eq "$rmon.$ryear") {$toToday="new\nsize 1200,700\nfill 1,1,255,255,255\nstring $colors{'Text'},40,10,giant,$rmon $daK, $ryear\n";$dawritte=1;
$toToday .= "string 232,25,25,440,10,giant,Current Outside Temperature: $savetotop\nstring 232,25,25,42,45,giant,C\nstring $colors{'Text'},440,30,small,Last updated: $donerun\n";
}
print FLY "new\nsize 1200,700\nfill 1,1,255,255,255\nstring $colors{'Text'},40,10,giant,$dorecs{$file} DigiTemp Logs\n";
print FLY "string 232,25,25,440,10,giant,Current Outside Temperature: $savetotop\n";
print FLY "string 232,25,25,42,45,giant,C\n";
print FLY "string $colors{'Text'},440,30,small,Last updated: $donerun\n";
$d = `cat /http/digitemp/logs/$file.txt`;@all = split(/\n/,$d);$d='';
foreach $l (@all) { #######
($my,$da,$ti,$sens,$fahr,$celc) = split(/\t/,$l);$celc =~ s/C //;$celc =~ s/ //g;$fahr =~ s/F //g;$fahr =~ s/ //g;
unless($ddone{$sens}) {
if($fstarty > 689) {$fstartx += $xmv; $fstarty=650;}
$fstartytxt = $fstarty - 7;$fstartxtxt = $fstartx + 7;
print FLY "fcircle $fstartx,$fstarty,10,$colors{$sens}\nstring $colors{$sens},$fstartxtxt,$fstartytxt,small, - $sens\n";$ddone{$sens}=1;
$toToday .= "fcircle $fstartx,$fstarty,10,$colors{$sens}\nstring $colors{$sens},$fstartxtxt,$fstartytxt,small, - $sens\n";
$fstarty += $ymv;
} ## END UNLESS MADE
if($sens =~ m/Temperature/i && $mintemp > $celc) {$mintemp = $celc;$minfahr=$fahr;}
if($sens =~ m/Temperature/i && $maxtemp < $celc) {$maxtemp = $celc;$maxfahr=$fahr;}
$ti =~ s#\:\d{1,2}$##;$ti =~ s/\:/\./g;
$write{$sens}{$da}{$ti}="$celc\t$fahr";
} ## FOREACH LINE END #####
$byday = 1080 / $daysread;$byday = sprintf("%0.0f",$byday);$widplot = $byday * $daysread;$endx=$widplot + 45;
$hiplot=550; ## V SIZE FOR PLOT
$botstart=620; ## PLOT COORD BOTTOM
$toplimit=$botstart-$hiplot; ## PLOT TOP MAX COORD
$sttx=45;$step = $byday / 2;$step = sprintf("%0.0f",$step);
print "Step: $step\tEndX: $endx\tByDay: $byday\n";

## PRINT VERTICAL GRID FOR DAYS IN A MONTH
$xxrrem='';
for(1..$daysread) {
$sttx += $byday;$sttxt = $sttx - $step;$sttxt -= 6;if($sttx > $endx) {$sttx = $endx-1;}
$digday = $_;unless(length($digday) == 2) {$digday = '0'.$digday;}
if($wkend{$digday}) {$ftext=$colors{'Weekend'};
print FLY "stringup $ftext,$sttxt,635,small,$digday\n";
} else {$ftext=$colors{'Text'};}
print FLY "frect $sttx,$toplimit,$sttx,$botstart,$colors{'Grid'}\nstringup $ftext,$sttxt,634,small,$digday\n";
unless($xxrrem) {$xxrrem=45;}
$dayfile1="$moshort.$digday.$ryear";
print "\t/http/web/digitemp/byday/$dayfile1.gif to check\n";
if(-f "/http/web/digitemp/byday/$dayfile1.gif") {
print "\tGot map record for $dayfile1.gif\n";
$bydaymap.="<area href=\"byday/$dayfile1.gif\" shape=\"rect\" coords=\"$xxrrem,$toplimit,$sttx,$botstart\">\n";
}
$xxrrem=$sttx;
} ## FOR END
$toplmprint = $toplimit - 8;$frhtxt=$sttx+8;
print FLY "line 45,$toplimit,$endx,$toplimit,$colors{'Grid'}\nstring $colors{'Text'},10,$toplmprint,small,$maxtemp\n";
print FLY "string $colors{'Text'},$frhtxt,$toplmprint,small,$maxfahr\n";
print FLY "string $colors{'Text'},10,612,small,$mintemp\n";
print FLY "string $colors{'Text'},$frhtxt,612,small,$minfahr\n";
$difftmp = $maxtemp - $mintemp;
($maxtempe,$sh) = split(/\./,$maxtemp);
$perdeg = $hiplot / $difftmp;#$perdeg = sprintf("%0.0f",$perdeg);
$frstup = $mintemp + 1;($frstup,$sh) = split(/\./,$frstup);
$cdif = $frstup - $mintemp;$cdif *= $perdeg;$cdif = sprintf("%0.0f",$cdif);$prf = $botstart - $cdif;$txtp=$prf-8;
if($prf > $toplimit) {print FLY "line 45,$prf,$endx,$prf,$colors{'Grid'}\nstring $colors{'Text'},10,$txtp,small,$frstup\n";}
$strfahr = ($frstup * 1.8) + 32;$frhtxt1=$frhtxt-14;$strfahr = sprintf("%0.2f",$strfahr);
print FLY "string $colors{'Text'},$frhtxt,$txtp,small,$strfahr\nstring 232,25,25,$frhtxt1,45,giant,F\n";
$toToday .= "string 232,25,25,$frhtxt1,45,giant,F\n";
print "Maxttmp: $maxtempe\tMinttmp: $frstup\tPre Degree: $perdeg\n";

if($frstup < $maxtempe) {
for($frstup..$maxtempe) {
$frstup++;$prf -= $perdeg;$prf1 = sprintf("%0.0f",$prf);$txtp=$prf1-8;
if($prf < $toplimit) {last;}
print "line 45,$prf1,$endx,$prf1,$colors{'Grid'} - Temp: $frstup\n";
print FLY "line 45,$prf1,$endx,$prf1,$colors{'Grid'}\nstring $colors{'Text'},10,$txtp,small,$frstup\n";
$strfahr = ($frstup * 1.8) + 32;$strfahr = sprintf("%0.2f",$strfahr);
print FLY "string $colors{'Text'},$frhtxt,$txtp,small,$strfahr\n";
} ## FOR END
} ## END IF LESS THAN MAX

## PRINT "LEFT AND BOTTOM OF THE GRID
print FLY "line 45,$botstart,$endx,$botstart,$colors{'Box'}\nfrect 45,$toplimit,45,$botstart,$colors{'Box'}\n";

print "Writing Temp values..\n______________\n";
$byhr = $byday / 24;
foreach $sens (keys(%write)) { ########### SENSOR
#print "Day Start: $daystart\tBy HR: $byhr\n";
foreach $da (sort num keys %{$write{$sens}}) { ### DAY
$daystart = $byday * $da;$daystart += 45;$daystart -= $byday;
print "Sensor: $sens\tDay: $da\n";
foreach $ti (sort num keys %{$write{$sens}{$da}}) { ### TIME
$temp = $write{$sens}{$da}{$ti};($temp,$farht) = split(/\t/,$temp);
($hr,$min) = split(/\./,$ti);$tmprem=$temp;
print "$ti\t$temp";
$temp=$temp-$mintemp;
$ycoord = $temp * $perdeg;$ycoord = sprintf("%0.0f",$ycoord);$ycoord = $botstart - $ycoord;
$xhr = $min / .6;$xhr /= 100;$xhr += $hr;$xhr *= $byhr;
print "\tX: $xhr\n";
$xhr += $daystart;$xhr = sprintf("%0.0f",$xhr);
$xhr1=$xhr+1;$ycoord1=$ycoord+1;
print FLY "circle $xhr,$ycoord,5,$colors{$sens}\n";
if($remx && $remy) {print FLY "line $remx,$remy,$xhr,$ycoord,$colors{$sens}\n";
print "line $xhr,$ycoord,$remx,$remy,$colors{$sens}\n";
}
print "\t$xhr $ycoord\n";
$remx=$xhr;$remy=$ycoord;
$xhr='';$ycoord='';
if($daymatch == $da && $dawritte) {$todayhsh{$sens}{$ti}="$tmprem\t$farht";
if($todayhigh < $tmprem) {$todayhigh = $tmprem;$todayhighf=$farht;}
if($todaylow > $tmprem) {$todaylow = $tmprem;$todaylowf=$farht;}
} ## END IF DAYS MATCH
} ############################################ TIME
} ## FOREACH DAY END ######################### DAY
$xhr2=$remx+4;$ycoord2=$remy-8;
print FLY "string $colors{$sens},$xhr2,$ycoord2,small,$tmprem\n";
print "string $colors{$sens},$xhr2,$ycoord2,small,$tmprem\n";
$remx='';$remy='';
} ## FOREACH SENSOR END ################### SENSOR

close(FLY);%write=();
print "Min: $mintemp\tMax: $maxtemp\n\n";
if($bydaymap) {open(TXT,">/http/web/digitemp/maps/$file.txt");print TXT "<map name=\"$file\">\n$bydaymap</map>";close(TXT);}
$bydaymap='';
# SEND MAP FILE TO YOUR SERVER
$report = `curl --connect-timeout 30 -F file=@/PATH_TO_FILE/$file.txt -F pass=PASSWORD -F fn=$file.txt $urlsendd`;

$output = `/http/fly/fly -q -i /http/ipLog/$file.flybuilt.txt`;
open(GIF,">/http/web/digitemp/$file.gif");
print GIF "$output";$output='';close(GIF);
if(-f "/http/digitemp/$file.flybuilt.txt") {unlink "/http/digitemp/$file.flybuilt.txt";}
unlink "/http/ipLog/$file.flybuilt.txt";
# SEND MONTH IMAGE FILE TO YOUR SERVER
$report = `curl --connect-timeout 30 -F file=@/PATH_TO_FILE/$file.gif -F pass=PASSWORD -F fn=$file.gif $urlsendd`;
} ## FOREACH FILE END ############

## FINISHED WITH MONTHLY GRAPH IMAGE FILE - NOW CREATE DAILY GRAPH

$sttx=45;
$byday = 1080 / 24;$byday = sprintf("%0.0f",$byday);$widplot = $byday * 24;$endx=$widplot + 45;
$toToday1 = "line 45,$botstart,$endx,$botstart,$colors{'Box'}\nfrect 45,$toplimit,45,$botstart,$colors{'Box'}\n";
print "line 45,$botstart,$endx,$botstart,$colors{'Box'}\nfrect 45,$toplimit,45,$botstart,$colors{'Box'}\n";

for(1..24) { ####
$hour=$_;
$sttx += $byday;$sttxt = $sttx - 6;if($sttx > $endx) {$sttx = $endx-1;}
unless(length($hour) == 2) {$hour = '0'.$hour;}
$ftext=$colors{'Text'};
$toToday .= "frect $sttx,$toplimit,$sttx,$botstart,$colors{'Grid'}\nstringup $ftext,$sttxt,635,small,$hour\n";
} ## FOR END ####
$hrstr=$endx+10;$hrystr=$botstart+4;
$toToday .= "string 232,25,25,$hrstr,$hrystr,giant,HRs\n";

$difftmp = $todayhigh - $todaylow;$mintemp=$todaylow;$maxtempe=$todayhigh;
$txtp = $botstart - 8;
$txtp1 = $toplimit - 7;$frhtxt-=3;
$toToday .= "string $colors{'Text'},10,$txtp,small,$todaylow\nstring $colors{'Text'},10,$txtp1,small,$todayhigh\n";
$toToday .= "string $colors{'Text'},$frhtxt,$txtp,small,$todaylowf\nstring $colors{'Text'},$frhtxt,$txtp1,small,$todayhighf\n";
$toToday .= "line 45,$toplimit,$endx,$toplimit,$colors{'Grid'}\n";

$perdeg = $hiplot / $difftmp;
print "Day $dayfile\: PerDeg $perdeg\tTmpDiff: $difftmp\tHigh: $todayhigh\tLow: $todaylow\n";
$frstup = $mintemp + 1;($frstup,$sh) = split(/\./,$frstup);
$cdif = $frstup - $mintemp;$cdif *= $perdeg;$cdif = sprintf("%0.0f",$cdif);$prf = $botstart - $cdif;$txtp=$prf-8;
$strfahr = ($frstup * 1.8) + 32;$strfahr = sprintf("%0.2f",$strfahr);
if($prf > $toplimit) {$toToday .= "line 45,$prf,$endx,$prf,$colors{'Grid'}\nstring $colors{'Text'},10,$txtp,small,$frstup\nstring $colors{'Text'},$frhtxt,$txtp,small,$strfahr\n";
print "line 45,$prf,$endx,$prf,$colors{'Grid'}\nstring $colors{'Text'},10,$txtp,small,$frstup\nstring $colors{'Text'},$frhtxt,$txtp,small,$strfahr\n";}

print "\nBy degree grid\n";
if($frstup < $maxtempe) {
for($frstup..$maxtempe) {
$frstup++;$prf -= $perdeg;$prf1 = sprintf("%0.0f",$prf);$txtp=$prf1-8;
if($prf < $toplimit) {last;}
$strfahr = ($frstup * 1.8) + 32;$strfahr = sprintf("%0.2f",$strfahr);
$toToday .= "line 45,$prf1,$endx,$prf1,$colors{'Grid'}\nstring $colors{'Text'},10,$txtp,small,$frstup\nstring $colors{'Text'},$frhtxt,$txtp,small,$strfahr\n";
print "line 45,$prf1,$endx,$prf1,$colors{'Grid'}\nstring $colors{'Text'},10,$txtp,small,$frstup\nstring $colors{'Text'},$frhtxt,$txtp,small,$strfahr\n";
} ## FOR END
} ## END IF LESS THAN MAX
$toToday .= $toToday1;$toToday1='';
$remx='';$remy='';
#$todayhsh{$sens}{$ti}=$tmprem;
$byday = 1080 / 24;$byday = sprintf("%0.0f",$byday);$byhr = 1080 / 24;
$daystart=45;
print "By Hour: $byhr\n";
foreach $sens (sort keys(%todayhsh)) { #############
foreach $ti (sort num keys %{$todayhsh{$sens}}) { ## TIME ######
$temp = $todayhsh{$sens}{$ti};($hr,$min) = split(/\./,$ti);
$temp=$temp-$mintemp;($temp,$fahr) = split(/\t/,$temp);
$ycoord = $temp * $perdeg;$ycoord = sprintf("%0.0f",$ycoord);$ycoord = $botstart - $ycoord;
$xhr = $min / .6;$xhr /= 100;$xhr += $hr;$xhr *= $byhr;
print "\tX: $xhr\tY: $ycoord\n";
$xhr += $daystart;$xhr = sprintf("%0.0f",$xhr);
$toToday .= "circle $xhr,$ycoord,5,$colors{$sens}\n";
if($remx && $remy) {
$toToday .= "line $remx,$remy,$xhr,$ycoord,$colors{$sens}\n";
print "line $xhr,$ycoord,$remx,$remy,$colors{$sens}\n";
}
print "\t$xhr $ycoord\n";
$remx=$xhr;$remy=$ycoord;
$xhr='';$ycoord='';
} ## FOREACH TIME END ##########################################
} ## FOREACH SENSOR END ############################

open(FLY,">/http/ipLog/day.$daymatch.flybuilt.txt");print FLY $toToday;close(FLY);$toToday='';
$output = `/http/fly/fly -q -i /http/ipLog/day.$daymatch.flybuilt.txt`;
open(GIF,">/http/web/digitemp/byday/$dayfile.gif");
print GIF "$output";$output='';close(GIF);
unlink "/http/ipLog/day.flybuilt.txt";
# SEND DAILY GRAPH TO YOUR SERVER
$report = `curl --connect-timeout 30 -F file=@/PASS_TO_YOUR_FILE/$dayfile.gif -F pass=PASSWORD -F fn=$dayfile.gif $urlsendd`;

## NOW WRITE A LOCAL FILES TO DISPLAY INFORMATION ON HOME NETWORK
unlink "/http/digitemp/temperature1.txt";
@all = glob("/http/web/digitemp/*");
foreach $file (@all) {
unless($file =~ m/\.gif$/) {next;}
$fn=$file;$fn =~ s#/http/web/digitemp/##;$fn =~ s/\.\w{3}$//;($year,$month) = split(/\./,$fn);
$files{$year}{$month}=1;
} ##

$nbsp=' ';
foreach $year (sort num keys(%files)) {
@months1=@months;$mcount='';
$topage .= "<tr><td colspan=7><b>$year</b></td></tr>\n<tr><td>$nbsp</td>";$nbsp='';
foreach $month (@months1) {$lnk='';$lnke='';$split='';
$mcount++;
if($mcount == 6) {$split="</tr>\n<tr><td></td>";}
if($files{$year}{$month}) {$lnk="<a href=\"/digitemp/$year.$month.gif\">";$lnke='</a>';}
$topage .= "<td>$lnk$let_month{$month}$lnke</td>$split";
} ## FOREACH MONTH END
$topage .= "</tr>\n";
} ## FOREACH YEAR END
$topage = "<p>More records<br>\n<table border=0>$topage</table></p>";

unless(-f "/http/web/digitemp/index.cgi") {$chmod=1;}

open(TXT,">/http/web/digitemp/index.cgi");
print TXT <<EOH;
#!/usr/bin/perl
if(-f "/http/web/digitemp/maps/$imchk.txt") {\$immap=" usemap=\\"#$imchk\\"";
\$map=`cat /http/web/digitemp/maps/$imchk.txt`;}
\$pagetext = '$topage';
\$currimage = '<p><a href="/digitemp/$imchk.gif"><img src="/digitemp/$imchk.gif" border=1 width=1200 height=700'.\$immap.'></a></p>';
\$body = '<html>
<head><title>Temperature Graphs by Year and Month</title>
<link rel="stylesheet" type="text/css" href="/Album/all.css" media="screen,projection"/>
</head><body bgcolor="#ffffff">';
print "Content-type: text/html\\n\\n\$body\\n<h2>Temperature Graphs by Year and Month</h2>\\n\$currimage\\n\$pagetext\\n\$map\\n</body>\\n</html>\\n";
exit(0);
EOH
close(TXT);
if($chmod) {`chmod 755 /http/web/digitemp/index.cgi`;}
exit(0);

sub num {$a <=> $b;}


and a small Bash program "getdate.sh" used in above Perl program to get the number of days in a given month:

View Code#!/bin/bash
# last day for month
lastday() {
# ja fe ma ap ma jn jl ag se oc no de
mlength=('xx' '31' '28' '31' '30' '31' '30' '31' '31' '30' '31' '30' '31')

year=$1
month=$2

if [ $month -ne 2 ] ; then
echo ${mlength[$month]}
return 0
fi

leap=0
((!(year%100))) && { ((!(year%400))) && leap=1 ; } || { ((!(year%4))) && leap=1 ; }

feblength=28
((leap)) && feblength=29
echo $feblength
}

# date to Julian date
date2jd() {

year=$1
month=$2
day=$3
lday=$(lastday $year $month) || exit $?

if ((day<1 || day> lday)) ; then
echo day out of range
exit 1
fi

echo $(( jd = day - 32075
+ 1461 * (year + 4800 - (14 - month)/12)/4
+ 367 * (month - 2 + (14 - month)/12*12)/12
- 3 * ((year + 4900 - (14 - month)/12)/100)/4
- 2400001 ))
}

jd2dow()
{
days=('Sunday' 'Monday' 'Tuesday' 'Wednesday' 'Thursday' 'Friday' 'Saturday')

jd=$1
if ((jd<1 || jd>782028)) ; then
echo julian day out of range
return 1
fi

((dow=(jd+3)%7))

echo ${days[dow]}
}

echo -n "The first day is 01.$1.$2, "
jd2dow $(date2jd $1 $2 01)
echo -n "The last day is $(lastday $1 $2).$1.$2, "
jd2dow $(date2jd $1 $2 $(lastday $1 $2))


Fly program can be downloaded here:
http://www.w3perl.com/flyThere's no place like ~
 
Post Reply
Home - Coding: AppleScript C Perl Shell Xcode