-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempd.pl
executable file
·129 lines (101 loc) · 3.28 KB
/
tempd.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/perl
# Set up the serial port
use Device::SerialPort;
use File::Slurp;
use warnings;
use strict;
use sigtrap qw/handler signal_handler normal-signals stack-trace error-signals/;
my @dmesg = split /\n/, `dmesg`;
my $tty;
foreach my $t ( glob '/dev/ttyUSB*' ) {
if(-e $t){
$tty = $t;
}
}
if(!defined($tty)){
die "Could not find any serial devices..";
}
print "Trying to open tty: $tty\n";
my $port = Device::SerialPort->new($tty)
or die "Cant open $tty ";
print "Connection to $tty a success!\n";
use Sys::Syslog; # Misses setlogsock.
use Sys::Syslog qw(:DEFAULT setlogsock); # Also gets setlogsock.
my $logopt = ""; # Options for sysslog
my $facility = "local0";
my $priority = "info";
print "Serial config start \n";
# 115200, 81N on the USB ftdi driver
$port->baudrate(115200); # you may change this value
$port->databits(8); # but not this and the two following
$port->parity("none");
$port->stopbits(1);
print "Serial config done. \n";
# now catch gremlins at start
my $tEnd = time()+2; # 2 seconds in future
while (time()< $tEnd) { # end latest after 2 seconds
my $c = $port->lookfor(); # char or nothing
next if $c eq ""; # restart if noting
print $c; # uncomment if you want to see the gremlin
last;
}
my $i=0;
if ( !-d "/tmp/derp" ) {
`mkdir /tmp/derp`;
if (! -r "/tmp/derp/running" ) {
`touch /tmp/derp/running`;
}
}
while (1) {
my $hold_temp; # New temperature to hold
my $pwr_sw_time; # Lock time for changing the power relay. As not to cause harm to physical equipment, and as a timing method.
my $fn_variables = "/tmp/derp/running";
# Check to see if new input variables has arrived
if(-e $fn_variables and -w $fn_variables){
my $tmp = `cat $fn_variables`;
chomp $tmp;
# Sanity check the input variables
if($tmp =~/^(.*?)$/){
$hold_temp = $1;
my $output = $hold_temp."\n";
print "[tempd.pl] Writing $output \n";
$port->write($output);
print "[tempd.pl] Writing done.\n";
}
unlink $fn_variables;
}
# Poll to see if any data is coming in
# print "[tempd.pl] Polling serial..\n";
my $char = $port->lookfor();
# print "[tempd.pl] Polling done.\n";
# If we get data, then print it
if ($char) {
my $input = "$char \n";
print " Input: $input";
if($input !~ /Temp/ig){
next;
}
my $date = `date`;
chomp $date;
my $output = $input;
# print $output;
write_file("/tmp/temp.log", $output);
my $ident = "Project_c";
setlogsock({ type => "udp", host => "einbox.net", port => "1234"});
openlog($ident, $logopt, $facility); # don't forget this
syslog($priority, $output);
closelog();
$i++;
}
sleep(0.5);
# Uncomment the following lines, for slower reading,
# but lower CPU usage, and to avoid
# buffer overflow due to sleep function.
#$port->lookclear;
#sleep (1);
}
sub signal_handler{
print "Trying to close serial port\n";
$port->close || warn "close failed";
die "[templ.pl] Closing down. Signal: $!";
}