diff options
author | Joe Robinson <joe@lc8n.com> | 2019-02-25 20:20:30 +0000 |
---|---|---|
committer | Joe Robinson <joe@lc8n.com> | 2019-02-25 20:20:30 +0000 |
commit | 98bafb368104e8d58a8ac5a52908385e970b7903 (patch) | |
tree | 22b737b9d2abddbfb3b610f8481932eb01152fc8 /scarecrow.rb | |
parent | 943f992dc370237509f0b73dd9ccc99b2fecdf2f (diff) | |
parent | 5e0e922a7288e60a0482380a27a5c1ae53e6cd0e (diff) |
Switch to Paho MQTT library for automatic reconnects
See merge request wjoe/scarecrow!2
Diffstat (limited to 'scarecrow.rb')
-rw-r--r-- | scarecrow.rb | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/scarecrow.rb b/scarecrow.rb index bd5509f..793f5de 100644 --- a/scarecrow.rb +++ b/scarecrow.rb @@ -1,5 +1,5 @@ require 'sinatra' -require 'mqtt' +require 'paho-mqtt' require 'sqlite3' require 'date' require 'dm-core' @@ -43,15 +43,11 @@ def init DataMapper.auto_upgrade! # sleep 10 Thread.new do - MQTT::Client.connect('192.168.0.59') do |c| - c.subscribe('+/temperature', '+/humidity', '+/moisture') - loop do - while !c.queue_empty? do - topic, message = c.get - handle_msg(topic, message) - end - sleep 1 - end + mqtt_client = PahoMqtt::Client.new + mqtt_client.connect('192.168.0.59', 1883, mqtt_client.keep_alive, true, mqtt_client.blocking) + mqtt_client.subscribe('+/temperature', '+/humidity', '+/moisture') + mqtt_client.on_message do |m| + handle_msg(m.topic, m.payload) end end end @@ -106,7 +102,7 @@ def get_value(type, device_id) value = entry[:value] end - return value + return value end def get_time(type, device_id) @@ -124,7 +120,7 @@ def get_time(type, device_id) value = entry[:timestamp] end - return value + return value end get '/' do |