Advertisement
Guest User

Untitled

a guest
May 22nd, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <mosquittopp.h>
  3.  
  4. class mqtt_test : public mosqpp::mosquittopp {
  5. public:
  6. mqtt_test()
  7. : mosquittopp("idd", true)
  8. {
  9. login();
  10. setupTls();
  11. run();
  12. }
  13.  
  14. virtual void on_connect(int rc) override{
  15. std::cout << "Connected with " << rc << std::endl;
  16. send();
  17. }
  18.  
  19. virtual void on_publish(int rc) override {
  20. std::cout << "Publish callback" << std::endl;
  21. }
  22. private:
  23. void setupTls(){
  24. std::string cafilepath = "ca.crt";
  25. int rc = tls_set(cafilepath.c_str());
  26. rc = tls_insecure_set(true);
  27. }
  28.  
  29. void login(){
  30. std::string username = "advtest01";
  31. std::string password = "advtest01_2016";
  32. int rc = username_pw_set(username.c_str(), password.c_str());
  33. }
  34.  
  35. void run(){
  36. int keepalive = 60;
  37. int port = 8883;
  38. std::string host = "device-de-2.storalytics.ch";
  39. connect(host.c_str(), port, keepalive);
  40. std::cout << "Connecting..." << std::endl;
  41. }
  42.  
  43. void send(){
  44. std::string topic = "/devices/advtest01/30/events";
  45. std::string msg = "{\"event\":\"aadata\"}";
  46. publish(topic, msg, 1);
  47. }
  48.  
  49. void publish(std::string topic, std::string msg, int qos = 0){
  50. int rc = mosqpp::mosquittopp::publish(NULL, topic.c_str(), msg.length(), msg.c_str(), qos, false);
  51. }
  52. };
  53.  
  54. int main(int argc, char** argv){
  55. mosqpp::lib_init();
  56. mqtt_test test;
  57. test.loop_forever();
  58. mosqpp::lib_cleanup();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement