Skip to content

Instantly share code, notes, and snippets.

@yoggy
Created January 6, 2015 07:59
Show Gist options
  • Select an option

  • Save yoggy/c244d42c116c1351f861 to your computer and use it in GitHub Desktop.

Select an option

Save yoggy/c244d42c116c1351f861 to your computer and use it in GitHub Desktop.
#
# mqtt publish sample
#
# $ gem install mqtt
# $ gem install pit
#
require 'rubygems'
require 'mqtt'
require 'json'
require 'pit'
def mqtt_pub(config_name, topic, key, value)
config = Pit.get(config_name,
:require => {
"remote_host" => "mqtt.example.com",
"remote_port" => 1883,
"username" => "username",
"password" => "password",
})
config["remote_port"] = config["remote_port"].to_i
begin
MQTT::Client.connect(config) do |c|
h = {}
h[key] = value
json_str = JSON.generate(h)
c.publish(topic, json_str)
end
rescue Exception => e
puts e
end
end
mqtt_pub("setting_name", "topic", "key", 12345)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment