source: de.wigbels.ruby/mqtttemp/mqtttemp.rb

Last change on this file was bc410f6, checked in by Norbert Wigbels <frickel@…>, 7 years ago

mqtttemp - Temp. / Hum. aus Sonoff

  • Property mode set to 100644
File size: 982 bytes
Line 
1require 'mqtt'
2require 'uri'
3require 'json'
4
5mypath = File.expand_path(File.dirname(__FILE__))
6
7File.readlines("/etc/default/mqtttemp").each do |line|
8 values = line.split("=")
9 ENV[values[0]] = values[1]
10end
11
12# Create a hash with the connection parameters from the URL
13uri = URI.parse ENV['CLOUDMQTT_URL']
14conn_opts = {
15 remote_host: uri.host,
16 remote_port: uri.port,
17 username: uri.user,
18 password: uri.password
19}
20topic = uri.path[1, uri.path.length] || 'tele/enklave/SENSOR'
21
22Thread.new do
23 MQTT::Client.connect(conn_opts) do |c|
24 c.get(topic) do |t, message|
25 puts "#{t}: #{message}"
26 my_hash = JSON.parse(message)
27 temperature = my_hash["SI7021"]["Temperature"]
28 humidity = my_hash["SI7021"]["Humidity"]
29 rrddata = "N:#{temperature}:U:U:U:U:U:U:U:U:#{humidity}:U:U:U:U:U:U:U:U"
30 puts rrddata
31 system("rrdtool update #{mypath}/mqtttemp.rrd #{rrddata}")
32 end
33 end
34end
35
36sleep
Note: See TracBrowser for help on using the repository browser.