栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python与c++程序通过mqtt通信

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python与c++程序通过mqtt通信

mqtt安装与基本测试
#安装mosquitto mqtt
#引入仓库
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
#更新源
sudo apt-get update
#安装mosquitto
sudo apt install mosquitto
#安装客户端
sudo apt-get install mosquitto-clients
######测试
#一个终端开启sub mqtt
mosquitto_sub -h localhost -t "test_local" -v
#另一个终端发布信息,看能否收到
mosquitto_pub -h localhost -t "test_local" -m "HELLO MQTT"

若sub的中端收到,那么mqtt安装成功。
python部分
#api参考网站
https://pypi.org/project/paho-mqtt/#usage-and-api
#安装pip
sudo apt install python-pip
#安装python 的mqtt包
pip install paho-mqtt
publish.single("MQTT Examples", 2); 第一个参数为mqtt的topic第二个是内容。
import paho.mqtt.publish as publish

publish.single("MQTT Examples", 2)
c++部分

#paho.mqtt.c 库
https://github.com/eclipse/paho.mqtt.c

#下载oaho c库
git clone https://github.com/eclipse/paho.mqtt.c.git
#编译安装
make
sudo make install

#编译sub程序
g++ MQTTClient_subscribe.c -o sub -lpaho-mqtt3c


#include 
#include 
#include 
#include "MQTTClient.h"

#define ADDRESS     "localhost"
#define CLIENTID    "ExampleClientSub"
#define TOPIC       "MQTT Examples"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L

volatile MQTTClient_deliveryToken deliveredtoken;

void delivered(void *context, MQTTClient_deliveryToken dt)
{
    printf("Message with token value %d delivery /confirm/iedn", dt);
    deliveredtoken = dt;
}

int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    printf("Message arrivedn");
    printf("     topic: %sn", topicName);
    printf("   message: %.*sn", message->payloadlen, (char*)message->payload);
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}

void connlost(void *context, char *cause)
{
    printf("nConnection lostn");
    printf("     cause: %sn", cause);
}

int main(int argc, char* argv[])
{
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    int rc;

    if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
        MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to create client, return code %dn", rc);
        rc = EXIT_FAILURE;
        goto exit;
    }

    if ((rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to set callbacks, return code %dn", rc);
        rc = EXIT_FAILURE;
        goto destroy_exit;
    }

    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %dn", rc);
        rc = EXIT_FAILURE;
        goto destroy_exit;
    }

    printf("Subscribing to topic %snfor client %s using QoS%dnn"
           "Press Q to quitnn", TOPIC, CLIENTID, QOS);
    if ((rc = MQTTClient_subscribe(client, TOPIC, QOS)) != MQTTCLIENT_SUCCESS)
    {
    	printf("Failed to subscribe, return code %dn", rc);
    	rc = EXIT_FAILURE;
    }
    else
    {
    	int ch;
    	do
    	{
        	ch = getchar();
    	} while (ch!='Q' && ch != 'q');

        if ((rc = MQTTClient_unsubscribe(client, TOPIC)) != MQTTCLIENT_SUCCESS)
        {
        	printf("Failed to unsubscribe, return code %dn", rc);
        	rc = EXIT_FAILURE;
        }
    }

    if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
    {
    	printf("Failed to disconnect, return code %dn", rc);
    	rc = EXIT_FAILURE;
    }
destroy_exit:
    MQTTClient_destroy(&client);
exit:
    return rc;
}

通信

先执行c++的程序,然后执行python ,能够拿到数据。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/657135.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号