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 | |
---|
1 | require 'mqtt'
|
---|
2 | require 'uri'
|
---|
3 | require 'json'
|
---|
4 |
|
---|
5 | mypath = File.expand_path(File.dirname(__FILE__))
|
---|
6 |
|
---|
7 | File.readlines("/etc/default/mqtttemp").each do |line|
|
---|
8 | values = line.split("=")
|
---|
9 | ENV[values[0]] = values[1]
|
---|
10 | end
|
---|
11 |
|
---|
12 | # Create a hash with the connection parameters from the URL
|
---|
13 | uri = URI.parse ENV['CLOUDMQTT_URL']
|
---|
14 | conn_opts = {
|
---|
15 | remote_host: uri.host,
|
---|
16 | remote_port: uri.port,
|
---|
17 | username: uri.user,
|
---|
18 | password: uri.password
|
---|
19 | }
|
---|
20 | topic = uri.path[1, uri.path.length] || 'tele/enklave/SENSOR'
|
---|
21 |
|
---|
22 | Thread.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
|
---|
34 | end
|
---|
35 |
|
---|
36 | sleep
|
---|
Note:
See
TracBrowser
for help on using the repository browser.