IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    移植MQTT服务端mosquitto到嵌入式linux平台

    reille发表于 2019-03-15 00:00:09
    love 0

    从上年接触MQTT开始,就有看到mosquitto,但未仔细了解过,初步认为是运行在服务器上的MQTT代理服务软件。这两天突然有个想法,想在ARM板上运行MQTT服务程序,用于各模块间的消息通信。

    根据资料,发现mosquitto居然是个轻量级的MQTT服务程序,由MQTT协议创始人之一的Andy Stanford-Clark开发。许多人把它移植到了树莓派、ARM板上。

    mosquitto介绍

    官方网址:http://mosquitto.org/

    官网简介:Mosquitto is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 3.1 and 3.1.1. Mosquitto is lightweight and is suitable for use on all devices from low power single board computers to full servers.

    移植参考文章

    • 交叉编译mosquitto用于MX6G2C(ARM Cortex-A7)
    • mosquitto移植笔记
    • MQTT的学习之Mosquitto安装&使用(1),该文章里面有介绍配置文件config.mk的说明和mosquitto参数详细说明,对移植很有用。

    移植过程

    1. 下载源码,下载链接:http://mosquitto.org/files/source/,本人选择版本:1.4.15,理由是不选择最新版本,并且1.4.15版本是1.4版本最后发布的版本。

    2. 修改config.mk

    a) 文件开始地方增加如下:

    # added by gyr 2019.03.13
    CC=arm-linux-gnueabihf-gcc
    CXX=arm-linux-gnueabihf-g++
    CPP=arm-linux-gnueabihf-g++
    AR=arm-linux-gnueabihf-ar
    LD=arm-linux-gnueabihf-ld
    DESTDIR=install_out

    b) 在config.mk末尾:

    STRIP定义改为如下:

    STRIP?=arm-linux-gnueabihf-strip

    prefix定义改为如下:

    prefix=out

    c) 去除一些选项,如不使用SSL等:

    WITH_TLS:=no
    
    WITH_TLS_PSK:=no
    
    WITH_SRV:=no
    
    WITH_UUID:=no

    根据makefile,DESTDIR定义了执行make install时,mosquitto配置文件的安装路径,即位于mosquitto-1.4.15/install_out目录下

    3. 编译:make && make install

    编译出来的mosquitto、客户端、lib分别位于:

    mosquitto-1.4.15/src/install_outout/sbin
    mosquitto-1.4.15/client/install_outout/bin
    mosquitto-1.4.15/lib/install_outout/lib

    4. Error: Invalid user ‘mosquitto’.

    把mosquitto-1.4.15/install_out/etc/mosquito/mosquitto.conf.example文件重命名为mosquitto.conf,并修改该文件,把其中#user mosquitto改为user root,root为板子登陆的用户名。再把该mosquitto.conf上传到板子上。

    ./mosquitto -c mosquitto.conf

    通过-c选项指定mosquitto配置文件加载路径

    ./mosquitto -c mosquitto.conf

    259604: mosquitto version 1.4.15 (build date 2019-03-13 22:19:05+0800) starting

    259604: Config loaded from mosquitto.conf.

    259604: Opening ipv4 listen socket on port 1883.

    259604: Opening ipv6 listen socket on port 1883.

    259604: Warning: Mosquitto should not be run as root/administrator.

    这里有个Warning,经测试,可以忽略该Warning

    5. 测试:

    使用mosquitto_sub和mosquitto_pub进行测试

    这两个程序需要用到mosquitto动态库,先导出该库的路径:

    export LD_LIBRARY_PATH=/opt/app/mqtt/boker/lib

    否则会出错:

    ./mosquitto_sub -v -t mqtt_test

    ./mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

    订阅消息:

    ./mosquitto_sub -v -t mqtt_test

    发布消息:

    ./mosquitto_pub -t mqtt_test -m “hello world ”

    -t指定消息主题,-m指定消息内容

    » 本文地址: http://velep.com/archives/1423.html
    » 文章出处: reille博客—http://velep.com , 如果没有特别声明,文章均为reille博客原创作品
    » 郑重声明: 原创作品未经允许不得转载,如需转载请联系reille#qq.com(#换成@)
    分享到:
    推荐阅读相关文章:
    • 移植paho.mqtt.c到嵌入式linux平台
    • 移植paho.mqtt.embedded-c到嵌入式linux平台
    • error: ‘__locale_t’ has not been declared
    • Window不能访问Ubuntu16.04 Samba共享
    • 关于FLT_EPSILON、DBL_EPSILON、LDBL_EPSILON
    • 移植sqlite到嵌入式linux平台
    • stm32移植ecos #3,我的stm32开发板配置
    • stm32移植ecos #2,选购stm32开发板


沪ICP备19023445号-2号
友情链接