blob: 5f8182ef9ea944ea94843f1189d5e7a4c4e6365b (
plain) (
blame)
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
|
#!/usr/bin/ruby -w
require "enumerable/statistics"
require "serialport"
SENSOR_BITS = 12
SENSOR_MAX = (1 << SENSOR_BITS) - 1
$stdout.sync = true
def log(msg)
$stdout.write("#{Time.now.strftime("%H:%M:%S")} #{msg}\n")
$stdout.flush
end
sensors = []
SerialPort.open("/dev/ttyACM1", 115200, 8, 1, SerialPort::NONE) do | port |
log("Allowing sensors to settle...")
sleep(3)
begin
port.read_nonblock(16 << 20)
rescue IO::WaitReadable
end
port.readline
log("Reading soil moisture sensors...")
10.times do
raw = port.readline
raise "Cannot parse sensor results #{raw.inspect}" unless raw =~ /\[([^\]]+)\]/
sensors << $1.split(/\s+/).filter { | x | not x.empty? }.map { | x | x.to_i }
log("Raw sensor results: \t#{sensors[-1].join("\t")}")
end
end
avgsensors = []
stdsensors = []
sensors.transpose.each do | sensor |
avgsensors << sensor.mean.round
stdsensors << sensor.stdev.round
end
log("Avg sensor results: \t#{avgsensors.join("\t")} \t(per sensor)")
log("SDv sensor results: \t#{stdsensors.join("\t")} \t(per sensor)")
avg = sensors.flatten.mean.round
std = sensors.flatten.stdev.round
log("Avg sensor results: \t#{avg} \t(#{(100.0 * avg.to_f / SENSOR_MAX.to_f).round}%) \t(overall)")
log("SDv sensor results: \t#{std} \t(overall)")
|