[516b934] | 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
|
---|
[daaef3e] | 23 | attr_accessor :temp1, :humidity1, :serialport, :mypath
|
---|
[516b934] | 24 |
|
---|
| 25 | def initialize(application_name)
|
---|
| 26 | super(application_name)
|
---|
| 27 | @serialport = SerialPort.new("/dev/ttyUSB0", 9600, 8, 1, SerialPort::NONE)
|
---|
[daaef3e] | 28 | @mypath = File.expand_path(File.dirname(__FILE__))
|
---|
[516b934] | 29 | end
|
---|
| 30 |
|
---|
[af9afaf] | 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 |
|
---|
[516b934] | 46 | def run
|
---|
| 47 | begin
|
---|
| 48 | while true do
|
---|
| 49 | openformat = @serialport.readline
|
---|
[af9afaf] | 50 | #openformat = "$1;1;;21,3;20,0;;;;;;;50;55;;;;;;;4,9;74;3,0;6;0;0"
|
---|
[516b934] | 51 | datagrams = openformat.split(';')
|
---|
[af9afaf] | 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 |
|
---|
[daaef3e] | 65 | system("rrdtool update #{mypath}/weather.rrd #{rrddata}")
|
---|
[af9afaf] | 66 |
|
---|
[516b934] | 67 | MQTT::Client.connect('mqtt://notwist:pixies@wigbels.net') do |c|
|
---|
[af9afaf] | 68 | c.publish('oben/temperatur', datagrams[0])
|
---|
[516b934] | 69 | end
|
---|
| 70 | #log(INFO, 'added 100 liter of gas to sensor-database')
|
---|
| 71 | end
|
---|
| 72 | @serialport.close
|
---|
| 73 | rescue => msg
|
---|
| 74 | log(ERROR, "error: #{msg}")
|
---|
| 75 | retry
|
---|
| 76 | ensure
|
---|
| 77 | @serialport.close
|
---|
| 78 | end
|
---|
| 79 | end
|
---|
| 80 | end
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | #------------------------------------------
|
---|
| 84 | status = HotColdRain.new("HotColdRain").start
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 |
|
---|