Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. require 'mqtt'
  4. require 'json'
  5.  
  6. org_id = "*****"
  7. mqtt_host = "#{org_id}.messaging.internetofthings.ibmcloud.com"
  8. mqtt_port = 1883
  9.  
  10. service_api_key = "*****"
  11. service_token = "*****"
  12.  
  13. device_type = "raspi"
  14. device_id = "12345"
  15.  
  16. app_id = "shinya"
  17. client_id = "a:#{org_id}:#{app_id}"
  18.  
  19. client = MQTT::Client.connect(
  20. :host => mqtt_host,
  21. :port => mqtt_port,
  22. :username => service_api_key,
  23. :password => service_token,
  24. :client_id => client_id
  25. )
  26.  
  27. event_id = "type01"
  28. format = "json"
  29.  
  30. # EVENT TOPIC
  31. topic = "iot-2/type/#{device_type}/id/#{device_id}/evt/#{event_id}/fmt/#{format}"
  32. #topic = "iot-2/type/#{device_type}/id/+/evt/#{event_id}/fmt/#{format}"
  33.  
  34. #COMMAND TOPIC
  35. #topic = "iot-2/type/#{device_type}/id/#{device_id}/cmd/#{command_id}/fmt/#{format}"
  36.  
  37. client.subscribe(topic)
  38.  
  39. client.get do |topic,message|
  40. # Block is executed for every message received
  41. p topic
  42. p message
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement