简体   繁体   English

Mqtt Raspberry PI C ++

[英]Mqtt Raspberry PI C++

I would like to make raspberry pi a publisher only by using MQTT protocol. 我想通过使用MQTT协议使raspberry pi成为发布者。 So far I have installed mosquitto library in my raspberry pi following this: 到目前为止,我已经在我的覆盆子pi中安装了mosquitto库:

http://www.instructables.com/id/Installing-MQTT-BrokerMosquitto-on-Raspberry-Pi/ http://www.instructables.com/id/Installing-MQTT-BrokerMosquitto-on-Raspberry-Pi/

I will only have to publish int values from it. 我只需要从中发布int值。 What I want to ask you guys is how will use mosquitto in my c++ program so I can achieve my goal mentioned above? 我想问你们的是如何在我的c ++程序中使用mosquitto,这样我才能达到上面提到的目标? (give me some guidance, links to where I can find something useful.) (给我一些指导,链接到我能找到有用的东西。)

You could do it easier by Python (especially when you are using Raspberry Pi). 你可以通过Python更容易地完成它(特别是当你使用Raspberry Pi时)。 This tutorial is good to start with (you may need Google translate because it's in Vietnamese). 教程很适合(您可能需要谷歌翻译,因为它是越南语)。

Basically, Raspberry Pi will be MQTT server and also MQTT client and it will Subscribe your Topic . 基本上,Raspberry Pi将是MQTT服务器和MQTT客户端,它将订阅您的主题 Your PC will be the second MQTT client and will Publish the Topic . 您的PC将成为第二个MQTT客户端,并将发布 主题

with assumption that you've already installed mosquitto library. 假设你已经安装了mosquitto库。 You can call the mqtt class as, 你可以调用mqtt类,

try {

    class myMqtt *qr2sp;
    //int rc;
    mosqpp::lib_init();

    qr2sp = new myMqtt ("qr2sp", "pcktatDoor", "192.168.178.100", 1883);

    while (1){

        /// call camera [qrcam()], read QR and send to publish()
        qr2sp->send_msg(qr2sp->qrcam().c_str());

        rc = qr2sp->loop();

        if (rc){
            qr2sp->reconnect();
}

and can declare the myMqtt class as, 并可以将myMqtt类声明为,

class myMqtt : public mosqpp::mosquittopp
{
  private:
   const char * host;
   const char * id;
   const char * topic;
   int      port;
   int      keepalive;

   void on_connect(int rc);
   void on_disconnect();
   void on_publish(int mid);

  public:
   myMqtt(const char *id, const char * _topic, const char *host, int port);
   ~myMqtt();
   bool send_msg(const char *message);
   std::string qrcam();

};

I wrote a little application with openCV and Mqtt using C++ - here [https]://github[.]com/meAbab/qrMqtt 我用openCV和Mqtt用C ++写了一个小应用程序 - 这里[https]:// github [。] com / meAbab / qrMqtt

Probably will help you to understand 可能会帮助你理解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM