arm-linux-gnueabihf-gcc交叉编译MQTT C Client

参考

https://www.cnblogs.com/homejim/p/8120632.html

https://www.cnblogs.com/homejim/p/8146405.html

https://www.cnblogs.com/homejim/p/8196763.html

 

创建个目录
mkdir mqtt_demo
cd mqtt_demo

把source code拉下来
git clone https://github.com/eclipse/paho.mqtt.c.git

导出交叉编译工具链
export PATH=/home/xxx/tools/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin:$PATH

设定cmake参数
cmake -DPAHO_WITH_SSL=TRUE -DPAHO_BUILD_SAMPLES=TRUE -DOPENSSL_SEARCH_PATH=/home/xxx/openssl/lib -DOPENSSL_INCLUDE_DIR=/home/xxx/openssl/include -DCMAKE_TOOLCHAIN_FILE=/home/xxx/mqtt_demo/paho.mqtt.c/cmake/toolchain.linux-arm11.cmake ./paho.mqtt.c

编译
make

生成的lib在 ./src
 

 

基于paho.mqtt.c/src/samples/MQTTClient_publish.c,修改了一些代码,使得其支持ssl访问mqtt服务器(此demo无需证书认证)

/*******************************************************************************
 * Copyright (c) 2012, 2017 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *   http://www.eclipse.org/legal/epl-v10.html
 * 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 "MQTTClient.h"

#define ADDRESS     "ssl://localhost:1883"
#define CLIENTID    "ExampleClientPub"
#define TOPIC       "MQTT Examples"
#define PAYLOAD     "Hello World!"
#define USERNAME    "you_name"
#define PASSWORD    "you_password"
#define QOS         1
#define TIMEOUT     10000L

int main(int argc, char* argv[])
{
    MQTTClient_SSLOptions ssl = MQTTClient_SSLOptions_initializer;
    ssl.enableServerCertAuth = 0; //disable certificate
    ssl.sslVersion = MQTT_SSL_VERSION_TLS_1_0;

    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
    int rc;

    MQTTClient_create(&client, ADDRESS, CLIENTID,
        MQTTCLIENT_PERSISTENCE_NONE, NULL);
    if(!client)
    {
        printf("MQTTClient_create == NULL\n");
        exit(EXIT_FAILURE);
    }

    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    conn_opts.username = USERNAME;
    conn_opts.password = PASSWORD;
    conn_opts.ssl = &ssl;
    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }

    pubmsg.payload = PAYLOAD;
    pubmsg.payloadlen = (int)strlen(PAYLOAD);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;
    MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
    printf("Waiting for up to %d seconds for publication of %s\n"
            "on topic %s for client with ClientID: %s\n",
            (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
    rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
    printf("Message with delivery token %d delivered\n", token);
    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);
    return rc;
}

修改paho.mqtt.c/src/samples/CMakeLists.txt,让MQTTClient_publish.c去链接libpaho-mqtt3cs.so这个库,这些库的区别如下:

libpaho-mqtt3a.so - asynchronous
libpaho-mqtt3as.so - asynchronous with SSL
libpaho-mqtt3c.so - "classic" / synchronous
libpaho-mqtt3cs.so - "classic" / synchronous with SSL

编译

make -f src/samples/Makefile

bin文件生成在./src/samples/,可以拿到板子运行

 

注:代码里的这些定义是根据你具体的项目来配的,这里仅供demo

ADDRESS 
CLIENTID
TOPIC   
PAYLOAD 
USERNAME
PASSWORD

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cfl927096306

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值