1 | #!/usr/bin/env ruby
|
---|
2 | #
|
---|
3 | # Author: Norbert Wigbels - foobla.wigbels.de/uber-foobla
|
---|
4 | #
|
---|
5 | # HotColdRain continously reads output of USB-WDE1
|
---|
6 | # and pushes data to mosquitto-db
|
---|
7 | #
|
---|
8 | # todos:
|
---|
9 | #
|
---|
10 |
|
---|
11 | #------------------------------------------
|
---|
12 | # Tainted mode 0-4
|
---|
13 | $SAFE=0
|
---|
14 |
|
---|
15 |
|
---|
16 | #------------------------------------------
|
---|
17 | require 'logger'
|
---|
18 | require 'serialport'
|
---|
19 | require 'mqtt'
|
---|
20 |
|
---|
21 | #------------------------------------------
|
---|
22 | class HotColdRain < Logger::Application
|
---|
23 | attr_accessor :temp1, :humidity1, :serialport, :mypath
|
---|
24 |
|
---|
25 | def initialize(application_name)
|
---|
26 | super(application_name)
|
---|
27 | @serialport = SerialPort.new("/dev/ttyUSB0", 9600, 8, 1, SerialPort::NONE)
|
---|
28 | @mypath = File.expand_path(File.dirname(__FILE__))
|
---|
29 | end
|
---|
30 |
|
---|
31 | def logOutReadableData(datagrams)
|
---|
32 | puts "TemperaturUnten: " + datagrams[3]
|
---|
33 | puts "LuftfeuchtigkeitUnten: " + datagrams[11]
|
---|
34 | puts
|
---|
35 | puts "TemperaturOben: " + datagrams[4]
|
---|
36 | puts "LuftfeuchtigkeitOben: " + datagrams[12]
|
---|
37 | puts
|
---|
38 | puts "TemperaturAussen: " + datagrams[19]
|
---|
39 | puts "LuftfeuchtigkeitAussen: " + datagrams[20]
|
---|
40 | puts "Windgeschwindigkeit km/h: " + datagrams[21]
|
---|
41 | puts "Niederschlag: " + datagrams[22]
|
---|
42 | puts "Regen ja/nein: " + datagrams[23]
|
---|
43 | puts "---------------------"
|
---|
44 | end
|
---|
45 |
|
---|
46 | def run
|
---|
47 | begin
|
---|
48 | while true do
|
---|
49 | openformat = @serialport.readline
|
---|
50 | #openformat = "$1;1;;21,3;20,0;;;;;;;50;55;;;;;;;4,9;74;3,0;6;0;0"
|
---|
51 | datagrams = openformat.split(';')
|
---|
52 |
|
---|
53 | logOutReadableData(datagrams)
|
---|
54 |
|
---|
55 | # $1;1;;21,3;20,0;;;;;;;50;55;;;;;;;4,9;74;3,0;6;0;0
|
---|
56 | # extract only data
|
---|
57 | # then replace empty data with U
|
---|
58 | # then localize float value
|
---|
59 | # finally prepare updatestring for rrdtool
|
---|
60 | datagrams = datagrams.values_at(3..23)
|
---|
61 | datagrams.collect! { |element| (element.empty?) ? "U" : element }
|
---|
62 | datagrams.collect! { |element| (element.include? ',') ? element.sub!(',','.') : element }
|
---|
63 | rrddata = 'N:'+datagrams.join(':')
|
---|
64 |
|
---|
65 | system("rrdtool update #{mypath}/weather.rrd #{rrddata}")
|
---|
66 |
|
---|
67 | # MQTT::Client.connect('mqtt://notwist:pixies@wigbels.net') do |c|
|
---|
68 | # c.publish('oben/temperatur', datagrams[0])
|
---|
69 | # end
|
---|
70 | end
|
---|
71 | @serialport.close
|
---|
72 | rescue => msg
|
---|
73 | log(ERROR, "error: #{msg}")
|
---|
74 | retry
|
---|
75 | ensure
|
---|
76 | @serialport.close
|
---|
77 | end
|
---|
78 | end
|
---|
79 | end
|
---|
80 |
|
---|
81 |
|
---|
82 | #------------------------------------------
|
---|
83 | status = HotColdRain.new("HotColdRain").start
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|