Created
January 6, 2015 07:59
-
-
Save yoggy/c244d42c116c1351f861 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # 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