-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtl433_to_mqtt.rb
executable file
·55 lines (50 loc) · 1.32 KB
/
rtl433_to_mqtt.rb
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
#!/usr/bin/env ruby
require 'mqtt'
require 'json'
require 'pty'
require 'colored'
require 'yaml'
require 'logger'
if ARGV.delete("-l")
logger = Logger.new('logs/rtl433.log','daily')
logger.formatter = proc do |sev, datetime, progname, msg|
# sev and progname not helpful. datetime already in msg so just
# slam the json messages in log file - one msg line
"#{msg}\n"
end
end
if File.exists?('config.yml')
cfg = YAML.load_file('config.yml')['rtl_433']
else
cfg = YAML.load_file('config.yml.example')['rtl_433']
end
mqtt = MQTT::Client.connect(
:host => cfg['host'],
:port => cfg['port'],
:username => cfg['username'],
:password => cfg['password']
)
begin
PTY.spawn("rtl_433 -F json -M UTC") do |stdout, stdin, pid|
begin
last_line = ""
stdout.each do |line|
begin
# some sensors send the same message multiple times - skip dups
next if line == last_line
v = JSON.parse(line)
v['ts'] = Time.now.getutc.to_i
puts v.inspect.green
mqtt.publish(cfg['topic'], v.to_json)
logger.info(v.to_json) if logger
last_line = line
rescue JSON::ParserError
puts "Unparsable: #{line}".yellow
end
end
rescue Errno::EIO
end
end
rescue PTY::ChildExited
puts "rtl_433 process exited"
end