Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <mosquittopp.h>
- class mqtt_test : public mosqpp::mosquittopp {
- public:
- mqtt_test()
- : mosquittopp("idd", true)
- {
- login();
- setupTls();
- run();
- }
- virtual void on_connect(int rc) override{
- std::cout << "Connected with " << rc << std::endl;
- send();
- }
- virtual void on_publish(int rc) override {
- std::cout << "Publish callback" << std::endl;
- }
- private:
- void setupTls(){
- std::string cafilepath = "ca.crt";
- int rc = tls_set(cafilepath.c_str());
- rc = tls_insecure_set(true);
- }
- void login(){
- std::string username = "advtest01";
- std::string password = "advtest01_2016";
- int rc = username_pw_set(username.c_str(), password.c_str());
- }
- void run(){
- int keepalive = 60;
- int port = 8883;
- std::string host = "device-de-2.storalytics.ch";
- connect(host.c_str(), port, keepalive);
- std::cout << "Connecting..." << std::endl;
- }
- void send(){
- std::string topic = "/devices/advtest01/30/events";
- std::string msg = "{\"event\":\"aadata\"}";
- publish(topic, msg, 1);
- }
- void publish(std::string topic, std::string msg, int qos = 0){
- int rc = mosqpp::mosquittopp::publish(NULL, topic.c_str(), msg.length(), msg.c_str(), qos, false);
- }
- };
- int main(int argc, char** argv){
- mosqpp::lib_init();
- mqtt_test test;
- test.loop_forever();
- mosqpp::lib_cleanup();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement