source: de.wigbels.ruby/hotcoldrain/hotcoldrain.rb@ f15e2ac

Last change on this file since f15e2ac was c22952c, checked in by Norbert Wigbels <njw@…>, 9 years ago

Stromzaehler

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[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#------------------------------------------
17require 'logger'
18require 'serialport'
19require 'mqtt'
20
21#------------------------------------------
22class 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
[c22952c]67 # MQTT::Client.connect('mqtt://notwist:pixies@wigbels.net') do |c|
68 # c.publish('oben/temperatur', datagrams[0])
69 # end
[516b934]70 end
71 @serialport.close
72 rescue => msg
73 log(ERROR, "error: #{msg}")
74 retry
75 ensure
76 @serialport.close
77 end
78 end
79end
80
81
82#------------------------------------------
83status = HotColdRain.new("HotColdRain").start
84
85
86
87
88
Note: See TracBrowser for help on using the repository browser.