zoukankan      html  css  js  c++  java
  • mqtt学习-3 编译运行测试

    1、按上节第一种配置编译时报错,无法打开文件“paho-mqtt3as.lib”,主要是没有勾选Static,顺便把SSL也勾选,会更新编译列表选项,

    然后再点Configure如果没有错误,红色全部消失、Generate。

     2、编译依旧报错,不过已经不影响本次测试了,后面再处理这个问题

    严重性 代码 说明 项目 文件 行 禁止显示状态
    错误 LNK2019 无法解析的外部符号 OpenSSL_version,函数 MQTTClient_getVersionInfo 中引用了该符号 paho-mqtt3cs I:mqttpaho.mqtt.cuildsrcMQTTClient.obj 1

     原因是我这里用的openssl版本问题

    下载

    https://slproweb.com/download/Win64OpenSSL-1_1_1d.exe 

    安装

    设置这个路径

     之前自动选择的是C:OpenSSL-Win32下的lib文件

     configure,generate即可解决这个问题

     3、测试推送

    订阅,修改地址,用户名,密码为mqtt学习-1配置

    3.1修改I:mqttpaho.mqtt.csrcsamplesMQTTAsync_subscribe.c如下

    /*******************************************************************************
     * Copyright (c) 2012, 2020 IBM Corp.
     *
     * All rights reserved. This program and the accompanying materials
     * are made available under the terms of the Eclipse Public License v2.0
     * and Eclipse Distribution License v1.0 which accompany this distribution. 
     *
     * The Eclipse Public License is available at 
     *   https://www.eclipse.org/legal/epl-2.0/
     * and the Eclipse Distribution License is available at 
     *   http://www.eclipse.org/org/documents/edl-v10.php.
     *
     * Contributors:
     *    Ian Craggs - initial contribution
     *******************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "MQTTAsync.h"
    
    #if !defined(_WIN32)
    #include <unistd.h>
    #else
    #include <windows.h>
    #endif
    
    #if defined(_WRS_KERNEL)
    #include <OsWrapper.h>
    #endif
    
    #define ADDRESS     "tcp://127.0.0.1:1883"
    #define CLIENTID    "ExampleClientSub"
    #define TOPIC       "MQTT Examples"
    #define PAYLOAD     "Hello World!"
    #define QOS         1
    #define TIMEOUT     10000L
    
    int disc_finished = 0;
    int subscribed = 0;
    int finished = 0;
    
    void connlost(void *context, char *cause)
    {
    	MQTTAsync client = (MQTTAsync)context;
    	MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
    	int rc;
    
    	printf("
    Connection lost
    ");
    	if (cause)
    		printf("     cause: %s
    ", cause);
    
    	printf("Reconnecting
    ");
    	conn_opts.keepAliveInterval = 20;
    	conn_opts.cleansession = 1;
    	conn_opts.username = "admin";
    	conn_opts.password = "password";
    	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to start connect, return code %d
    ", rc);
    		finished = 1;
    	}
    }
    
    
    int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
    {
        printf("Message arrived
    ");
        printf("     topic: %s
    ", topicName);
        printf("   message: %.*s
    ", message->payloadlen, (char*)message->payload);
        MQTTAsync_freeMessage(&message);
        MQTTAsync_free(topicName);
        return 1;
    }
    
    void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
    {
    	printf("Disconnect failed, rc %d
    ", response->code);
    	disc_finished = 1;
    }
    
    void onDisconnect(void* context, MQTTAsync_successData* response)
    {
    	printf("Successful disconnection
    ");
    	disc_finished = 1;
    }
    
    void onSubscribe(void* context, MQTTAsync_successData* response)
    {
    	printf("Subscribe succeeded
    ");
    	subscribed = 1;
    }
    
    void onSubscribeFailure(void* context, MQTTAsync_failureData* response)
    {
    	printf("Subscribe failed, rc %d
    ", response->code);
    	finished = 1;
    }
    
    
    void onConnectFailure(void* context, MQTTAsync_failureData* response)
    {
    	printf("Connect failed, rc %d
    ", response->code);
    	finished = 1;
    }
    
    
    void onConnect(void* context, MQTTAsync_successData* response)
    {
    	MQTTAsync client = (MQTTAsync)context;
    	MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
    	int rc;
    
    	printf("Successful connection
    ");
    
    	printf("Subscribing to topic %s
    for client %s using QoS%d
    
    "
               "Press Q<Enter> to quit
    
    ", TOPIC, CLIENTID, QOS);
    	opts.onSuccess = onSubscribe;
    	opts.onFailure = onSubscribeFailure;
    	opts.context = client;
    	if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to start subscribe, return code %d
    ", rc);
    		finished = 1;
    	}
    }
    
    
    int main(int argc, char* argv[])
    {
    	MQTTAsync client;
    	MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
    	MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
    	int rc;
    	int ch;
    
    	if ((rc = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL))
    			!= MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to create client, return code %d
    ", rc);
    		rc = EXIT_FAILURE;
    		goto exit;
    	}
    
    	if ((rc = MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL)) != MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to set callbacks, return code %d
    ", rc);
    		rc = EXIT_FAILURE;
    		goto destroy_exit;
    	}
    
    	conn_opts.keepAliveInterval = 20;
    	conn_opts.cleansession = 1;
    	conn_opts.onSuccess = onConnect;
    	conn_opts.onFailure = onConnectFailure;
    	conn_opts.context = client;
    	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to start connect, return code %d
    ", rc);
    		rc = EXIT_FAILURE;
    		goto destroy_exit;
    	}
    
    	while (!subscribed && !finished)
    		#if defined(_WIN32)
    			Sleep(100);
    		#else
    			usleep(10000L);
    		#endif
    
    	if (finished)
    		goto exit;
    
    	do 
    	{
    		ch = getchar();
    	} while (ch!='Q' && ch != 'q');
    
    	disc_opts.onSuccess = onDisconnect;
    	disc_opts.onFailure = onDisconnectFailure;
    	if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to start disconnect, return code %d
    ", rc);
    		rc = EXIT_FAILURE;
    		goto destroy_exit;
    	}
     	while (!disc_finished)
     	{
    		#if defined(_WIN32)
    			Sleep(100);
    		#else
    			usleep(10000L);
    		#endif
     	}
    
    destroy_exit:
    	MQTTAsync_destroy(&client);
    exit:
     	return rc;
    }
    

    3.2修改I:mqttpaho.mqtt.csrcsamplesMQTTAsync_publish.c如下

    /*******************************************************************************
     * Copyright (c) 2012, 2020 IBM Corp.
     *
     * All rights reserved. This program and the accompanying materials
     * are made available under the terms of the Eclipse Public License v2.0
     * and Eclipse Distribution License v1.0 which accompany this distribution. 
     *
     * The Eclipse Public License is available at 
     *   https://www.eclipse.org/legal/epl-2.0/
     * and the Eclipse Distribution License is available at 
     *   http://www.eclipse.org/org/documents/edl-v10.php.
     *
     * Contributors:
     *    Ian Craggs - initial contribution
     *******************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "MQTTAsync.h"
    
    #if !defined(_WIN32)
    #include <unistd.h>
    #else
    #include <windows.h>
    #endif
    
    #if defined(_WRS_KERNEL)
    #include <OsWrapper.h>
    #endif
    
    #define ADDRESS     "tcp://127.0.0.1:1883"
    #define CLIENTID    "ExampleClientPub"
    #define TOPIC       "MQTT Examples"
    #define PAYLOAD     "Hello World!"
    #define QOS         1
    #define TIMEOUT     10000L
    
    int finished = 0;
    
    void connlost(void *context, char *cause)
    {
    	MQTTAsync client = (MQTTAsync)context;
    	MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
    	int rc;
    
    	printf("
    Connection lost
    ");
    	printf("     cause: %s
    ", cause);
    
    	printf("Reconnecting
    ");
    	conn_opts.keepAliveInterval = 20;
    	conn_opts.cleansession = 1;
    	conn_opts.username = "admin";
    	conn_opts.password = "password";
    	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to start connect, return code %d
    ", rc);
     		finished = 1;
    	}
    }
    
    void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
    {
    	printf("Disconnect failed
    ");
    	finished = 1;
    }
    
    void onDisconnect(void* context, MQTTAsync_successData* response)
    {
    	printf("Successful disconnection
    ");
    	finished = 1;
    }
    
    void onSendFailure(void* context, MQTTAsync_failureData* response)
    {
    	MQTTAsync client = (MQTTAsync)context;
    	MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
    	int rc;
    
    	printf("Message send failed token %d error code %d
    ", response->token, response->code);
    	opts.onSuccess = onDisconnect;
    	opts.onFailure = onDisconnectFailure;
    	opts.context = client;
    	if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to start disconnect, return code %d
    ", rc);
    		exit(EXIT_FAILURE);
    	}
    }
    
    void onSend(void* context, MQTTAsync_successData* response)
    {
    	MQTTAsync client = (MQTTAsync)context;
    	MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
    	int rc;
    
    	printf("Message with token value %d delivery confirmed
    ", response->token);
    	opts.onSuccess = onDisconnect;
    	opts.onFailure = onDisconnectFailure;
    	opts.context = client;
    	if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to start disconnect, return code %d
    ", rc);
    		exit(EXIT_FAILURE);
    	}
    }
    
    
    void onConnectFailure(void* context, MQTTAsync_failureData* response)
    {
    	printf("Connect failed, rc %d
    ", response ? response->code : 0);
    	finished = 1;
    }
    
    
    void onConnect(void* context, MQTTAsync_successData* response)
    {
    	MQTTAsync client = (MQTTAsync)context;
    	MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
    	MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
    	int rc;
    
    	printf("Successful connection
    ");
    	opts.onSuccess = onSend;
    	opts.onFailure = onSendFailure;
    	opts.context = client;
    	pubmsg.payload = PAYLOAD;
    	pubmsg.payloadlen = (int)strlen(PAYLOAD);
    	pubmsg.qos = QOS;
    	pubmsg.retained = 0;
    	if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to start sendMessage, return code %d
    ", rc);
    		exit(EXIT_FAILURE);
    	}
    }
    
    int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* m)
    {
    	/* not expecting any messages */
    	return 1;
    }
    
    int main(int argc, char* argv[])
    {
    	MQTTAsync client;
    	MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
    	int rc;
    
    	if ((rc = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to create client object, return code %d
    ", rc);
    		exit(EXIT_FAILURE);
    	}
    
    	if ((rc = MQTTAsync_setCallbacks(client, NULL, connlost, messageArrived, NULL)) != MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to set callback, return code %d
    ", rc);
    		exit(EXIT_FAILURE);
    	}
    
    	conn_opts.keepAliveInterval = 20;
    	conn_opts.cleansession = 1;
    	conn_opts.onSuccess = onConnect;
    	conn_opts.onFailure = onConnectFailure;
    	conn_opts.context = client;
    	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
    	{
    		printf("Failed to start connect, return code %d
    ", rc);
    		exit(EXIT_FAILURE);
    	}
    
    	printf("Waiting for publication of %s
    "
             "on topic %s for client with ClientID: %s
    ",
             PAYLOAD, TOPIC, CLIENTID);
    	while (!finished)
    		#if defined(_WIN32)
    			Sleep(100);
    		#else
    			usleep(10000L);
    		#endif
    
    	MQTTAsync_destroy(&client);
     	return rc;
    }
      
    

    3.3进入输出目录,将paho-mqtt3a.dll拷贝到Debug目录下

    先运行

     再运行

    说明运行正常 

      

    1、建了一个小群:616945527(软件), 欢迎大家加入,加群口令abc123,硬件嵌入式开发者推荐75764412(单片机)。
    闲置域名www.nsxz.com出售(等宽等高字符四字域名,可组合多种有意义词语)。
  • 相关阅读:
    基础
    条件语句/变量和基本数据类型
    编程语言介绍
    asp.net中log4net使用方法
    web布到服务器上出错
    《转》IEnumerable、IEnumerator两个接口的认识
    异步ADO.NET
    Session的使用
    AJAX参数及各种HTTP状态值
    简易的抓取别人网站内容
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/14491082.html
Copyright © 2011-2022 走看看