From 5e0e922a7288e60a0482380a27a5c1ae53e6cd0e Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Mon, 25 Feb 2019 20:16:29 +0000 Subject: Switch to Paho MQTT library for automatic reconnects --- scarecrow.rb | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'scarecrow.rb') 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 -- cgit v1.2.3