Last change
on this file since 2c75e95 was
2c75e95,
checked in by Norbert Wigbels <frickel@…>, 7 years ago
|
Wassersensor
|
-
Property mode set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[2c75e95] | 1 | #!/usr/bin/env ruby |
---|
| 2 | require 'pi_piper' |
---|
| 3 | require 'eventmachine' |
---|
| 4 | |
---|
| 5 | include PiPiper |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | mypath = File.expand_path(File.dirname(__FILE__)) |
---|
| 9 | trigger_step = 0.01 |
---|
| 10 | trigger_watch = 0 |
---|
| 11 | counter = 0.0 |
---|
| 12 | timestamp = Time.now.to_i |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | def last_rrd_count(concretepath) |
---|
| 16 | val = 0.0 |
---|
| 17 | handle = IO.popen("rrdtool lastupdate #{concretepath}") |
---|
| 18 | handle.each_line do |line| |
---|
| 19 | m = line.match("^[0-9]*: ([0-9.]*) [0-9.]*") |
---|
| 20 | if m |
---|
| 21 | val = m[1].to_f |
---|
| 22 | break |
---|
| 23 | end |
---|
| 24 | end |
---|
| 25 | return val |
---|
| 26 | end |
---|
| 27 | |
---|
| 28 | watch :pin => 9, :trigger => :falling do |
---|
| 29 | #puts "Pin changed from #{pin.last_value} to #{pin.value}" |
---|
| 30 | puts('triggered watch to 1') |
---|
| 31 | trigger_watch = 1 |
---|
| 32 | end |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | rrdpath = mypath + "/gasneu.rrd" |
---|
| 36 | counter = last_rrd_count(rrdpath) |
---|
| 37 | puts('Counter restored to: ' + counter.to_s) |
---|
| 38 | #PiPiper::Pin.new(:pin => 24, :direction => :in) |
---|
| 39 | |
---|
| 40 | EventMachine.run do |
---|
| 41 | EM.add_periodic_timer(1) do |
---|
| 42 | if trigger_watch == 1 then |
---|
| 43 | counter = counter + trigger_step |
---|
| 44 | #system("rrdtool update #{rrdpath} N:#{counter}:#{trigger_step}") |
---|
| 45 | puts('Counter updated to: ' + counter.to_s) |
---|
| 46 | timestamp = Time.now.to_i |
---|
| 47 | trigger_watch = 0 |
---|
| 48 | elsif Time.now.to_i - timestamp > 3600 |
---|
| 49 | #system("rrdtool update #{rrdpath} N:#{counter}:0") |
---|
| 50 | timestamp = Time.now.to_i |
---|
| 51 | end |
---|
| 52 | end |
---|
| 53 | end |
---|
| 54 | |
---|
Note: See
TracBrowser
for help on using the repository browser.